plgd-dev / go-coap

Implementation of CoAP Server & Client in Go
https://coap.technology
Apache License 2.0
449 stars 116 forks source link

How do I access Message ID in a handler method? #215

Closed emmanuelay closed 1 year ago

emmanuelay commented 3 years ago

I am writing a CoAP server which accepts requests using UDP. My code utilizes handlers and the signature looks like this:

func (s *Server) defaultHandler(w mux.ResponseWriter, r *message.Message) {
...
}

I have two questions that I would really appreciate if you could help me out with:

1) How do I access the message ID that the client sent to the server from within the handler above? The reason is that I want to store the message ID and perform an eventual ACK at a later stage.

2) If the client has sent a confirmable message to the server, and I wish to confirm it later in a completely separate process. Does the server automatically send ACKs which is implied in the documentation? And if so, is there any way to prevent the package from sending an ACK automatically?

emmanuelay commented 3 years ago

Maybe @jkralik knows more about this? :-)

jkralik commented 3 years ago

@emmanuelay

  1. How do I access the message ID that the client sent to the server from within the handler above? The reason is that I want to store the message ID and perform an eventual ACK at a later stage.

When you create server you need to use option udp.WithHandlerFunc instead of udp.WithMux. At the handler you get msg *pool.Message which has functions msg.MessageID() https://github.com/plgd-dev/go-coap/blob/4d466801917705aec7cd97b6b53c7502d4c1f7c5/udp/message/pool/message.go#L52

  1. If the client has sent a confirmable message to the server, and I wish to confirm it later in a completely separate process. Does the server automatically send ACKs which is implied in the documentation? And if so, is there any way to prevent the package from sending an ACK automatically?

Yes it is send automatically. It sends ack in separated message/response of request. For your usecase just don't send the response immediately and ACK will be send via separate message. And then you can create response later after you process request similar as: https://github.com/plgd-dev/go-coap/blob/4d466801917705aec7cd97b6b53c7502d4c1f7c5/examples/observe/server/main.go#L78

JosefWN commented 2 years ago

Would be useful to be able to access the MessageID somehow in logging middlewares using udp.WithMux. Logging the message id helps debugging client issues, for example. Right now I'm using a logging CoAP proxy for this.

jkralik commented 2 years ago

Would be useful to be able to access the MessageID somehow in logging middlewares using udp.WithMux. Logging the message id helps debugging client issues, for example. Right now I'm using a logging CoAP proxy for this.

@JosefWN with blockwise, mux gets completed constructed message, so it is not possible at the layer mux. In go-coap/v3 you can read all MessageID via https://github.com/plgd-dev/go-coap/blob/b8901963ae43acd9a14f98b3d47ec17b26b38e7a/options/commonOptions.go#L278

jkralik commented 1 year ago

Feel free to reopen it but v3 allows you to set MessageId and also Type.