Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Handling server canceled observation #15

Open
arvindh123 opened this issue Nov 4, 2024 · 0 comments
Open

Feature: Handling server canceled observation #15

arvindh123 opened this issue Nov 4, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@arvindh123
Copy link

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

@arvindh123 arvindh123 added the enhancement New feature or request label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant