Coap state management
State Management for CoAP Protocol
import coap_message
object coap_interaction_state = {
type this = {
IDLE,
REQUEST_SENT,
RESPONSE_RECEIVED,
COMPLETED,
OBSERVING,
ERROR
}
}
type coap_interaction = struct {
state : coap_interaction_state,
request : coap_message, # The original request message
response : coap_message, # The response message, if any
error : string # Error details, if any
}
var interactions : map[ip.endpoint, coap_interaction]
after init {
}
action update_interaction_state(endpoint:ip.endpoint, new_state:coap_interaction_state) = {
if endpoint in interactions {
interactions[endpoint].state := new_state
}
}
action record_response(endpoint:ip.endpoint, response:coap_message) = {
if endpoint in interactions {
interactions[endpoint].response := response
update_interaction_state(endpoint, RESPONSE_RECEIVED)
}
}
action handle_interaction_error(endpoint:ip.endpoint, error_msg:string) = {
if endpoint in interactions {
interactions[endpoint].error := error_msg
update_interaction_state(endpoint, ERROR)
}
}