gotthardp / gen_coap

Generic Erlang CoAP Client/Server
Other
106 stars 47 forks source link

got_non phase in transport loops? #21

Closed tpitale closed 6 years ago

tpitale commented 6 years ago

Hey @gotthardp, I was looking at https://github.com/gotthardp/gen_coap/blob/master/src/coap_transport.erl#L80-L82 and trying to figure out how the transport gets out of the got_non phase, or how the transport "completes" otherwise.

Would you be able to enlighten me on this? Thanks for the help.

tpitale commented 6 years ago

I think that non means non-ack'd, but I'm still learning. I was sort of expecting there to be a got_non({out, Message}) or something for when the responder is done with handle_request or something.

tpitale commented 6 years ago

Or if non means there is no response at all. What would be the normal exit state of the transport? I can't seem to find how it would timeout from this phase.

gotthardp commented 6 years ago

NON means "Non-confirmable" message per CoAP RFC. NON (from the transport level) does not have any response. It's just one-shot message. The transport level is terminated on timeout (see timeout_after).

tpitale commented 6 years ago

So timeout_after goes to the channel which calls back to coap_transport:timeout with an Event and the state of the transport. This goes here: https://github.com/gotthardp/gen_coap/blob/master/src/coap_transport.erl#L41-L42

Which would call got_non({timeout … I think?

tpitale commented 6 years ago

Random/related question: does pack => "peer ack" and aack => "application ack"?

gotthardp commented 6 years ago

I wrote it more than a year ago, so I don't remember everything, but yes, pack => "peer ack" and aack => "application ack"

tpitale commented 6 years ago

got_non({timeout … doesn't appear to be in coap_transport. I may not know enough erlang to know that something else might catch this function call?

gotthardp commented 6 years ago

Both NON and CON timeout at the transport level via the "transport" timeout handled on the beginning of the file https://github.com/gotthardp/gen_coap/blob/master/src/coap_transport.erl#L59 https://github.com/gotthardp/gen_coap/blob/master/src/coap_transport.erl#L38

tpitale commented 6 years ago

I see in idle there is a timeout_after for the atom transport. Which goes to timeout(transport, _State) and returns undefined.

So … does the transport just terminate when the channel does?

gotthardp commented 6 years ago

The transport terminates itself after the lifetime timeout.

tpitale commented 6 years ago

I just realized that the transport itself isn't a gen_server. The inclusion of state in the functions had me thrown for a bit there 😄.

So when undefined is returned, the channel no longer has state for the transport here: https://github.com/gotthardp/gen_coap/blob/master/src/coap_channel.erl#L137-L142

Make sense now, thank you for being so patient!

gotthardp commented 6 years ago

Oh yes, it's not a gen_server, it's not even a process. When undefined is returned, it is removed from the dict and that's it. https://github.com/gotthardp/gen_coap/blob/master/src/coap_channel.erl#L184