<s> '''<STR_LIT>''' <EOL> import socket <EOL> import struct <EOL> import logging <EOL> import threading <EOL> import json <EOL> from socket import gethostname , getfqdn <EOL> class CoapObjectService ( object ) : <EOL> def __init__ ( self , baseObject = None , port = None ) : <EOL> if port == None : <EOL> self . _port = <NUM_LIT> <EOL> else : <EOL> self . _port = port <EOL> if baseObject == None : <EOL> from SmartObject . SmartObject import SmartObject <EOL> self . _baseObject = SmartObject ( ) <EOL> else : <EOL> self . _baseObject = baseObject <EOL> self . resources = self . _baseObject . resources <EOL> self . _host = gethostname ( ) <EOL> self . _baseObject . Properties . update ( { '<STR_LIT>' : '<STR_LIT>' + self . _host + '<STR_LIT::>' + repr ( self . _port ) } ) <EOL> self . _coapHandler = CoapRequestHandler ( self . _baseObject ) <EOL> self . _coapServer = COAPServer ( self . _host , self . _port , self . _coapHandler ) <EOL> print '<STR_LIT>' , baseObject . Properties . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def baseObject ( self ) : <EOL> return self . _baseObject <EOL> class CoapRequestHandler ( object ) : <EOL> def __init__ ( self , baseObject ) : <EOL> self . _linkCache = { } <EOL> self . _linkBaseDict = baseObject . resources <EOL> def do_GET ( self , path , options = None ) : <EOL> self . _query = None <EOL> self . _observing = False <EOL> for option in options : <EOL> ( number , value ) = option . values ( ) <EOL> if number == COAPOption . URI_QUERY : <EOL> self . _query = value <EOL> if number == COAPOption . OBSERVE : <EOL> self . _observing = True <EOL> self . _currentResource = self . linkToRef ( path ) <EOL> if hasattr ( self . _currentResource , '<STR_LIT>' ) : <EOL> self . _contentType = self . _currentResource . _serializeContentTypes [ <NUM_LIT:0> ] <EOL> return <NUM_LIT:200> , self . _currentResource . serialize ( self . _currentResource . get ( self . _query ) , self . _contentType ) , self . _contentType <EOL> else : <EOL> self . _contentType = '<STR_LIT:application/json>' <EOL> return <NUM_LIT:200> , json . dumps ( self . _currentResource . get ( ) ) , self . _contentType <EOL> def do_POST ( self , path , payload , options = None ) : <EOL> self . _currentResource = self . linkToRef ( path ) <EOL> if hasattr ( self . _currentResource , '<STR_LIT>' ) : <EOL> self . _contentType = self . _currentResource . _serializeContentTypes [ <NUM_LIT:0> ] <EOL> self . _currentResource . set ( self . _currentResource . parse ( str ( payload ) , self . _contentType ) ) <EOL> return <NUM_LIT:200> , '<STR_LIT>' , self . _contentType <EOL> else : <EOL> self . _contentType = '<STR_LIT:application/json>' <EOL> self . _currentResource . set ( json . loads ( str ( payload ) ) ) <EOL> return <NUM_LIT:200> , '<STR_LIT>' , self . _contentType <EOL> def do_PUT ( self , path , payload , flag ) : <EOL> pass <EOL> def do_DELETE ( self , path , payload , flag ) : <EOL> pass <EOL> def linkToRef ( self , linkPath ) : <EOL> '''<STR_LIT>''' <EOL> self . _linkPath = linkPath <EOL> if self . _linkPath in self . _linkCache . keys ( ) : <EOL> return self . _linkCache [ self . _linkPath ] <EOL> self . _currentDict = self . _linkBaseDict <EOL> self . _pathElements = linkPath . split ( '<STR_LIT:/>' ) <EOL> for pathElement in self . _pathElements [ : - <NUM_LIT:1> ] : <EOL> self . _currentDict = self . _currentDict [ pathElement ] . resources <EOL> self . _resource = self . _currentDict [ self . _pathElements [ - <NUM_LIT:1> ] ] <EOL> return self . _resource <EOL> def getByLink ( self , linkPath ) : <EOL> return self . linkToRef ( linkPath ) . get ( ) <EOL> def setByLink ( self , linkPath , newValue ) : <EOL> self . linkToRef ( linkPath ) . set ( newValue ) <EOL> '''<STR_LIT>''' <EOL> PYTHON_MAJOR = <NUM_LIT:2> <EOL> def info ( msg ) : <EOL> print ( msg ) <EOL> def exception ( e ) : <EOL> raise ( e ) <EOL> '''<STR_LIT>''' <EOL> M_PLAIN = "<STR_LIT>" <EOL> M_JSON = "<STR_LIT:application/json>" <EOL> if PYTHON_MAJOR >= <NUM_LIT:3> : <EOL> pass <EOL> else : <EOL> from urlparse import urlparse <EOL> GPIO = None <EOL> def HTTPCode2CoAPCode ( code ) : <EOL> return int ( code / <NUM_LIT:100> ) * <NUM_LIT:32> + ( code % <NUM_LIT:100> ) <EOL> class COAPContentFormat ( ) : <EOL> FORMATS = { <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT:50> : "<STR_LIT:application/json>" <EOL> } <EOL> @ staticmethod <EOL> def getCode ( fmt ) : <EOL> if fmt == None : <EOL> return None <EOL> for code in COAPContentFormat . FORMATS : <EOL> if COAPContentFormat . FORMATS [ code ] == fmt : <EOL> return code <EOL> return None <EOL> @ staticmethod <EOL> def toString ( code ) : <EOL> if code == None : <EOL> return None <EOL> if code in COAPContentFormat . FORMATS : <EOL> return COAPContentFormat . FORMATS [ code ] <EOL> raise Exception ( "<STR_LIT>" % code ) <EOL> class COAPOption ( ) : <EOL> OPTIONS = { <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" , <EOL> <NUM_LIT:7> : "<STR_LIT>" , <EOL> <NUM_LIT:8> : "<STR_LIT>" , <EOL> <NUM_LIT:11> : "<STR_LIT>" , <EOL> <NUM_LIT:12> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT:15> : "<STR_LIT>" , <EOL> <NUM_LIT:16> : "<STR_LIT>" , <EOL> <NUM_LIT:20> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" <EOL> } <EOL> IF_MATCH = <NUM_LIT:1> <EOL> URI_HOST = <NUM_LIT:3> <EOL> ETAG = <NUM_LIT:4> <EOL> IF_NONE_MATCH = <NUM_LIT:5> <EOL> OBSERVE = <NUM_LIT:6> <EOL> URI_PORT = <NUM_LIT:7> <EOL> LOCATION_PATH = <NUM_LIT:8> <EOL> URI_PATH = <NUM_LIT:11> <EOL> CONTENT_FORMAT = <NUM_LIT:12> <EOL> MAX_AGE = <NUM_LIT> <EOL> URI_QUERY = <NUM_LIT:15> <EOL> ACCEPT = <NUM_LIT:16> <EOL> LOCATION_QUERY = <NUM_LIT:20> <EOL> PROXY_URI = <NUM_LIT> <EOL> PROXY_SCHEME = <NUM_LIT> <EOL> class COAPMessage ( ) : <EOL> TYPES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> CON = <NUM_LIT:0> <EOL> NON = <NUM_LIT:1> <EOL> ACK = <NUM_LIT:2> <EOL> RST = <NUM_LIT:3> <EOL> def __init__ ( self , msg_type = <NUM_LIT:0> , code = <NUM_LIT:0> , uri = None ) : <EOL> self . version = <NUM_LIT:1> <EOL> self . type = msg_type <EOL> self . code = code <EOL> self . id = <NUM_LIT:0> <EOL> self . token = None <EOL> self . options = [ ] <EOL> self . host = "<STR_LIT>" <EOL> self . port = <NUM_LIT> <EOL> self . uri_path = "<STR_LIT>" <EOL> self . content_format = None <EOL> self . payload = None <EOL> if uri != None : <EOL> p = urlparse ( uri ) <EOL> self . host = p . hostname <EOL> if p . port : <EOL> self . port = int ( p . port ) <EOL> self . uri_path = p . path <EOL> def __getOptionHeader__ ( self , byte ) : <EOL> delta = ( byte & <NUM_LIT> ) >> <NUM_LIT:4> <EOL> length = byte & <NUM_LIT> <EOL> return ( delta , length ) <EOL> def __str__ ( self ) : <EOL> result = [ ] <EOL> result . append ( "<STR_LIT>" % self . version ) <EOL> result . append ( "<STR_LIT>" % self . TYPES [ self . type ] ) <EOL> result . append ( "<STR_LIT>" % self . CODES [ self . code ] ) <EOL> result . append ( "<STR_LIT>" % self . id ) <EOL> result . append ( "<STR_LIT>" % self . token ) <EOL> result . append ( "<STR_LIT>" % self . uri_path ) <EOL> result . append ( "<STR_LIT>" % ( COAPContentFormat . toString ( self . content_format ) if self . content_format else M_PLAIN ) ) <EOL> result . append ( "<STR_LIT>" % self . payload ) <EOL> result . append ( "<STR_LIT>" ) <EOL> return '<STR_LIT:\n>' . join ( result ) <EOL> def getOptionHeaderValue ( self , value ) : <EOL> if value > <NUM_LIT> : <EOL> return <NUM_LIT> <EOL> if value > <NUM_LIT:12> : <EOL> return <NUM_LIT> <EOL> return value <EOL> def getOptionHeaderExtension ( self , value ) : <EOL> buff = bytearray ( ) <EOL> v = self . getOptionHeaderValue ( value ) <EOL> if v == <NUM_LIT> : <EOL> value -= <NUM_LIT> <EOL> buff . append ( ( value & <NUM_LIT> ) >> <NUM_LIT:8> ) <EOL> buff . append ( value & <NUM_LIT> ) <EOL> elif v == <NUM_LIT> : <EOL> value -= <NUM_LIT> <EOL> buff . append ( value ) <EOL> return buff <EOL> def appendOption ( self , buff , lastnumber , option , data ) : <EOL> delta = option - lastnumber <EOL> length = len ( data ) <EOL> d = self . getOptionHeaderValue ( delta ) <EOL> l = self . getOptionHeaderValue ( length ) <EOL> b = <NUM_LIT:0> <EOL> b |= ( d << <NUM_LIT:4> ) & <NUM_LIT> <EOL> b |= l & <NUM_LIT> <EOL> buff . append ( b ) <EOL> ext = self . getOptionHeaderExtension ( delta ) ; <EOL> for b in ext : <EOL> buff . append ( b ) <EOL> ext = self . getOptionHeaderExtension ( length ) ; <EOL> for b in ext : <EOL> buff . append ( b ) <EOL> for b in data : <EOL> buff . append ( b ) <EOL> return option <EOL> def getBytes ( self ) : <EOL> buff = bytearray ( ) <EOL> byte = ( self . version & <NUM_LIT> ) << <NUM_LIT:6> <EOL> byte |= ( self . type & <NUM_LIT> ) << <NUM_LIT:4> <EOL> if self . token : <EOL> token_len = min ( len ( self . token ) , <NUM_LIT:8> ) ; <EOL> else : <EOL> token_len = <NUM_LIT:0> <EOL> byte |= token_len <EOL> buff . append ( byte ) <EOL> buff . append ( self . code ) <EOL> buff . append ( ( self . id & <NUM_LIT> ) >> <NUM_LIT:8> ) <EOL> buff . append ( self . id & <NUM_LIT> ) <EOL> if self . token : <EOL> for c in self . token : <EOL> buff . append ( c ) <EOL> lastnumber = <NUM_LIT:0> <EOL> if len ( self . uri_path ) > <NUM_LIT:0> : <EOL> paths = self . uri_path . split ( "<STR_LIT:/>" ) <EOL> for p in paths : <EOL> if len ( p ) > <NUM_LIT:0> : <EOL> if PYTHON_MAJOR >= <NUM_LIT:3> : <EOL> data = p . encode ( ) <EOL> else : <EOL> data = bytearray ( p ) <EOL> lastnumber = self . appendOption ( buff , lastnumber , COAPOption . URI_PATH , data ) <EOL> if self . content_format != None : <EOL> data = bytearray ( ) <EOL> fmt_code = self . content_format <EOL> if fmt_code > <NUM_LIT> : <EOL> data . append ( ( fmt_code & <NUM_LIT> ) >> <NUM_LIT:8> ) <EOL> data . append ( fmt_code & <NUM_LIT> ) <EOL> lastnumber = self . appendOption ( buff , lastnumber , COAPOption . CONTENT_FORMAT , data ) <EOL> buff . append ( <NUM_LIT> ) <EOL> if self . payload : <EOL> if PYTHON_MAJOR >= <NUM_LIT:3> : <EOL> data = self . payload . encode ( ) <EOL> else : <EOL> data = bytearray ( self . payload ) <EOL> for c in data : <EOL> buff . append ( c ) <EOL> return buff <EOL> def parseByteArray ( self , buff ) : <EOL> self . version = ( buff [ <NUM_LIT:0> ] & <NUM_LIT> ) >> <NUM_LIT:6> <EOL> self . type = ( buff [ <NUM_LIT:0> ] & <NUM_LIT> ) >> <NUM_LIT:4> <EOL> token_length = buff [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> index = <NUM_LIT:4> <EOL> if token_length > <NUM_LIT:0> : <EOL> self . token = buff [ index : index + token_length ] <EOL> index += token_length <EOL> self . code = buff [ <NUM_LIT:1> ] <EOL> self . id = ( buff [ <NUM_LIT:2> ] << <NUM_LIT:8> ) | buff [ <NUM_LIT:3> ] <EOL> number = <NUM_LIT:0> <EOL> while index < len ( buff ) and buff [ index ] != <NUM_LIT> : <EOL> ( delta , length ) = self . __getOptionHeader__ ( buff [ index ] ) <EOL> offset = <NUM_LIT:1> <EOL> if delta == <NUM_LIT> : <EOL> delta += buff [ index + offset ] <EOL> offset += <NUM_LIT:1> <EOL> elif delta == <NUM_LIT> : <EOL> delta += <NUM_LIT:255> + ( ( buff [ index + offset ] << <NUM_LIT:8> ) | buff [ index + offset + <NUM_LIT:1> ] ) <EOL> offset += <NUM_LIT:2> <EOL> if length == <NUM_LIT> : <EOL> length += buff [ index + offset ] <EOL> offset += <NUM_LIT:1> <EOL> elif length == <NUM_LIT> : <EOL> length += <NUM_LIT:255> + ( ( buff [ index + offset ] << <NUM_LIT:8> ) | buff [ index + offset + <NUM_LIT:1> ] ) <EOL> offset += <NUM_LIT:2> <EOL> number += delta <EOL> valueBytes = buff [ index + offset : index + offset + length ] <EOL> if number in [ COAPOption . IF_MATCH , COAPOption . ETAG ] : <EOL> value = valueBytes <EOL> elif number in [ COAPOption . URI_PORT , COAPOption . CONTENT_FORMAT , COAPOption . MAX_AGE , COAPOption . ACCEPT ] : <EOL> value = <NUM_LIT:0> <EOL> for b in valueBytes : <EOL> value <<= <NUM_LIT:8> <EOL> value |= b <EOL> else : <EOL> if PYTHON_MAJOR >= <NUM_LIT:3> : <EOL> value = valueBytes . decode ( ) <EOL> else : <EOL> value = str ( valueBytes ) <EOL> self . options . append ( { '<STR_LIT>' : number , '<STR_LIT:value>' : value } ) <EOL> index += offset + length <EOL> index += <NUM_LIT:1> <EOL> if len ( buff ) > index : <EOL> self . payload = buff [ index : ] <EOL> else : <EOL> self . payload = "<STR_LIT>" <EOL> for option in self . options : <EOL> ( number , value ) = option . values ( ) <EOL> if number == COAPOption . URI_PATH : <EOL> self . uri_path += "<STR_LIT>" % value <EOL> class COAPRequest ( COAPMessage ) : <EOL> CODES = { <NUM_LIT:0> : None , <EOL> <NUM_LIT:1> : "<STR_LIT:GET>" , <EOL> <NUM_LIT:2> : "<STR_LIT:POST>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" <EOL> } <EOL> GET = <NUM_LIT:1> <EOL> POST = <NUM_LIT:2> <EOL> PUT = <NUM_LIT:3> <EOL> DELETE = <NUM_LIT:4> <EOL> def __init__ ( self , msg_type = <NUM_LIT:0> , code = <NUM_LIT:0> , uri = None ) : <EOL> COAPMessage . __init__ ( self , msg_type , code , uri ) <EOL> class COAPGet ( COAPRequest ) : <EOL> def __init__ ( self , uri ) : <EOL> COAPRequest . __init__ ( self , COAPMessage . CON , COAPRequest . GET , uri ) <EOL> class COAPPost ( COAPRequest ) : <EOL> def __init__ ( self , uri ) : <EOL> COAPRequest . __init__ ( self , COAPMessage . CON , COAPRequest . POST , uri ) <EOL> class COAPPut ( COAPRequest ) : <EOL> def __init__ ( self , uri ) : <EOL> COAPRequest . __init__ ( self , COAPMessage . CON , COAPRequest . PUT , uri ) <EOL> class COAPDelete ( COAPRequest ) : <EOL> def __init__ ( self , uri ) : <EOL> COAPRequest . __init__ ( self , COAPMessage . CON , COAPRequest . DELETE , uri ) <EOL> class COAPResponse ( COAPMessage ) : <EOL> CODES = { <NUM_LIT:0> : None , <EOL> <NUM_LIT:64> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" <EOL> } <EOL> OK = <NUM_LIT:64> <EOL> CREATED = <NUM_LIT> <EOL> DELETED = <NUM_LIT> <EOL> VALID = <NUM_LIT> <EOL> CHANGED = <NUM_LIT> <EOL> CONTENT = <NUM_LIT> <EOL> BAD_REQUEST = <NUM_LIT> <EOL> UNAUTHORIZED = <NUM_LIT> <EOL> BAD_OPTION = <NUM_LIT> <EOL> FORBIDDEN = <NUM_LIT> <EOL> NOT_FOUND = <NUM_LIT> <EOL> NOT_ALLOWED = <NUM_LIT> <EOL> NOT_ACCEPTABLE = <NUM_LIT> <EOL> PRECONDITION_FAILED = <NUM_LIT> <EOL> ENTITY_TOO_LARGE = <NUM_LIT> <EOL> UNSUPPORTED_CONTENT = <NUM_LIT> <EOL> INTERNAL_ERROR = <NUM_LIT> <EOL> NOT_IMPLEMENTED = <NUM_LIT> <EOL> BAD_GATEWAY = <NUM_LIT> <EOL> SERVICE_UNAVAILABLE = <NUM_LIT> <EOL> GATEWAY_TIMEOUT = <NUM_LIT> <EOL> PROXYING_NOT_SUPPORTED = <NUM_LIT> <EOL> def __init__ ( self ) : <EOL> COAPMessage . __init__ ( self ) <EOL> class COAPClient ( ) : <EOL> def __init__ ( self ) : <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> self . socket . settimeout ( <NUM_LIT:1.0> ) <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , socket . IP_MULTICAST_TTL , <NUM_LIT:2> ) <EOL> def sendRequest ( self , message ) : <EOL> data = message . getBytes ( ) ; <EOL> sent = <NUM_LIT:0> <EOL> while sent < <NUM_LIT:4> : <EOL> try : <EOL> self . socket . sendto ( data , ( message . host , message . port ) ) <EOL> data = self . socket . recv ( <NUM_LIT> ) <EOL> response = COAPResponse ( ) <EOL> response . parseByteArray ( bytearray ( data ) ) <EOL> return response <EOL> except socket . timeout : <EOL> sent += <NUM_LIT:1> <EOL> return None <EOL> class COAPServer ( threading . Thread ) : <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def __init__ ( self , host , port , handler ) : <EOL> threading . Thread . __init__ ( self , name = "<STR_LIT>" ) <EOL> self . handler = COAPHandler ( handler ) <EOL> self . host = host <EOL> self . port = port <EOL> self . multicast_ip = '<STR_LIT>' <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> self . socket . bind ( ( '<STR_LIT>' , port ) ) <EOL> self . socket . settimeout ( <NUM_LIT:1> ) <EOL> self . running = True <EOL> self . daemon = True <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> info ( "<STR_LIT>" % ( self . host , self . port ) ) <EOL> while self . running == True : <EOL> try : <EOL> ( request , client ) = self . socket . recvfrom ( <NUM_LIT> ) <EOL> requestBytes = bytearray ( request ) <EOL> coapRequest = COAPRequest ( ) <EOL> coapRequest . parseByteArray ( requestBytes ) <EOL> coapResponse = COAPResponse ( ) <EOL> self . processMessage ( coapRequest , coapResponse ) <EOL> responseBytes = coapResponse . getBytes ( ) <EOL> self . socket . sendto ( responseBytes , client ) <EOL> self . logger . debug ( '<STR_LIT>' % ( coapRequest . CODES [ coapRequest . code ] , coapRequest . uri_path , coapRequest . version , coapResponse . CODES [ coapResponse . code ] ) ) <EOL> except socket . timeout as e : <EOL> continue <EOL> except Exception as e : <EOL> if self . running == True : <EOL> exception ( e ) <EOL> info ( "<STR_LIT>" ) <EOL> def enableMulticast ( self ) : <EOL> while not self . running : <EOL> pass <EOL> mreq = struct . pack ( "<STR_LIT>" , socket . inet_aton ( self . multicast_ip ) , socket . INADDR_ANY ) <EOL> self . socket . setsockopt ( socket . IPPROTO_IP , socket . IP_ADD_MEMBERSHIP , mreq ) <EOL> info ( "<STR_LIT>" % ( self . multicast_ip , self . port ) ) <EOL> def stop ( self ) : <EOL> self . running = False <EOL> self . socket . close ( ) <EOL> def processMessage ( self , request , response ) : <EOL> if request . type == COAPMessage . CON : <EOL> response . type = COAPMessage . ACK <EOL> else : <EOL> response . type = COAPMessage . NON <EOL> if request . token : <EOL> response . token = request . token <EOL> response . id = request . id <EOL> response . uri_path = request . uri_path <EOL> if request . code == COAPRequest . GET : <EOL> self . handler . do_GET ( request , response ) <EOL> elif request . code == COAPRequest . POST : <EOL> self . handler . do_POST ( request , response ) <EOL> elif request . code / <NUM_LIT:32> == <NUM_LIT:0> : <EOL> response . code = COAPResponse . NOT_IMPLEMENTED <EOL> else : <EOL> exception ( Exception ( "<STR_LIT>" % response ) ) <EOL> class COAPHandler ( ) : <EOL> def __init__ ( self , handler ) : <EOL> self . handler = handler <EOL> def do_GET ( self , request , response ) : <EOL> try : <EOL> ( code , body , contentType ) = self . handler . do_GET ( request . uri_path [ <NUM_LIT:1> : ] , request . options ) <EOL> if code == <NUM_LIT:0> : <EOL> response . code = COAPResponse . NOT_FOUND <EOL> elif code == <NUM_LIT:200> : <EOL> response . code = COAPResponse . CONTENT <EOL> else : <EOL> response . code = HTTPCode2CoAPCode ( code ) <EOL> response . payload = body <EOL> response . content_format = COAPContentFormat . getCode ( contentType ) <EOL> except Exception as e : <EOL> response . code = COAPResponse . INTERNAL_ERROR <EOL> raise e <EOL> def do_POST ( self , request , response ) : <EOL> try : <EOL> ( code , body , contentType ) = self . handler . do_POST ( request . uri_path [ <NUM_LIT:1> : ] , request . payload , request . options ) <EOL> if code == <NUM_LIT:0> : <EOL> response . code = COAPResponse . NOT_FOUND <EOL> elif code == <NUM_LIT:200> : <EOL> response . code = COAPResponse . CHANGED <EOL> else : <EOL> response . code = HTTPCode2CoAPCode ( code ) <EOL> response . payload = body <EOL> response . content_format = COAPContentFormat . getCode ( contentType ) <EOL> except Exception as e : <EOL> response . code = COAPResponse . INTERNAL_ERROR <EOL> raise e </s>
