<s> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> import collections <EOL> import threading <EOL> import time <EOL> import errno <EOL> from types import * <EOL> from pdu import * <EOL> from err import * <EOL> from opt import * <EOL> class TransmissionControlObject ( object ) : <EOL> class State ( object ) : <EOL> def __init__ ( self ) : <EOL> self . names = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . value = self . names . index ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . names [ self . value ] <EOL> def __getattr__ ( self , name ) : <EOL> return self . value == self . names . index ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if not name in ( "<STR_LIT>" , "<STR_LIT:value>" ) : <EOL> value , name = self . names . index ( name ) , "<STR_LIT:value>" <EOL> object . __setattr__ ( self , name , value ) <EOL> class Mode ( object ) : <EOL> def __init__ ( self ) : <EOL> self . names = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . value = dict ( [ ( name , False ) for name in self . names ] ) <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> def __getattr__ ( self , name ) : <EOL> return self . value [ name ] <EOL> def __init__ ( self , send_miu , recv_miu ) : <EOL> self . lock = threading . RLock ( ) <EOL> self . mode = TransmissionControlObject . Mode ( ) <EOL> self . state = TransmissionControlObject . State ( ) <EOL> self . send_queue = collections . deque ( ) <EOL> self . recv_queue = collections . deque ( ) <EOL> self . send_ready = threading . Condition ( self . lock ) <EOL> self . recv_ready = threading . Condition ( self . lock ) <EOL> self . recv_miu = recv_miu <EOL> self . send_miu = send_miu <EOL> self . recv_buf = <NUM_LIT:1> <EOL> self . send_buf = <NUM_LIT:1> <EOL> self . addr = None <EOL> self . peer = None <EOL> @ property <EOL> def is_bound ( self ) : <EOL> return self . addr != None <EOL> def setsockopt ( self , option , value ) : <EOL> if option == SO_SNDBUF : <EOL> raise NotImplemented <EOL> if option == SO_RCVBUF : <EOL> with self . lock : self . recv_buf = int ( value ) <EOL> def getsockopt ( self , option ) : <EOL> if option == SO_SNDMIU : <EOL> return self . send_miu <EOL> if option == SO_RCVMIU : <EOL> return self . recv_miu <EOL> if option == SO_SNDBUF : <EOL> return self . send_buf <EOL> if option == SO_RCVBUF : <EOL> return self . recv_buf <EOL> def bind ( self , addr ) : <EOL> if self . addr and addr and self . addr != addr : <EOL> log . warn ( "<STR_LIT>" . format ( self . addr , addr ) ) <EOL> self . addr = addr <EOL> def poll ( self , event , timeout ) : <EOL> if event == "<STR_LIT>" : <EOL> with self . recv_ready : <EOL> if len ( self . recv_queue ) == <NUM_LIT:0> : <EOL> self . recv_ready . wait ( timeout ) <EOL> if len ( self . recv_queue ) > <NUM_LIT:0> : <EOL> return self . recv_queue [ <NUM_LIT:0> ] . type <EOL> return None <EOL> if event == "<STR_LIT>" : <EOL> with self . send_ready : <EOL> if len ( self . send_queue ) >= self . send_buf : <EOL> self . send_ready . wait ( timeout ) <EOL> return len ( self . send_queue ) < self . send_buf <EOL> def send ( self , pdu ) : <EOL> with self . send_ready : <EOL> self . send_queue . append ( pdu ) <EOL> self . send_ready . wait ( ) <EOL> def recv ( self ) : <EOL> with self . recv_ready : <EOL> try : return self . recv_queue . popleft ( ) <EOL> except IndexError : self . recv_ready . wait ( ) <EOL> return self . recv_queue . popleft ( ) <EOL> def close ( self ) : <EOL> with self . lock : <EOL> self . send_queue . clear ( ) <EOL> self . recv_queue . clear ( ) <EOL> self . send_ready . notify_all ( ) <EOL> self . recv_ready . notify_all ( ) <EOL> self . state . SHUTDOWN = True <EOL> def enqueue ( self , pdu ) : <EOL> with self . lock : <EOL> if len ( self . recv_queue ) < self . recv_buf : <EOL> self . recv_queue . append ( pdu ) <EOL> self . recv_ready . notify ( ) <EOL> return True <EOL> else : log . warn ( "<STR_LIT>" ) <EOL> return False <EOL> def dequeue ( self , maxlen , notify = True ) : <EOL> with self . lock : <EOL> pdu = self . send_queue . popleft ( ) <EOL> if len ( pdu ) <= maxlen : <EOL> if notify == True : <EOL> self . send_ready . notify ( ) <EOL> return pdu <EOL> else : self . send_queue . appendleft ( pdu ) <EOL> class RawAccessPoint ( TransmissionControlObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , recv_miu ) : <EOL> super ( RawAccessPoint , self ) . __init__ ( <NUM_LIT> , recv_miu ) <EOL> self . state . ESTABLISHED = True <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . addr ) <EOL> def setsockopt ( self , option , value ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> super ( RawAccessPoint , self ) . setsockopt ( option , value ) <EOL> def getsockopt ( self , option ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> return super ( RawAccessPoint , self ) . getsockopt ( option ) <EOL> def poll ( self , event , timeout ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if not event in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise Error ( errno . EINVAL ) <EOL> return super ( RawAccessPoint , self ) . poll ( event , timeout ) is not None <EOL> def send ( self , pdu ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> log . debug ( "<STR_LIT>" . format ( str ( self ) , pdu ) ) <EOL> super ( RawAccessPoint , self ) . send ( pdu ) <EOL> return self . state . ESTABLISHED == True <EOL> def recv ( self ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> try : return super ( RawAccessPoint , self ) . recv ( ) <EOL> except IndexError : raise Error ( errno . EPIPE ) <EOL> def close ( self ) : <EOL> super ( RawAccessPoint , self ) . close ( ) <EOL> def enqueue ( self , pdu ) : <EOL> return super ( RawAccessPoint , self ) . enqueue ( pdu ) <EOL> def dequeue ( self , maxlen ) : <EOL> try : return super ( RawAccessPoint , self ) . dequeue ( <NUM_LIT:3> + <NUM_LIT> + <NUM_LIT> ) <EOL> except IndexError : return None <EOL> class LogicalDataLink ( TransmissionControlObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , recv_miu ) : <EOL> super ( LogicalDataLink , self ) . __init__ ( <NUM_LIT> , recv_miu ) <EOL> self . state . ESTABLISHED = True <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . addr , self . peer ) <EOL> def setsockopt ( self , option , value ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> super ( LogicalDataLink , self ) . setsockopt ( option , value ) <EOL> def getsockopt ( self , option ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> return super ( LogicalDataLink , self ) . getsockopt ( option ) <EOL> def connect ( self , dest ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> with self . lock : <EOL> self . peer = dest <EOL> return self . peer > <NUM_LIT:0> <EOL> def poll ( self , event , timeout ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if not event in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise Error ( errno . EINVAL ) <EOL> return super ( LogicalDataLink , self ) . poll ( event , timeout ) is not None <EOL> def sendto ( self , message , dest ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if self . peer and dest != self . peer : <EOL> raise Error ( errno . EDESTADDRREQ ) <EOL> if len ( message ) > self . send_miu : <EOL> raise Error ( errno . EMSGSIZE ) <EOL> pdu = UnnumberedInformation ( dest , self . addr , sdu = message ) <EOL> super ( LogicalDataLink , self ) . send ( pdu ) <EOL> return self . state . ESTABLISHED == True <EOL> def recvfrom ( self ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> try : pdu = super ( LogicalDataLink , self ) . recv ( ) <EOL> except IndexError : raise Error ( errno . EPIPE ) <EOL> return ( pdu . sdu , pdu . ssap ) if pdu else ( None , None ) <EOL> def close ( self ) : <EOL> super ( LogicalDataLink , self ) . close ( ) <EOL> def enqueue ( self , pdu ) : <EOL> if not isinstance ( pdu , UnnumberedInformation ) : <EOL> log . warn ( "<STR_LIT>" . format ( pdu . name ) ) <EOL> return False <EOL> if len ( pdu . sdu ) > self . recv_miu : <EOL> log . warn ( "<STR_LIT>" ) <EOL> return False <EOL> return super ( LogicalDataLink , self ) . enqueue ( pdu ) <EOL> def dequeue ( self , maxlen ) : <EOL> try : return super ( LogicalDataLink , self ) . dequeue ( maxlen ) <EOL> except IndexError : return None <EOL> class DataLinkConnection ( TransmissionControlObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , recv_miu , recv_win ) : <EOL> super ( DataLinkConnection , self ) . __init__ ( <NUM_LIT> , recv_miu ) <EOL> self . state . CLOSED = True <EOL> self . acks_ready = threading . Condition ( self . lock ) <EOL> self . acks_recvd = <NUM_LIT:0> <EOL> self . recv_confs = <NUM_LIT:0> <EOL> self . send_token = threading . Condition ( self . lock ) <EOL> self . recv_buf = recv_win <EOL> self . recv_win = recv_win <EOL> self . recv_cnt = <NUM_LIT:0> <EOL> self . recv_ack = <NUM_LIT:0> <EOL> self . send_win = None <EOL> self . send_cnt = <NUM_LIT:0> <EOL> self . send_ack = <NUM_LIT:0> <EOL> def __str__ ( self ) : <EOL> s = "<STR_LIT>" <EOL> s += "<STR_LIT>" <EOL> s += "<STR_LIT>" <EOL> return s . format ( dlc = self ) <EOL> def log ( self , string ) : <EOL> log . debug ( "<STR_LIT>" <EOL> . format ( dlc = self , s = string ) ) <EOL> def err ( self , string ) : <EOL> log . error ( "<STR_LIT>" . format ( dlc = self , s = string ) ) <EOL> def setsockopt ( self , option , value ) : <EOL> with self . lock : <EOL> if option == SO_RCVMIU and self . state . CLOSED : <EOL> self . recv_miu = min ( value , <NUM_LIT> ) <EOL> return <EOL> if option == SO_RCVBUF and self . state . CLOSED : <EOL> self . recv_win = min ( value , <NUM_LIT:15> ) <EOL> self . recv_buf = self . recv_win <EOL> return <EOL> if option == SO_RCVBSY : <EOL> self . mode . RECV_BUSY = bool ( value ) <EOL> return <EOL> super ( DataLinkConnection , self ) . setsockopt ( option , value ) <EOL> def getsockopt ( self , option ) : <EOL> if option == SO_RCVBUF : <EOL> return self . recv_win <EOL> if option == SO_SNDBSY : <EOL> return self . mode . SEND_BUSY <EOL> if option == SO_RCVBSY : <EOL> return self . mode . RECV_BUSY <EOL> return super ( DataLinkConnection , self ) . getsockopt ( option ) <EOL> def listen ( self , backlog ) : <EOL> with self . lock : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if not self . state . CLOSED : <EOL> self . err ( "<STR_LIT>" . format ( self . state ) ) <EOL> raise RuntimeError <EOL> self . state . LISTEN = True <EOL> self . recv_buf = backlog <EOL> def accept ( self ) : <EOL> with self . lock : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if not self . state . LISTEN : <EOL> self . err ( "<STR_LIT>" . format ( self . state ) ) <EOL> raise Error ( errno . EINVAL ) <EOL> self . recv_buf += <NUM_LIT:1> <EOL> try : pdu = super ( DataLinkConnection , self ) . recv ( ) <EOL> except IndexError : raise Error ( errno . EPIPE ) <EOL> self . recv_buf -= <NUM_LIT:1> <EOL> if isinstance ( pdu , Connect ) : <EOL> dlc = DataLinkConnection ( self . recv_miu , self . recv_win ) <EOL> dlc . addr = self . addr <EOL> dlc . peer = pdu . ssap <EOL> dlc . send_miu = pdu . miu <EOL> dlc . send_win = pdu . rw <EOL> pdu = ConnectionComplete ( dlc . peer , dlc . addr ) <EOL> pdu . miu , pdu . rw = dlc . recv_miu , dlc . recv_win <EOL> log . info ( "<STR_LIT>" % dlc . peer ) <EOL> dlc . state . ESTABLISHED = True <EOL> self . send_queue . append ( pdu ) <EOL> return dlc <EOL> raise RuntimeError ( "<STR_LIT>" + pdu . name ) <EOL> def connect ( self , dest ) : <EOL> with self . lock : <EOL> if not self . state . CLOSED : <EOL> self . err ( "<STR_LIT>" . format ( self . state ) ) <EOL> if self . state . ESTABLISHED : <EOL> raise Error ( errno . EISCONN ) <EOL> if self . state . CONNECT : <EOL> raise Error ( errno . EALREADY ) <EOL> raise Error ( errno . EPIPE ) <EOL> if type ( dest ) is StringType : <EOL> pdu = Connect ( <NUM_LIT:1> , self . addr , self . recv_miu , self . recv_win , dest ) <EOL> elif type ( dest ) is IntType : <EOL> pdu = Connect ( dest , self . addr , self . recv_miu , self . recv_win ) <EOL> else : raise TypeError ( "<STR_LIT>" ) <EOL> self . state . CONNECT = True <EOL> self . send_queue . append ( pdu ) <EOL> try : pdu = super ( DataLinkConnection , self ) . recv ( ) <EOL> except IndexError : raise Error ( errno . EPIPE ) <EOL> if isinstance ( pdu , DisconnectedMode ) : <EOL> self . log ( "<STR_LIT>" . format ( pdu . reason ) ) <EOL> self . state . CLOSED = True <EOL> raise ConnectRefused ( pdu . reason ) <EOL> if isinstance ( pdu , ConnectionComplete ) : <EOL> self . peer = pdu . ssap <EOL> self . recv_buf = self . recv_win <EOL> self . send_miu = pdu . miu <EOL> self . send_win = pdu . rw <EOL> self . state . ESTABLISHED = True <EOL> return <EOL> raise RuntimeError ( "<STR_LIT>" + pdu . name ) <EOL> @ property <EOL> def send_window_slots ( self ) : <EOL> return ( self . send_win - self . send_cnt + self . send_ack ) % <NUM_LIT:16> <EOL> @ property <EOL> def recv_window_slots ( self ) : <EOL> return ( self . recv_win - self . recv_cnt + self . recv_ack ) % <NUM_LIT:16> <EOL> def send ( self , message ) : <EOL> with self . send_token : <EOL> if not self . state . ESTABLISHED : <EOL> self . err ( "<STR_LIT>" . format ( self . state ) ) <EOL> if self . state . CLOSE_WAIT : <EOL> raise Error ( errno . EPIPE ) <EOL> raise Error ( errno . ENOTCONN ) <EOL> if len ( message ) > self . send_miu : <EOL> raise Error ( errno . EMSGSIZE ) <EOL> while self . send_window_slots == <NUM_LIT:0> and self . state . ESTABLISHED : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . send_token . wait ( ) <EOL> self . log ( "<STR_LIT>" . format ( str ( self ) ) ) <EOL> if self . state . ESTABLISHED : <EOL> pdu = Information ( self . peer , self . addr , sdu = message ) <EOL> pdu . ns = self . send_cnt <EOL> self . send_cnt = ( self . send_cnt + <NUM_LIT:1> ) % <NUM_LIT:16> <EOL> super ( DataLinkConnection , self ) . send ( pdu ) <EOL> return self . state . ESTABLISHED == True <EOL> def recv ( self ) : <EOL> with self . lock : <EOL> if not ( self . state . ESTABLISHED or self . state . CLOSE_WAIT ) : <EOL> self . err ( "<STR_LIT>" . format ( self . state ) ) <EOL> raise Error ( errno . ENOTCONN ) <EOL> try : pdu = super ( DataLinkConnection , self ) . recv ( ) <EOL> except IndexError : return None <EOL> if isinstance ( pdu , Information ) : <EOL> self . recv_confs += <NUM_LIT:1> <EOL> if self . recv_confs > self . recv_win : <EOL> self . err ( "<STR_LIT>" <EOL> . format ( self . recv_confs , self . recv_win ) ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return pdu . sdu <EOL> if isinstance ( pdu , Disconnect ) : <EOL> self . close ( ) <EOL> return None <EOL> raise RuntimeError ( "<STR_LIT>" + pdu . name ) <EOL> def poll ( self , event , timeout ) : <EOL> if self . state . SHUTDOWN : <EOL> raise Error ( errno . EBADF ) <EOL> if not event in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> raise Error ( errno . EINVAL ) <EOL> if event == "<STR_LIT>" : <EOL> if self . state . ESTABLISHED or self . state . CLOSE_WAIT : <EOL> ptype = super ( DataLinkConnection , self ) . poll ( event , timeout ) <EOL> if self . state . ESTABLISHED or self . state . CLOSE_WAIT : <EOL> return ptype == ProtocolDataUnit . Information <EOL> else : return False <EOL> if event == "<STR_LIT>" : <EOL> if self . state . ESTABLISHED : <EOL> if super ( DataLinkConnection , self ) . poll ( event , timeout ) : <EOL> return self . state . ESTABLISHED <EOL> if event == "<STR_LIT>" : <EOL> with self . acks_ready : <EOL> while not self . acks_recvd > <NUM_LIT:0> : <EOL> self . acks_ready . wait ( timeout ) <EOL> if self . acks_recvd > <NUM_LIT:0> : <EOL> self . acks_recvd = self . acks_recvd - <NUM_LIT:1> <EOL> return True <EOL> return False <EOL> def close ( self ) : <EOL> with self . lock : <EOL> self . log ( "<STR_LIT>" ) <EOL> if self . state . ESTABLISHED and self . is_bound : <EOL> self . state . DISCONNECT = True <EOL> self . send_token . notify_all ( ) <EOL> self . acks_ready . notify_all ( ) <EOL> pdu = Disconnect ( self . peer , self . addr ) <EOL> self . send_queue . append ( pdu ) <EOL> try : super ( DataLinkConnection , self ) . recv ( ) <EOL> except IndexError : pass <EOL> super ( DataLinkConnection , self ) . close ( ) <EOL> self . acks_ready . notify_all ( ) <EOL> self . send_token . notify_all ( ) <EOL> def enqueue ( self , pdu ) : <EOL> self . log ( "<STR_LIT>" . format ( pdu = pdu ) ) <EOL> if not pdu . type in connection_mode_pdu_types : <EOL> self . err ( "<STR_LIT>" ) <EOL> pdu = FrameReject . from_pdu ( pdu , flags = "<STR_LIT>" , dlc = self ) <EOL> self . close ( ) ; self . send_queue . append ( pdu ) <EOL> return <EOL> if self . state . CLOSED : <EOL> pdu = DisconnectedMode ( pdu . ssap , pdu . dsap , reason = <NUM_LIT:1> ) <EOL> self . send_queue . append ( pdu ) <EOL> if self . state . LISTEN : <EOL> if isinstance ( pdu , Connect ) : <EOL> if super ( DataLinkConnection , self ) . enqueue ( pdu ) == False : <EOL> log . warn ( "<STR_LIT>" ) <EOL> pdu = DisconnectedMode ( pdu . ssap , pdu . dsap , reason = <NUM_LIT> ) <EOL> self . send_queue . append ( pdu ) <EOL> return False <EOL> return True <EOL> if self . state . CONNECT : <EOL> if ( isinstance ( pdu , ConnectionComplete ) or <EOL> isinstance ( pdu , DisconnectedMode ) ) : <EOL> with self . lock : <EOL> self . recv_queue . append ( pdu ) <EOL> self . recv_ready . notify ( ) <EOL> if self . state . DISCONNECT : <EOL> if isinstance ( pdu , DisconnectedMode ) : <EOL> with self . lock : <EOL> self . recv_queue . append ( pdu ) <EOL> self . recv_ready . notify ( ) <EOL> if self . state . ESTABLISHED : <EOL> return self . _enqueue_state_established ( pdu ) <EOL> def _enqueue_state_established ( self , pdu ) : <EOL> if isinstance ( pdu , Information ) : <EOL> if len ( pdu . sdu ) > self . recv_miu : <EOL> self . log ( "<STR_LIT>" + str ( self ) ) <EOL> pdu = FrameReject . from_pdu ( pdu , flags = "<STR_LIT:I>" , dlc = self ) <EOL> elif pdu . ns != self . recv_cnt : <EOL> self . log ( "<STR_LIT>" + str ( self ) ) <EOL> pdu = FrameReject . from_pdu ( pdu , flags = "<STR_LIT:S>" , dlc = self ) <EOL> if isinstance ( pdu , FrameReject ) : <EOL> self . send_queue . clear ( ) <EOL> self . send_queue . append ( pdu ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> return <EOL> if isinstance ( pdu , FrameReject ) : <EOL> with self . lock : <EOL> self . state . SHUTDOWN = True <EOL> self . close ( ) <EOL> return <EOL> if isinstance ( pdu , Disconnect ) : <EOL> with self . lock : <EOL> self . state . CLOSE_WAIT = True <EOL> pdu = DisconnectedMode ( self . peer , self . addr , reason = <NUM_LIT:0> ) <EOL> self . send_queue . clear ( ) <EOL> self . send_queue . append ( pdu ) <EOL> return <EOL> if ( isinstance ( pdu , Information ) or <EOL> isinstance ( pdu , ReceiveReady ) or <EOL> isinstance ( pdu , ReceiveNotReady ) ) : <EOL> with self . lock : <EOL> acks = ( pdu . nr - self . send_ack ) % <NUM_LIT:16> <EOL> if acks : <EOL> self . acks_recvd += acks <EOL> self . acks_ready . notify_all ( ) <EOL> self . send_token . notify ( ) <EOL> self . send_ack = pdu . nr <EOL> if isinstance ( pdu , ReceiveNotReady ) : <EOL> self . mode . SEND_BUSY = True <EOL> if isinstance ( pdu , ReceiveReady ) : <EOL> self . mode . SEND_BUSY = False <EOL> if isinstance ( pdu , Information ) : <EOL> with self . lock : <EOL> self . recv_cnt = ( self . recv_cnt + <NUM_LIT:1> ) % <NUM_LIT:16> <EOL> super ( DataLinkConnection , self ) . enqueue ( pdu ) <EOL> def dequeue ( self , maxlen ) : <EOL> self . super = super ( DataLinkConnection , self ) <EOL> with self . lock : <EOL> if self . state . ESTABLISHED : <EOL> if self . mode . RECV_BUSY_SENT != self . mode . RECV_BUSY : <EOL> self . mode . RECV_BUSY_SENT = self . mode . RECV_BUSY <EOL> Ack = ( ReceiveReady , ReceiveNotReady ) [ self . mode . RECV_BUSY ] <EOL> return Ack ( self . peer , self . addr , self . recv_ack ) <EOL> try : pdu = self . super . dequeue ( maxlen , notify = False ) <EOL> except IndexError : pdu = None <EOL> if pdu : self . log ( "<STR_LIT>" . format ( pdu . name ) ) <EOL> if isinstance ( pdu , FrameReject ) : <EOL> self . state . SHUTDOWN = True <EOL> self . close ( ) <EOL> elif isinstance ( pdu , Information ) and self . state . ESTABLISHED : <EOL> if ( self . recv_confs and self . recv_cnt != self . recv_ack ) : <EOL> self . log ( "<STR_LIT>" + str ( self ) ) <EOL> self . recv_ack = ( self . recv_ack + self . recv_confs ) % <NUM_LIT:16> <EOL> self . recv_confs = <NUM_LIT:0> <EOL> pdu . nr = self . recv_ack <EOL> self . send_ready . notify ( ) <EOL> elif isinstance ( pdu , DisconnectedMode ) and self . state . CLOSE_WAIT : <EOL> dm = Disconnect ( dsap = self . peer , ssap = self . addr ) <EOL> self . recv_queue . append ( dm ) <EOL> self . recv_ready . notify ( ) <EOL> self . send_token . notify_all ( ) <EOL> elif pdu is None and self . state . ESTABLISHED : <EOL> if ( self . recv_confs and maxlen >= <NUM_LIT:3> and <EOL> self . recv_window_slots == <NUM_LIT:0> ) : <EOL> self . log ( "<STR_LIT>" + str ( self ) ) <EOL> self . recv_ack = ( self . recv_ack + self . recv_confs ) % <NUM_LIT:16> <EOL> self . recv_confs = <NUM_LIT:0> <EOL> Ack = ( ReceiveReady , ReceiveNotReady ) [ self . mode . RECV_BUSY ] <EOL> pdu = Ack ( self . peer , self . addr , self . recv_ack ) <EOL> return pdu <EOL> def sendack ( self , maxlen ) : <EOL> if self . state . ESTABLISHED : <EOL> with self . lock : <EOL> if ( self . recv_confs and maxlen >= <NUM_LIT:3> and <EOL> self . recv_cnt != self . recv_ack ) : <EOL> self . log ( "<STR_LIT>" + str ( self ) ) <EOL> self . recv_ack = ( self . recv_ack + self . recv_confs ) % <NUM_LIT:16> <EOL> self . recv_confs = <NUM_LIT:0> <EOL> Ack = ( ReceiveReady , ReceiveNotReady ) [ self . mode . RECV_BUSY ] <EOL> pdu = Ack ( self . peer , self . addr , self . recv_ack ) <EOL> return pdu </s>
