Skip to content

Feature: Handling server canceled observation #15

@arvindh123

Description

@arvindh123

Is your feature request related to a problem? Please describe.

Canceled observation by server are not detected and handled by CLI

Describe the feature you are requesting, as well as the possible use case(s) for it.

Detecting the server canceled observer

We should retrieve the observer on each time the function is called
If there is no Observe option is not found , then server has canceled the observer ("This need to verify with coap protocol spec")

// Receive receives a message.
func (c Client) Receive(path string, verbose bool, opts ...message.Option) (mux.Observation, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()

	return c.conn.Observe(ctx, path, func(res *pool.Message) {
		body, err := res.ReadBody()
		if err != nil {
			fmt.Println("Error reading message body: ", err)
			return
		}
		bodySize, err := res.BodySize()
		if err != nil {
			fmt.Println("Error getting body size: ", err)
			return
		}
		if _, err := res.Observe(); err != nil {
			if !errors.Is(err, message.ErrOptionNotFound) {
				fmt.Println("Error getting Observer: ", err)
			}
			cancel()
			return
		}

		if bodySize == 0 {
			fmt.Println("Received observe")
		}
		switch verbose {
		case true:
			fmt.Printf(verboseFmt,
				time.Now().Format(time.RFC1123),
				res.Code(),
				res.Type(),
				res.Token(),
				res.MessageID(),
				bodySize)
			if len(body) > 0 {
				fmt.Printf("Payload: %s\n\n", string(body))
			}
		case false:
			if len(body) > 0 {
				fmt.Printf("Payload: %s\n", string(body))
			}
		}
	}, opts...)
}

In the above same code, difference highlighted version:

// Receive receives a message.
func (c Client) Receive(path string, verbose bool, opts ...message.Option) (mux.Observation, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()

	return c.conn.Observe(ctx, path, func(res *pool.Message) {
		body, err := res.ReadBody()
		if err != nil {
			fmt.Println("Error reading message body: ", err)
			return
		}
		bodySize, err := res.BodySize()
		if err != nil {
			fmt.Println("Error getting body size: ", err)
			return
		}
+		if _, err := res.Observe(); err != nil {
+			if !errors.Is(err, message.ErrOptionNotFound) {
+				fmt.Println("Error getting Observer: ", err)
+			}
+			cancel()
+			return
+		}

		if bodySize == 0 {
			fmt.Println("Received observe")
		}
		switch verbose {
		case true:
			fmt.Printf(verboseFmt,
				time.Now().Format(time.RFC1123),
				res.Code(),
				res.Type(),
				res.Token(),
				res.MessageID(),
				bodySize)
			if len(body) > 0 {
				fmt.Printf("Payload: %s\n\n", string(body))
			}
		case false:
			if len(body) > 0 {
				fmt.Printf("Payload: %s\n", string(body))
			}
		}
	}, opts...)
}

Indicate the importance of this feature to you.

Must-have

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions