<s> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> import time <EOL> from types import * <EOL> import threading <EOL> import collections <EOL> import random <EOL> import nfc . clf <EOL> import nfc . dep <EOL> from tco import * <EOL> from pdu import * <EOL> from err import * <EOL> from opt import * <EOL> RAW_ACCESS_POINT , LOGICAL_DATA_LINK , DATA_LINK_CONNECTION = range ( <NUM_LIT:3> ) <EOL> wks_map = { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : <NUM_LIT:4> } <EOL> class ServiceAccessPoint ( object ) : <EOL> def __init__ ( self , addr , llc ) : <EOL> self . llc = llc <EOL> self . addr = addr <EOL> self . sock_list = collections . deque ( ) <EOL> self . send_list = collections . deque ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . addr ) <EOL> @ property <EOL> def mode ( self ) : <EOL> with self . llc . lock : <EOL> try : <EOL> if isinstance ( self . sock_list [ <NUM_LIT:0> ] , RawAccessPoint ) : <EOL> return RAW_ACCESS_POINT <EOL> if isinstance ( self . sock_list [ <NUM_LIT:0> ] , LogicalDataLink ) : <EOL> return LOGICAL_DATA_LINK <EOL> if isinstance ( self . sock_list [ <NUM_LIT:0> ] , DataLinkConnection ) : <EOL> return DATA_LINK_CONNECTION <EOL> except IndexError : return <NUM_LIT:0> <EOL> def insert_socket ( self , socket ) : <EOL> with self . llc . lock : <EOL> try : insertable = type ( socket ) == type ( self . sock_list [ <NUM_LIT:0> ] ) <EOL> except IndexError : insertable = True <EOL> if insertable : <EOL> socket . bind ( self . addr ) <EOL> self . sock_list . appendleft ( socket ) <EOL> else : log . error ( "<STR_LIT>" ) <EOL> return insertable <EOL> def remove_socket ( self , socket ) : <EOL> assert socket . addr == self . addr <EOL> socket . close ( ) <EOL> with self . llc . lock : <EOL> try : self . sock_list . remove ( socket ) <EOL> except ValueError : pass <EOL> if len ( self . sock_list ) == <NUM_LIT:0> : <EOL> self . llc . sap [ self . addr ] = None <EOL> def send ( self , pdu ) : <EOL> self . send_list . append ( pdu ) <EOL> def shutdown ( self ) : <EOL> while True : <EOL> try : socket = self . sock_list . pop ( ) <EOL> except IndexError : return <EOL> log . debug ( "<STR_LIT>" % str ( socket ) ) <EOL> socket . bind ( None ) ; socket . close ( ) <EOL> def enqueue ( self , pdu ) : <EOL> with self . llc . lock : <EOL> if isinstance ( pdu , Connect ) : <EOL> for socket in self . sock_list : <EOL> if socket . state . LISTEN : <EOL> socket . enqueue ( pdu ) <EOL> return <EOL> else : <EOL> for socket in self . sock_list : <EOL> if pdu . ssap == socket . peer or socket . peer is None : <EOL> socket . enqueue ( pdu ) <EOL> return <EOL> if pdu . type in connection_mode_pdu_types : <EOL> self . send ( DisconnectedMode ( pdu . ssap , pdu . dsap , reason = <NUM_LIT:1> ) ) <EOL> def dequeue ( self , max_size ) : <EOL> with self . llc . lock : <EOL> for socket in self . sock_list : <EOL> pdu = socket . dequeue ( max_size ) <EOL> if pdu : return pdu <EOL> else : <EOL> try : return self . send_list . popleft ( ) <EOL> except IndexError : pass <EOL> def sendack ( self , max_size ) : <EOL> with self . llc . lock : <EOL> for socket in self . sock_list : <EOL> pdu = socket . sendack ( max_size ) <EOL> if pdu : return pdu <EOL> class ServiceDiscovery ( object ) : <EOL> def __init__ ( self , llc ) : <EOL> self . llc = llc <EOL> self . snl = dict ( ) <EOL> self . tids = range ( <NUM_LIT> ) <EOL> self . resp = threading . Condition ( self . llc . lock ) <EOL> self . sent = dict ( ) <EOL> self . sdreq = collections . deque ( ) <EOL> self . sdres = collections . deque ( ) <EOL> self . dmpdu = collections . deque ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def mode ( self ) : <EOL> return LOGICAL_DATA_LINK <EOL> def resolve ( self , name ) : <EOL> with self . resp : <EOL> if self . snl is None : return None <EOL> log . debug ( "<STR_LIT>" . format ( name ) ) <EOL> try : return self . snl [ name ] <EOL> except KeyError : pass <EOL> tid = random . choice ( self . tids ) <EOL> self . tids . remove ( tid ) <EOL> self . sdreq . append ( ( tid , name ) ) <EOL> while not self . snl is None and not name in self . snl : <EOL> self . resp . wait ( ) <EOL> return None if self . snl is None else self . snl [ name ] <EOL> def enqueue ( self , pdu ) : <EOL> with self . llc . lock : <EOL> if isinstance ( pdu , ServiceNameLookup ) and not self . snl is None : <EOL> for tid , sap in pdu . sdres : <EOL> try : name = self . sent [ tid ] <EOL> except KeyError : pass <EOL> else : <EOL> log . debug ( "<STR_LIT>" <EOL> . format ( name , sap ) ) <EOL> self . snl [ name ] = sap <EOL> self . tids . append ( tid ) <EOL> self . resp . notify_all ( ) <EOL> for tid , name in pdu . sdreq : <EOL> try : sap = self . llc . snl [ name ] <EOL> except KeyError : sap = <NUM_LIT:0> <EOL> self . sdres . append ( ( tid , sap ) ) <EOL> def dequeue ( self , max_size ) : <EOL> if max_size < <NUM_LIT:2> : <EOL> return None <EOL> with self . llc . lock : <EOL> if len ( self . sdres ) > <NUM_LIT:0> or len ( self . sdreq ) > <NUM_LIT:0> : <EOL> pdu = ServiceNameLookup ( dsap = <NUM_LIT:1> , ssap = <NUM_LIT:1> ) <EOL> max_size -= len ( pdu ) <EOL> while max_size > <NUM_LIT:0> : <EOL> try : pdu . sdres . append ( self . sdres . popleft ( ) ) <EOL> except IndexError : break <EOL> for i in range ( len ( self . sdreq ) ) : <EOL> tid , name = self . sdreq [ <NUM_LIT:0> ] <EOL> if <NUM_LIT:1> + len ( name ) > max_size : <EOL> self . sdreq . rotate ( - <NUM_LIT:1> ) <EOL> else : <EOL> pdu . sdreq . append ( self . sdreq . popleft ( ) ) <EOL> self . sent [ tid ] = name <EOL> return pdu <EOL> if len ( self . dmpdu ) > <NUM_LIT:0> and max_size >= <NUM_LIT:2> : <EOL> return self . dmpdu . popleft ( ) <EOL> def shutdown ( self ) : <EOL> with self . llc . lock : <EOL> self . snl = None <EOL> self . resp . notify_all ( ) <EOL> class LogicalLinkController ( object ) : <EOL> def __init__ ( self , recv_miu = <NUM_LIT> , send_lto = <NUM_LIT> , send_agf = True , <EOL> symm_log = True ) : <EOL> self . lock = threading . RLock ( ) <EOL> self . cfg = dict ( ) <EOL> self . cfg [ '<STR_LIT>' ] = recv_miu <EOL> self . cfg [ '<STR_LIT>' ] = send_lto <EOL> self . cfg [ '<STR_LIT>' ] = send_agf <EOL> self . cfg [ '<STR_LIT>' ] = symm_log <EOL> self . snl = dict ( { "<STR_LIT>" : <NUM_LIT:1> } ) <EOL> self . sap = <NUM_LIT:64> * [ None ] <EOL> self . sap [ <NUM_LIT:0> ] = ServiceAccessPoint ( <NUM_LIT:0> , self ) <EOL> self . sap [ <NUM_LIT:1> ] = ServiceDiscovery ( self ) <EOL> def __str__ ( self ) : <EOL> local = "<STR_LIT>" . format ( <EOL> miu = self . cfg . get ( '<STR_LIT>' ) , lto = self . cfg . get ( '<STR_LIT>' ) ) <EOL> remote = "<STR_LIT>" . format ( <EOL> miu = self . cfg . get ( '<STR_LIT>' ) , lto = self . cfg . get ( '<STR_LIT>' ) ) <EOL> return "<STR_LIT>" . format ( local = local , remote = remote ) <EOL> def activate ( self , mac ) : <EOL> assert type ( mac ) in ( nfc . dep . Initiator , nfc . dep . Target ) <EOL> self . mac = None <EOL> miu = self . cfg [ '<STR_LIT>' ] <EOL> lto = self . cfg [ '<STR_LIT>' ] <EOL> wks = <NUM_LIT:1> + sum ( sorted ( [ <NUM_LIT:1> << sap for sap in self . snl . values ( ) if sap < <NUM_LIT:15> ] ) ) <EOL> pax = ParameterExchange ( version = ( <NUM_LIT:1> , <NUM_LIT:1> ) , miu = miu , lto = lto , wks = wks ) <EOL> if type ( mac ) == nfc . dep . Initiator : <EOL> gb = mac . activate ( gbi = '<STR_LIT>' + pax . to_string ( ) [ <NUM_LIT:2> : ] ) <EOL> self . run = self . run_as_initiator <EOL> role = "<STR_LIT>" <EOL> if type ( mac ) == nfc . dep . Target : <EOL> gb = mac . activate ( gbt = '<STR_LIT>' + pax . to_string ( ) [ <NUM_LIT:2> : ] , wt = <NUM_LIT:9> ) <EOL> self . run = self . run_as_target <EOL> role = "<STR_LIT>" <EOL> if gb is not None and gb . startswith ( '<STR_LIT>' ) and len ( gb ) >= <NUM_LIT:6> : <EOL> info = [ "<STR_LIT>" . format ( role ) ] <EOL> info . append ( "<STR_LIT>" ) <EOL> info . append ( "<STR_LIT>" . format ( pax . version ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . lto ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . miu ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . wks ) ) <EOL> pax = ProtocolDataUnit . from_string ( "<STR_LIT>" + str ( gb [ <NUM_LIT:3> : ] ) ) <EOL> info . append ( "<STR_LIT>" ) <EOL> info . append ( "<STR_LIT>" . format ( pax . version ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . lto ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . miu ) ) <EOL> info . append ( "<STR_LIT>" . format ( pax . wks ) ) <EOL> log . info ( '<STR_LIT:\n>' . join ( info ) ) <EOL> self . cfg [ '<STR_LIT>' ] = pax . version <EOL> self . cfg [ '<STR_LIT>' ] = pax . miu <EOL> self . cfg [ '<STR_LIT>' ] = pax . lto <EOL> self . cfg [ '<STR_LIT>' ] = pax . wks <EOL> self . cfg [ '<STR_LIT>' ] = pax . lsc <EOL> log . debug ( "<STR_LIT>" . format ( self . cfg ) ) <EOL> if type ( mac ) == nfc . dep . Initiator and mac . rwt is not None : <EOL> max_rwt = <NUM_LIT> / <NUM_LIT> * <NUM_LIT:2> ** <NUM_LIT:10> <EOL> if mac . rwt > max_rwt : <EOL> log . warning ( "<STR_LIT>" <EOL> . format ( mac . rwt , max_rwt ) ) <EOL> self . mac = mac <EOL> return bool ( self . mac ) <EOL> def terminate ( self , reason ) : <EOL> log . debug ( "<STR_LIT>" . format ( reason ) ) <EOL> if reason == "<STR_LIT>" : <EOL> self . exchange ( Disconnect ( <NUM_LIT:0> , <NUM_LIT:0> ) , timeout = <NUM_LIT:0.1> ) <EOL> self . mac . deactivate ( ) <EOL> elif reason == "<STR_LIT>" : <EOL> self . mac . deactivate ( ) <EOL> for i in range ( <NUM_LIT> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if not self . sap [ i ] is None : <EOL> log . debug ( "<STR_LIT>" % i ) <EOL> self . sap [ i ] . shutdown ( ) <EOL> self . sap [ i ] = None <EOL> def exchange ( self , pdu , timeout ) : <EOL> if not isinstance ( pdu , Symmetry ) or self . cfg . get ( '<STR_LIT>' ) is True : <EOL> log . debug ( "<STR_LIT>" . format ( pdu ) ) <EOL> data = pdu . to_string ( ) if pdu else None <EOL> try : <EOL> data = self . mac . exchange ( data , timeout ) <EOL> if data is None : return None <EOL> except nfc . clf . DigitalProtocolError as error : <EOL> log . debug ( "<STR_LIT>" . format ( error ) ) <EOL> return None <EOL> pdu = ProtocolDataUnit . from_string ( data ) <EOL> if not isinstance ( pdu , Symmetry ) or self . cfg . get ( '<STR_LIT>' ) is True : <EOL> log . debug ( "<STR_LIT>" . format ( pdu ) ) <EOL> return pdu <EOL> def run_as_initiator ( self , terminate = lambda : False ) : <EOL> recv_timeout = <NUM_LIT> * ( self . cfg [ '<STR_LIT>' ] + <NUM_LIT:10> ) <EOL> symm = <NUM_LIT:0> <EOL> try : <EOL> pdu = self . collect ( delay = <NUM_LIT> ) <EOL> while not terminate ( ) : <EOL> if pdu is None : pdu = Symmetry ( ) <EOL> pdu = self . exchange ( pdu , recv_timeout ) <EOL> if pdu is None : <EOL> return self . terminate ( reason = "<STR_LIT>" ) <EOL> if pdu == Disconnect ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> return self . terminate ( reason = "<STR_LIT>" ) <EOL> symm = symm + <NUM_LIT:1> if type ( pdu ) == Symmetry else <NUM_LIT:0> <EOL> self . dispatch ( pdu ) <EOL> pdu = self . collect ( delay = <NUM_LIT> ) <EOL> if pdu is None and symm >= <NUM_LIT:10> : <EOL> pdu = self . collect ( delay = <NUM_LIT> ) <EOL> else : <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> raise KeyboardInterrupt <EOL> except IOError : <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> raise SystemExit <EOL> finally : <EOL> log . debug ( "<STR_LIT>" ) <EOL> def run_as_target ( self , terminate = lambda : False ) : <EOL> recv_timeout = <NUM_LIT> * ( self . cfg [ '<STR_LIT>' ] + <NUM_LIT:10> ) <EOL> symm = <NUM_LIT:0> <EOL> try : <EOL> pdu = None <EOL> while not terminate ( ) : <EOL> pdu = self . exchange ( pdu , recv_timeout ) <EOL> if pdu is None : <EOL> return self . terminate ( reason = "<STR_LIT>" ) <EOL> if pdu == Disconnect ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> return self . terminate ( reason = "<STR_LIT>" ) <EOL> symm = symm + <NUM_LIT:1> if type ( pdu ) == Symmetry else <NUM_LIT:0> <EOL> self . dispatch ( pdu ) <EOL> pdu = self . collect ( delay = <NUM_LIT> ) <EOL> if pdu is None and symm >= <NUM_LIT:10> : <EOL> pdu = self . collect ( delay = <NUM_LIT> ) <EOL> if pdu is None : pdu = Symmetry ( ) <EOL> else : <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> raise KeyboardInterrupt <EOL> except IOError : <EOL> self . terminate ( reason = "<STR_LIT>" ) <EOL> raise SystemExit <EOL> finally : <EOL> log . debug ( "<STR_LIT>" ) <EOL> def collect ( self , delay = None ) : <EOL> if delay : time . sleep ( delay ) <EOL> pdu_list = list ( ) <EOL> max_data = None <EOL> with self . lock : <EOL> active_sap_list = [ sap for sap in self . sap if sap is not None ] <EOL> for sap in active_sap_list : <EOL> pdu = sap . dequeue ( max_data if max_data else <NUM_LIT> ) <EOL> if pdu is not None : <EOL> if self . cfg [ '<STR_LIT>' ] == False : <EOL> return pdu <EOL> pdu_list . append ( pdu ) <EOL> if max_data is None : <EOL> max_data = self . cfg [ "<STR_LIT>" ] + <NUM_LIT:2> <EOL> max_data -= len ( pdu ) <EOL> if max_data < bool ( len ( pdu_list ) == <NUM_LIT:1> ) * <NUM_LIT:2> + <NUM_LIT:2> + <NUM_LIT:2> : <EOL> break <EOL> else : max_data = self . cfg [ "<STR_LIT>" ] + <NUM_LIT:2> <EOL> for sap in active_sap_list : <EOL> if sap . mode == DATA_LINK_CONNECTION : <EOL> pdu = sap . sendack ( max_data ) <EOL> if not pdu is None : <EOL> if self . cfg [ '<STR_LIT>' ] == False : <EOL> return pdu <EOL> pdu_list . append ( pdu ) <EOL> max_data -= len ( pdu ) <EOL> if max_data < bool ( len ( pdu_list ) == <NUM_LIT:1> ) * <NUM_LIT:2> + <NUM_LIT:2> + <NUM_LIT:3> : <EOL> break <EOL> if len ( pdu_list ) > <NUM_LIT:1> : <EOL> return AggregatedFrame ( aggregate = pdu_list ) <EOL> if len ( pdu_list ) == <NUM_LIT:1> : <EOL> return pdu_list [ <NUM_LIT:0> ] <EOL> return None <EOL> def dispatch ( self , pdu ) : <EOL> if isinstance ( pdu , Symmetry ) : <EOL> return <EOL> if isinstance ( pdu , AggregatedFrame ) : <EOL> if pdu . dsap == <NUM_LIT:0> and pdu . ssap == <NUM_LIT:0> : <EOL> [ log . debug ( "<STR_LIT:U+0020>" + str ( p ) ) for p in pdu ] <EOL> [ self . dispatch ( p ) for p in pdu ] <EOL> return <EOL> if isinstance ( pdu , Connect ) and pdu . dsap == <NUM_LIT:1> : <EOL> addr = self . snl . get ( pdu . sn ) <EOL> if not addr or self . sap [ addr ] is None : <EOL> log . debug ( "<STR_LIT>" . format ( pdu . sn ) ) <EOL> pdu = DisconnectedMode ( pdu . ssap , <NUM_LIT:1> , reason = <NUM_LIT:2> ) <EOL> self . sap [ <NUM_LIT:1> ] . dmpdu . append ( pdu ) <EOL> return <EOL> pdu = Connect ( dsap = addr , ssap = pdu . ssap , rw = pdu . rw , miu = pdu . miu ) <EOL> with self . lock : <EOL> sap = self . sap [ pdu . dsap ] <EOL> if sap : <EOL> sap . enqueue ( pdu ) <EOL> return <EOL> log . debug ( "<STR_LIT>" . format ( str ( pdu ) ) ) <EOL> return <EOL> def resolve ( self , name ) : <EOL> return self . sap [ <NUM_LIT:1> ] . resolve ( name ) <EOL> def socket ( self , socket_type ) : <EOL> if socket_type == RAW_ACCESS_POINT : <EOL> return RawAccessPoint ( recv_miu = self . cfg [ "<STR_LIT>" ] ) <EOL> if socket_type == LOGICAL_DATA_LINK : <EOL> return LogicalDataLink ( recv_miu = self . cfg [ "<STR_LIT>" ] ) <EOL> if socket_type == DATA_LINK_CONNECTION : <EOL> return DataLinkConnection ( recv_miu = <NUM_LIT> , recv_win = <NUM_LIT:1> ) <EOL> def setsockopt ( self , socket , option , value ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if option == SO_RCVMIU : <EOL> value = min ( value , self . cfg [ '<STR_LIT>' ] ) <EOL> socket . setsockopt ( option , value ) <EOL> return socket . getsockopt ( option ) <EOL> def getsockopt ( self , socket , option ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if isinstance ( socket , LogicalDataLink ) : <EOL> socket . send_miu = self . cfg [ '<STR_LIT>' ] <EOL> if isinstance ( socket , RawAccessPoint ) : <EOL> socket . send_miu = self . cfg [ '<STR_LIT>' ] <EOL> return socket . getsockopt ( option ) <EOL> def bind ( self , socket , addr_or_name = None ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not socket . addr is None : <EOL> raise Error ( errno . EINVAL ) <EOL> if addr_or_name is None : <EOL> self . _bind_by_none ( socket ) <EOL> elif type ( addr_or_name ) is IntType : <EOL> self . _bind_by_addr ( socket , addr_or_name ) <EOL> elif type ( addr_or_name ) is StringType : <EOL> self . _bind_by_name ( socket , addr_or_name ) <EOL> else : raise Error ( errno . EFAULT ) <EOL> def _bind_by_none ( self , socket ) : <EOL> with self . lock : <EOL> try : addr = <NUM_LIT:32> + self . sap [ <NUM_LIT:32> : <NUM_LIT:64> ] . index ( None ) <EOL> except ValueError : raise Error ( errno . EAGAIN ) <EOL> else : <EOL> socket . bind ( addr ) <EOL> self . sap [ addr ] = ServiceAccessPoint ( addr , self ) <EOL> self . sap [ addr ] . insert_socket ( socket ) <EOL> def _bind_by_addr ( self , socket , addr ) : <EOL> with self . lock : <EOL> if addr in range ( <NUM_LIT:32> , <NUM_LIT:64> ) : <EOL> if self . sap [ addr ] is None : <EOL> socket . bind ( addr ) <EOL> self . sap [ addr ] = ServiceAccessPoint ( addr , self ) <EOL> self . sap [ addr ] . insert_socket ( socket ) <EOL> else : raise Error ( errno . EADDRINUSE ) <EOL> else : raise Error ( errno . EACCES ) <EOL> def _bind_by_name ( self , socket , name ) : <EOL> if not ( name . startswith ( "<STR_LIT>" ) or <EOL> name . startswith ( "<STR_LIT>" ) or <EOL> name == "<STR_LIT>" ) : <EOL> raise Error ( errno . EFAULT ) <EOL> with self . lock : <EOL> if self . snl . get ( name ) != None : <EOL> raise Error ( errno . EADDRINUSE ) <EOL> addr = wks_map . get ( name ) <EOL> if addr is None : <EOL> try : addr = <NUM_LIT:16> + self . sap [ <NUM_LIT:16> : <NUM_LIT:32> ] . index ( None ) <EOL> except ValueError : raise Error ( errno . EADDRNOTAVAIL ) <EOL> socket . bind ( addr ) <EOL> self . sap [ addr ] = ServiceAccessPoint ( addr , self ) <EOL> self . sap [ addr ] . insert_socket ( socket ) <EOL> self . snl [ name ] = addr <EOL> def connect ( self , socket , dest ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not socket . is_bound : <EOL> self . bind ( socket ) <EOL> socket . connect ( dest ) <EOL> log . debug ( "<STR_LIT>" . format ( socket . addr , socket . peer ) ) <EOL> def listen ( self , socket , backlog ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not isinstance ( socket , DataLinkConnection ) : <EOL> raise Error ( errno . EOPNOTSUPP ) <EOL> if not type ( backlog ) == IntType : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if backlog < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> backlog = min ( backlog , <NUM_LIT:16> ) <EOL> if not socket . is_bound : <EOL> self . bind ( socket ) <EOL> socket . listen ( backlog ) <EOL> def accept ( self , socket ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not isinstance ( socket , DataLinkConnection ) : <EOL> raise Error ( errno . EOPNOTSUPP ) <EOL> while True : <EOL> client = socket . accept ( ) <EOL> if not client . is_bound : <EOL> self . bind ( client ) <EOL> if self . sap [ client . addr ] . insert_socket ( client ) : <EOL> log . debug ( "<STR_LIT>" <EOL> . format ( client . addr , client . peer ) ) <EOL> return client <EOL> else : <EOL> pdu = DisconnectedMode ( client . peer , socket . addr , reason = <NUM_LIT> ) <EOL> super ( DataLinkConnection , socket ) . send ( pdu ) <EOL> def send ( self , socket , message ) : <EOL> return self . sendto ( socket , message , socket . peer ) <EOL> def sendto ( self , socket , message , dest ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if isinstance ( socket , RawAccessPoint ) : <EOL> if not isinstance ( message , ProtocolDataUnit ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not socket . is_bound : <EOL> self . bind ( socket ) <EOL> socket . send_miu = self . cfg [ '<STR_LIT>' ] <EOL> return socket . send ( message ) <EOL> if not type ( message ) == StringType : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if isinstance ( socket , LogicalDataLink ) : <EOL> if dest is None : <EOL> raise Error ( errno . EDESTADDRREQ ) <EOL> if not socket . is_bound : <EOL> self . bind ( socket ) <EOL> socket . send_miu = self . cfg [ '<STR_LIT>' ] <EOL> return socket . sendto ( message , dest ) <EOL> if isinstance ( socket , DataLinkConnection ) : <EOL> return socket . send ( message ) <EOL> def recv ( self , socket ) : <EOL> message , sender = self . recvfrom ( socket ) <EOL> return message <EOL> def recvfrom ( self , socket ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not ( socket . addr and self . sap [ socket . addr ] ) : <EOL> raise Error ( errno . EBADF ) <EOL> if isinstance ( socket , RawAccessPoint ) : <EOL> return ( socket . recv ( ) , None ) <EOL> if isinstance ( socket , LogicalDataLink ) : <EOL> return socket . recvfrom ( ) <EOL> if isinstance ( socket , DataLinkConnection ) : <EOL> return ( socket . recv ( ) , socket . peer ) <EOL> def poll ( self , socket , event , timeout = None ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if not ( socket . addr and self . sap [ socket . addr ] ) : <EOL> raise Error ( errno . EBADF ) <EOL> return socket . poll ( event , timeout ) <EOL> def close ( self , socket ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> if socket . is_bound : <EOL> self . sap [ socket . addr ] . remove_socket ( socket ) <EOL> else : socket . close ( ) <EOL> def getsockname ( self , socket ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> return socket . addr <EOL> def getpeername ( self , socket ) : <EOL> if not isinstance ( socket , TransmissionControlObject ) : <EOL> raise Error ( errno . ENOTSOCK ) <EOL> return socket . peer </s>
