def initialize(self, args): self.name = args.name self.address = "{}_{}".format(self.federation, self.name) self.transport = Transport(self.address) self.logfile = args.logfile self.loglevel = args.loglevel self.logagent = args.logagent
def initialize(self, args): self.name = args.name self.address = "{}_{}".format(self.federation, self.name) self.transport = Transport(self.address) self.logfile = args.logfile self.loglevel = args.loglevel self.logagent = args.logagent self._keep_alive = True self._broadcasted = False
from __future__ import print_function from nluas.Transport import * # Makes this work with both py2 and py3 from six.moves import input import sys name, destination = sys.argv[1], sys.argv[2] t = Transport(name) t.subscribe(destination, lambda ntuple: print("Got", ntuple)) while True: t.send(destination, input())
def main(argv): #if six.PY3: # sys.stderr.write('bridge_client.py currently only supports python2') # sys.exit(1) parse_arguments(argv[1:]) setup_logging() # If the user presses ctrl-C, exit cleanly. signal.signal(signal.SIGINT, lambda s, f: client_quit()) # Create the bridge socket Global.bridgesocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Global.bridgesocket.connect((Global.args.host, Global.args.port)) # Create a pyre instance Global.pyre = Pyre() Global.pyre.start() # Create a poller object that tests if anything is available on # any of: 1) The local pyre channels, 2) The bridge socket, 3) stdin. poller = zmq.Poller() poller.register(Global.pyre.socket(), zmq.POLLIN) poller.register(Global.bridgesocket, zmq.POLLIN) poller.register(0, zmq.POLLIN) # stdin logging.warning('Starting bridge client to server at %s:%d' % (Global.args.host, Global.args.port)) while True: items = dict(poller.poll()) logging.debug('Got items =%s' % (items)) if 0 in items: # stdin # User typed 'quit'. Note: slightly different from # a quit message on the global channel in that this # doesn't cause remote to quit. message = input() if message == 'quit': client_quit() elif message == 'help': print( 'You can quit the bridge_client (but not the federation) by typing "quit".' ) else: print('Unrecognized command %s' % (message)) if Global.bridgesocket.fileno() in items: # Got a message from the remote. rec = server_recv() logging.debug('Got remote data %s' % (rec)) if rec[0] == 'JOIN': channel = rec[1] # If we don't already have a proxy object, create one. if channel not in Global.proxies: t = Transport.Transport(channel) Global.pyre.join(channel) Global.proxies[channel] = t Global.proxy_uuids[t._pyre.uuid()] = t logging.info('Creating bridge proxy %s' % (channel)) elif rec[0] == 'LEAVE': # Don't actually know how to handle this. pass elif rec[0] == 'SHOUT': # Use the proxy object to relay the message. name = rec[1] channel = rec[2] message = rec[3] if Global.localchannelcount.get(channel, 0) > 0: logging.debug('Bridge proxy shout %s %s %s' % (name, channel, message)) Global.proxies[name].send(channel, message) else: logging.warning('Unexpected msg %s from client.' % (rec)) if Global.pyre.socket() in items: # Got a message on Pyre. event = Global.pyre.recv() logging.debug('Got local pyre event %s' % (event)) eventtype = event[0].decode('utf-8') sid = uuid.UUID(bytes=event[1]) name = event[2].decode('utf-8') # Ignore pyre events from proxies if sid in Global.proxy_uuids: logging.debug('Ignoring proxied pyre event') continue if eventtype == 'JOIN': channel = event[3].decode('utf-8') if Global.localchannelcount.get(channel, 0) == 0: Global.pyre.join(channel) Global.localchannelcount[channel] = 0 server_send(['JOIN', channel]) logging.debug('Bridge client joining local channel %s' % (channel)) Global.localchannelcount[channel] += 1 elif eventtype == 'LEAVE': channel = event[3].decode('utf-8') Global.localchannelcount[channel] -= 1 if Global.localchannelcount[channel] == 0: Global.pyre.leave(channel) server_send(['LEAVE', channel]) logging.debug('Bridge client leaving channel %s' % (channel)) elif eventtype == 'SHOUT': channel = event[3].decode('utf-8') # Quit if federation QUIT message received. if event[4] == u'QUIT': logging.warning( 'Bridge client received a local QUIT message. Exiting.' ) client_quit() # Since the server communicates with json, we # need to un-json the message (which server_send # will re-json). There's probably a more elegant way # to do this. if six.PY3: decoded = event[4].decode('utf-8') server_send(['SHOUT', name, channel, decoded]) else: message = json.loads(event[4].decode('utf-8')) server_send(['SHOUT', name, channel, message])