<s> """<STR_LIT>""" <EOL> import sys <EOL> import fnmatch <EOL> from twisted . internet import defer <EOL> from twisted . internet . protocol import DatagramProtocol <EOL> from twisted . internet import reactor <EOL> from twisted . python import log <EOL> import txthings . coap as coap <EOL> import txthings . resource as resource <EOL> import txthings . ext . link_header as link_header <EOL> DEFAULT_LIFETIME = <NUM_LIT> <EOL> def parseUriQuery ( query_list ) : <EOL> query_dict = { } <EOL> for query in query_list : <EOL> name , value = query . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if name in query_dict : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> query_dict [ name ] = value <EOL> return query_dict <EOL> class DirectoryResource ( resource . CoAPResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> resource . CoAPResource . __init__ ( self ) <EOL> self . visible = True <EOL> self . addParam ( resource . LinkParam ( "<STR_LIT:title>" , "<STR_LIT>" ) ) <EOL> self . directory = { } <EOL> self . eid = <NUM_LIT:0> <EOL> def render_POST ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . opt . content_format is not coap . media_types_rev [ '<STR_LIT>' ] : <EOL> log . msg ( '<STR_LIT>' ) <EOL> response = coap . Message ( code = coap . UNSUPPORTED_CONTENT_FORMAT , payload = '<STR_LIT>' ) <EOL> return defer . succeed ( response ) <EOL> try : <EOL> params = parseUriQuery ( request . opt . uri_query ) <EOL> except ValueError : <EOL> log . msg ( "<STR_LIT>" ) <EOL> response = coap . Message ( code = coap . BAD_REQUEST , payload = "<STR_LIT>" ) <EOL> return defer . succeed ( response ) <EOL> if '<STR_LIT>' not in params : <EOL> log . msg ( "<STR_LIT>" ) <EOL> response = coap . Message ( code = coap . BAD_REQUEST , payload = "<STR_LIT>" ) <EOL> return defer . succeed ( response ) <EOL> endpoint = params [ '<STR_LIT>' ] <EOL> if endpoint in self . directory : <EOL> del self . children [ self . directory [ endpoint ] ] <EOL> else : <EOL> self . directory [ endpoint ] = str ( self . eid ) <EOL> self . eid += <NUM_LIT:1> <EOL> link_format = link_header . parse_link_value ( request . payload ) <EOL> domain = params . get ( '<STR_LIT:d>' , '<STR_LIT>' ) <EOL> endpoint_type = params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> lifetime = params . get ( '<STR_LIT>' , DEFAULT_LIFETIME ) <EOL> context = params . get ( '<STR_LIT>' , '<STR_LIT>' + request . remote [ <NUM_LIT:0> ] + "<STR_LIT::>" + str ( request . remote [ <NUM_LIT:1> ] ) ) <EOL> entry = DirectoryEntryResource ( self , link_format , endpoint , domain , endpoint_type , lifetime , context ) <EOL> self . putChild ( self . directory [ endpoint ] , entry ) <EOL> response = coap . Message ( code = coap . CREATED , payload = '<STR_LIT>' ) <EOL> response . opt . location_path = ( '<STR_LIT>' , self . directory [ endpoint ] ) <EOL> log . msg ( "<STR_LIT>" % ( endpoint , lifetime ) ) <EOL> return defer . succeed ( response ) <EOL> class DirectoryEntryResource ( resource . CoAPResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , link_format , endpoint , domain , endpoint_type , lifetime , context ) : <EOL> resource . CoAPResource . __init__ ( self ) <EOL> self . visible = False <EOL> self . parent = parent <EOL> self . endpoint = endpoint <EOL> self . link_format = link_format <EOL> self . domain = domain <EOL> self . endpoint_type = endpoint_type <EOL> self . context = context <EOL> self . timeout = reactor . callLater ( float ( lifetime ) , self . removeResource ) <EOL> def render_PUT ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> params = parseUriQuery ( request . opt . uri_query ) <EOL> except ValueError : <EOL> log . msg ( "<STR_LIT>" ) <EOL> response = coap . Message ( code = coap . BAD_REQUEST , payload = "<STR_LIT>" ) <EOL> return defer . succeed ( response ) <EOL> lifetime = params . get ( '<STR_LIT>' , DEFAULT_LIFETIME ) <EOL> self . timeout . cancel ( ) <EOL> self . timeout = reactor . callLater ( float ( lifetime ) , self . removeResource ) <EOL> self . endpoint_type = params . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . context = params . get ( '<STR_LIT>' , '<STR_LIT>' + request . remote [ <NUM_LIT:0> ] + "<STR_LIT::>" + str ( request . remote [ <NUM_LIT:1> ] ) ) <EOL> response = coap . Message ( code = coap . CHANGED , payload = '<STR_LIT>' ) <EOL> log . msg ( "<STR_LIT>" % ( self . endpoint , lifetime ) ) <EOL> return defer . succeed ( response ) <EOL> def render_DELETE ( self , request ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" % self . endpoint ) <EOL> self . removeResource ( ) <EOL> response = coap . Message ( code = coap . DELETED , payload = '<STR_LIT>' ) <EOL> return defer . succeed ( response ) <EOL> def removeResource ( self ) : <EOL> """<STR_LIT>""" <EOL> location = self . parent . directory . pop ( self . endpoint ) <EOL> del ( self . parent . children [ location ] ) <EOL> class EndpointLookupResource ( resource . CoAPResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory_resource ) : <EOL> resource . CoAPResource . __init__ ( self ) <EOL> self . visible = True <EOL> self . directory = directory_resource <EOL> def render_GET ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> params = parseUriQuery ( request . opt . uri_query ) <EOL> except ValueError : <EOL> log . msg ( "<STR_LIT>" ) <EOL> response = coap . Message ( code = coap . BAD_REQUEST , payload = "<STR_LIT>" ) <EOL> return defer . succeed ( response ) <EOL> ep_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> d_pattern = params . get ( '<STR_LIT:d>' , '<STR_LIT:*>' ) <EOL> et_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> link_format = [ ] <EOL> first_entry = True <EOL> for entry in self . directory . children . values ( ) : <EOL> if fnmatch . fnmatch ( entry . endpoint , ep_pattern ) and fnmatch . fnmatch ( entry . domain , d_pattern ) and fnmatch . fnmatch ( entry . endpoint_type , et_pattern ) : <EOL> if first_entry is True : <EOL> first_entry = False <EOL> else : <EOL> link_format . append ( '<STR_LIT:U+002C>' ) <EOL> link_format . append ( '<STR_LIT:<>' ) <EOL> link_format . append ( entry . context ) <EOL> link_format . append ( '<STR_LIT>' ) <EOL> link_format . append ( entry . endpoint ) <EOL> link_format . append ( '<STR_LIT:">' ) <EOL> response = coap . Message ( code = coap . CONTENT , payload = '<STR_LIT>' . join ( link_format ) ) <EOL> response . opt . content_format = coap . media_types_rev [ '<STR_LIT>' ] <EOL> return defer . succeed ( response ) <EOL> class ResourceLookupResource ( resource . CoAPResource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , directory_resource ) : <EOL> resource . CoAPResource . __init__ ( self ) <EOL> self . visible = True <EOL> self . directory = directory_resource <EOL> def render_GET ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> params = parseUriQuery ( request . opt . uri_query ) <EOL> except ValueError : <EOL> log . msg ( "<STR_LIT>" ) <EOL> response = coap . Message ( code = coap . BAD_REQUEST , payload = "<STR_LIT>" ) <EOL> return defer . succeed ( response ) <EOL> ep_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> d_pattern = params . get ( '<STR_LIT:d>' , '<STR_LIT:*>' ) <EOL> et_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> rt_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> title_pattern = params . get ( '<STR_LIT:title>' , '<STR_LIT:*>' ) <EOL> if_pattern = params . get ( '<STR_LIT>' , '<STR_LIT:*>' ) <EOL> link_format = [ ] <EOL> first_entry = True <EOL> for entry in self . directory . children . values ( ) : <EOL> if fnmatch . fnmatch ( entry . endpoint , ep_pattern ) and fnmatch . fnmatch ( entry . domain , d_pattern ) and fnmatch . fnmatch ( entry . endpoint_type , et_pattern ) : <EOL> for uri , params in entry . link_format . items ( ) : <EOL> if fnmatch . fnmatch ( params . get ( '<STR_LIT>' , '<STR_LIT>' ) , rt_pattern ) and fnmatch . fnmatch ( params . get ( '<STR_LIT:title>' , '<STR_LIT>' ) , title_pattern ) and fnmatch . fnmatch ( params . get ( '<STR_LIT>' , '<STR_LIT>' ) , if_pattern ) : <EOL> if first_entry is True : <EOL> first_entry = False <EOL> else : <EOL> link_format . append ( '<STR_LIT:U+002C>' ) <EOL> link_format . append ( '<STR_LIT:<>' ) <EOL> link_format . append ( entry . context ) <EOL> link_format . append ( uri ) <EOL> link_format . append ( '<STR_LIT:>>' ) <EOL> for name , value in params . items ( ) : <EOL> link_format . append ( '<STR_LIT:;>' ) <EOL> link_format . append ( name ) <EOL> link_format . append ( '<STR_LIT>' ) <EOL> link_format . append ( value ) <EOL> link_format . append ( '<STR_LIT:">' ) <EOL> response = coap . Message ( code = coap . CONTENT , payload = '<STR_LIT>' . join ( link_format ) ) <EOL> response . opt . content_format = coap . media_types_rev [ '<STR_LIT>' ] <EOL> return defer . succeed ( response ) <EOL> log . startLogging ( sys . stdout ) <EOL> root = resource . CoAPResource ( ) <EOL> rd = DirectoryResource ( ) <EOL> root . putChild ( '<STR_LIT>' , rd ) <EOL> lookup = resource . CoAPResource ( ) <EOL> root . putChild ( '<STR_LIT>' , lookup ) <EOL> ep_lookup = EndpointLookupResource ( rd ) <EOL> lookup . putChild ( '<STR_LIT>' , ep_lookup ) <EOL> res_lookup = ResourceLookupResource ( rd ) <EOL> lookup . putChild ( '<STR_LIT>' , res_lookup ) <EOL> endpoint = resource . Endpoint ( root ) <EOL> protocol = coap . Coap ( endpoint ) <EOL> reactor . listenUDP ( coap . COAP_PORT , protocol ) <EOL> reactor . run ( ) </s>
