<s> """<STR_LIT>""" <EOL> import sys <EOL> from bacpypes . debugging import bacpypes_debugging , ModuleLogger <EOL> from bacpypes . udp import UDPDirector <EOL> from bacpypes . comm import Client , Server , bind <EOL> from bacpypes . pdu import Address , PDU <EOL> from bacpypes . core import run , stop <EOL> from bacpypes . consolelogging import ArgumentParser <EOL> from bacpypes . console import ConsoleClient <EOL> _debug = <NUM_LIT:0> <EOL> _log = ModuleLogger ( globals ( ) ) <EOL> local_unicast_tuple = None <EOL> local_broadcast_tuple = None <EOL> @ bacpypes_debugging <EOL> class MiddleMan ( Client , Server ) : <EOL> def indication ( self , pdu ) : <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , pdu ) <EOL> if not pdu . pduData : <EOL> stop ( ) <EOL> return <EOL> line = pdu . pduData . decode ( '<STR_LIT>' ) [ : - <NUM_LIT:1> ] <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , line ) <EOL> line_parts = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , line_parts ) <EOL> if len ( line_parts ) != <NUM_LIT:2> : <EOL> sys . stderr . write ( "<STR_LIT>" % ( line , ) ) <EOL> return <EOL> addr , msg = line_parts <EOL> if addr == "<STR_LIT:*>" : <EOL> dest = local_broadcast_tuple <EOL> elif '<STR_LIT::>' in addr : <EOL> addr , port = addr . split ( '<STR_LIT::>' ) <EOL> if addr == "<STR_LIT:*>" : <EOL> dest = ( local_broadcast_tuple [ <NUM_LIT:0> ] , int ( port ) ) <EOL> else : <EOL> dest = ( addr , int ( port ) ) <EOL> else : <EOL> dest = ( addr , local_unicast_tuple [ <NUM_LIT:1> ] ) <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , dest ) <EOL> try : <EOL> self . request ( PDU ( msg . encode ( '<STR_LIT>' ) , destination = dest ) ) <EOL> except Exception as err : <EOL> sys . stderr . write ( "<STR_LIT>" % ( err , ) ) <EOL> return <EOL> def confirmation ( self , pdu ) : <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , pdu ) <EOL> line = pdu . pduData . decode ( '<STR_LIT>' ) <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , line ) <EOL> if pdu . pduSource == local_unicast_tuple : <EOL> sys . stdout . write ( "<STR_LIT>" % ( line , ) ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT>" % ( <EOL> line , pdu . pduSource , <EOL> ) ) <EOL> @ bacpypes_debugging <EOL> class BroadcastReceiver ( Client ) : <EOL> def confirmation ( self , pdu ) : <EOL> if _debug : BroadcastReceiver . _debug ( '<STR_LIT>' , pdu ) <EOL> line = pdu . pduData . decode ( '<STR_LIT>' ) <EOL> if _debug : MiddleMan . _debug ( '<STR_LIT>' , line ) <EOL> if pdu . pduSource == local_unicast_tuple : <EOL> sys . stdout . write ( "<STR_LIT>" % ( line , ) ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT>" % ( <EOL> line , pdu . pduSource , <EOL> ) ) <EOL> try : <EOL> parser = ArgumentParser ( description = __doc__ ) <EOL> parser . add_argument ( "<STR_LIT:address>" , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> parser . add_argument ( "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , <EOL> default = False , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> args = parser . parse_args ( ) <EOL> if _debug : _log . debug ( "<STR_LIT>" ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , args ) <EOL> if args . address == "<STR_LIT>" : <EOL> local_unicast_tuple = ( '<STR_LIT>' , <NUM_LIT> ) <EOL> local_broadcast_tuple = ( '<STR_LIT>' , <NUM_LIT> ) <EOL> elif args . address . startswith ( "<STR_LIT>" ) : <EOL> port = int ( args . address [ <NUM_LIT:4> : ] ) <EOL> local_unicast_tuple = ( '<STR_LIT>' , port ) <EOL> local_broadcast_tuple = ( '<STR_LIT>' , port ) <EOL> else : <EOL> address = Address ( args . address ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , address ) <EOL> local_unicast_tuple = address . addrTuple <EOL> local_broadcast_tuple = address . addrBroadcastTuple <EOL> if _debug : _log . debug ( "<STR_LIT>" , local_unicast_tuple ) <EOL> if _debug : _log . debug ( "<STR_LIT>" , local_broadcast_tuple ) <EOL> console = ConsoleClient ( ) <EOL> middle_man = MiddleMan ( ) <EOL> unicast_director = UDPDirector ( local_unicast_tuple ) <EOL> bind ( console , middle_man , unicast_director ) <EOL> if args . noBroadcast : <EOL> _log . debug ( "<STR_LIT>" ) <EOL> elif local_unicast_tuple == local_broadcast_tuple : <EOL> _log . debug ( "<STR_LIT>" ) <EOL> elif local_broadcast_tuple [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> _log . debug ( "<STR_LIT>" ) <EOL> else : <EOL> broadcast_receiver = BroadcastReceiver ( ) <EOL> broadcast_director = UDPDirector ( local_broadcast_tuple , reuse = True ) <EOL> bind ( broadcast_receiver , broadcast_director ) <EOL> _log . debug ( "<STR_LIT>" ) <EOL> run ( ) <EOL> except Exception as e : <EOL> _log . exception ( "<STR_LIT>" , e ) <EOL> finally : <EOL> _log . debug ( "<STR_LIT>" ) </s>
