<div><div>Hello,</div><div><br></div><div>I am implementing UDP-based protocol in Twisted (CoAP) which allows two behaviors when answering requests:</div><div><br></div><div>1. If response is immediately available - send acknowledgement and response in a single datagram (piggyback response)</div>

<div>2. If response needs to be fetched or prepared - send datagram with acknowledgement, and then send another datagram with a response (separate response)</div><div><br></div><div>(I think behavior #1 is called synchronous in most Twisted tutorials, and behavior #2 is called asynchronous.)</div>
<div><br></div><div>When programmer is implementing his application on top of CoAP protocol, he or she needs to choose how his request handler is going to behave. I would like to handle both behaviors in the same manner - by forcing every user-written request handler to return Deferred. Then I would check Deferred.called parameter. </div>

<div>1. If True - callback will execute immediately and send proper ACK+Response (that means request handler used defer.succeed() or somethin similar)</div><div>2. If False I send empty ACK, and wait for callback to send Response</div>

<div><br></div><div>code:</div><div>def respond(request):</div><div>        d = requestHandler(request)</div><div>        if d.called is False:</div><div>            sendEmptyAck()</div><div>        d.addCallback(sendResponse)</div>
</div>
<div><br></div><div>I assume that sendResponse can send either ACK+RSP, or only RSP.</div><div><br></div><div>I would like to ask if this is a proper approach?</div><div><br></div><div>Best Regards</div><div>Maciej Wasilak</div>