#NEWLINE# def _receive( self , timestamp , sender , rawbytes ) : #NEWLINE# #INDENT# output = [ ] #NEWLINE# output += [ '\n{0} _receive message:' . format ( self . name ) ] #NEWLINE# output += [ '- timestamp: {0}' . format ( timestamp ) ] #NEWLINE# output += [ '- sender:    {0}' . format ( sender ) ] #NEWLINE# output += [ '- bytes:     {0}' . format ( u . formatBuf ( rawbytes ) ) ] #NEWLINE# output = '\n' . join ( output ) #NEWLINE# log . debug ( output ) #NEWLINE# srcIp = sender [ 0 ] #NEWLINE# srcIp = u . trimAddress ( srcIp ) #NEWLINE# srcPort = sender [ 1 ] #NEWLINE# try : #NEWLINE# #INDENT# message = m . parseMessage ( rawbytes ) #NEWLINE# #UNINDENT# except e . messageFormatError as err : #NEWLINE# #INDENT# log . warning ( 'malformed message {0}: {1}' . format ( u . formatBuf ( rawbytes ) , str ( err ) ) ) #NEWLINE# return #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# if ( message [ 'code' ] in d . METHOD_ALL ) : #NEWLINE# #INDENT# path = coapUri . options2path ( message [ 'options' ] ) #NEWLINE# log . debug ( 'path="{0}"' . format ( path ) ) #NEWLINE# resource = None #NEWLINE# with self . resourceLock : #NEWLINE# #INDENT# for r in self . resources : #NEWLINE# #INDENT# if r . matchesPath ( path ) : #NEWLINE# #INDENT# resource = r #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# log . debug ( 'resource={0}' . format ( resource ) ) #NEWLINE# if ( not resource ) : #NEWLINE# #INDENT# raise e . coapRcNotFound ( ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# if ( message [ 'code' ] == d . METHOD_GET ) : #NEWLINE# #INDENT# ( respCode , respOptions , respPayload ) = resource . GET ( options = message [ 'options' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'code' ] == d . METHOD_POST ) : #NEWLINE# #INDENT# ( respCode , respOptions , respPayload ) = resource . POST ( options = message [ 'options' ] , payload = message [ 'payload' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'code' ] == d . METHOD_PUT ) : #NEWLINE# #INDENT# ( respCode , respOptions , respPayload ) = resource . PUT ( options = message [ 'options' ] , payload = message [ 'payload' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'code' ] == d . METHOD_DELETE ) : #NEWLINE# #INDENT# ( respCode , respOptions , respPayload ) = resource . DELETE ( options = message [ 'options' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise SystemError ( 'unexpected code {0}' . format ( message [ 'code' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# if isinstance ( err , e . coapRc ) : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise e . coapRcInternalServerError ( ) #NEWLINE# #UNINDENT# #UNINDENT# if ( message [ 'type' ] == d . TYPE_CON ) : #NEWLINE# #INDENT# responseType = d . TYPE_ACK #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'type' ] == d . TYPE_NON ) : #NEWLINE# #INDENT# responseType = d . TYPE_NON #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise SystemError ( 'unexpected type {0}' . format ( message [ 'type' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# response = m . buildMessage ( msgtype = responseType , token = message [ 'token' ] , code = respCode , messageId = message [ 'messageId' ] , options = respOptions , payload = respPayload ) #NEWLINE# self . socketUdp . sendUdp ( destIp = srcIp , destPort = srcPort , msg = response ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'code' ] in d . COAP_RC_ALL ) : #NEWLINE# #INDENT# msgkey = ( srcIp , srcPort , message [ 'token' ] , message [ 'messageId' ] ) #NEWLINE# found = False #NEWLINE# with self . transmittersLock : #NEWLINE# #INDENT# self . _cleanupTransmitter ( ) #NEWLINE# for ( k , v ) in self . transmitters . items ( ) : #NEWLINE# #INDENT# if ( ( msgkey [ 0 ] == k [ 0 ] ) and ( msgkey [ 1 ] == k [ 1 ] ) and ( ( msgkey [ 2 ] == k [ 2 ] ) or ( msgkey [ 3 ] == k [ 3 ] ) ) ) : #NEWLINE# #INDENT# found = True #NEWLINE# v . receiveMessage ( timestamp , srcIp , srcPort , message ) #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( found == False ) : #NEWLINE# #INDENT# raise e . coapRcBadRequest ( 'could not find transmitter corresponding to {0}, transmitters are {1}' . format ( msgkey , ',' . join ( [ str ( k ) for k in self . transmitters . keys ( ) ] ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise NotImplementedError ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except e . coapRc as err : #NEWLINE# #INDENT# log . warning ( err ) #NEWLINE# if ( message [ 'type' ] == d . TYPE_CON ) : #NEWLINE# #INDENT# responseType = d . TYPE_ACK #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( message [ 'type' ] == d . TYPE_NON ) : #NEWLINE# #INDENT# responseType = d . TYPE_NON #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise SystemError ( 'unexpected type {0}' . format ( message [ 'type' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# response = m . buildMessage ( msgtype = responseType , token = message [ 'token' ] , code = err . rc , messageId = message [ 'messageId' ] ) #NEWLINE# self . socketUdp . sendUdp ( destIp = srcIp , destPort = srcPort , msg = response ) #NEWLINE# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# log . critical ( traceback . format_exc ( ) )