def __init__(self, servers, event_plugins=None): self.channels = [] self._servers = servers if event_plugins is None: event_plugins = {} self.connection_manager = ConnectionManager( PluginEventManager( { Events.CONNECT: EventHook(self._on_connect), Events.RPL_WELCOME: EventHook(self._on_welcome, source=True, target=True, message=True), Events.PING: EventHook(self._on_ping, message=True), Events.PONG: EventHook(self._on_pong, message=True), Events.MODE: EventHook(self._on_mode, source=True, target=True, args=True), Events.UMODE: EventHook(self._on_umode, source=True, target=True, message=True), Events.JOIN: EventHook(self._on_join, source=True, message=True), Events.PART: EventHook(self._on_part, source=True, target=True, message=True), Events.QUIT: EventHook(self._on_quit, source=True, message=True), Events.RPL_NAMEREPLY: EventHook( self._on_name_reply, source=True, target=True, args=True, message=True ), Events.PRIVMSG: EventHook(self._on_privmsg, source=True, target=True, message=True), Events.PUBMSG: EventHook(self._on_pubmsg, source=True, target=True, message=True), Events.RPL_CHANNELMODEIS: EventHook(self._on_channel_mode, source=True, target=True, args=True), Events.ERR_NICKNAMEINUSE: EventHook(self.on_nickname_in_use), Events.ERROR: EventHook(self._on_error, message=True), }, event_plugins, ) ) self.process_lock = Lock() self.ping_timers = {}
def connect_objects(self, what, what_id, to, to_id): # # what and to are table names of needed models # what_wrapper = None to_wrapper = None for wrapper in ModelWrapper.__subclasses__(): if wrapper.model_class.__tablename__ == what: what_model = wrapper.model_class.query.filter_by(id=what_id).all()[0] what_wrapper = wrapper(what_model) if wrapper.model_class.__tablename__ == to: to_model = wrapper.model_class.query.filter_by(id=to_id).all()[0] to_wrapper = wrapper(to_model) ConnectionManager.connect(what_wrapper, to_wrapper)
def __init__(self): # TODO: consider saving and loading the connections list to a file # to preserve the list between sessions self._con_mgr = ConnectionManager() # The data on the common clipboard. self._clipboard_data = None # Type of data on the clipboard (eg. text, bitmap, etc.) # This should be one of the supported types in info.py self._data_type = None # None will mean that this client is owner, otherwise it should be a # Connection object. self._data_owner = None # TODO add command line switch to change port, which would be passed in # here self._network = Network(con_callback=self._new_connection_request, dis_callback=self._disconnect_request) self._network.start()
class Client(object): """The base IRC Client, which wraps a ConnectionManager and provides an interface to low level connection functions. It can be extended to make a full IRC client or an IRC bot.""" def __init__(self, servers, event_plugins=None): self.channels = [] self._servers = servers self.pinged = False if event_plugins is None: event_plugins = {} self.connection_manager = ConnectionManager(PluginEventManager({ Events.CONNECT: EventHook(self._on_connect), Events.RPL_WELCOME: EventHook(self._on_welcome, source=True, target=True, message=True), Events.PING: EventHook(self._on_ping, message=True), Events.MODE: EventHook(self._on_mode, source=True, target=True, args=True), Events.UMODE: EventHook(self._on_umode, source=True, target=True, message=True), Events.JOIN: EventHook(self._on_join, source=True, message=True), Events.PART: EventHook(self._on_part, source=True, target=True, message=True), Events.QUIT: EventHook(self._on_quit, source=True, message=True), Events.RPL_NAMEREPLY: EventHook(self._on_name_reply, source=True, target=True, args=True, message=True), Events.PRIVMSG: EventHook(self._on_privmsg, source=True, target=True, message=True), Events.PUBMSG: EventHook(self._on_pubmsg, source=True, target=True, message=True), Events.RPL_CHANNELMODEIS: EventHook(self._on_channel_mode, source=True, target=True, args=True), Events.ERR_NICKNAMEINUSE: EventHook(self.on_nickname_in_use), Events.ERROR: EventHook(self._on_error, message=True) }, event_plugins)) self.process_lock = Lock() def start(self): """Starts the Client by first connecting to all given servers and then starting the main loop.""" for server in self._servers: self._connect(server) try: self.on_initial_connect() except NotImplementedError: pass while self.connection_manager.running: with self.process_lock: self.connection_manager.process() def __connect(self, server): connection = Connection(server) connected = connection.connect() if connected: self.connection_manager.register(connection) return connected def _try_connect(self, server): while True: if self.__connect(server): return time.sleep(30) def _connect(self, server): """Performs a connection to the server by creating a Connection object, connecting it, and then registering the new Connection with the ConnectionManager.""" server.reset() if not self.__connect(server): Thread(target=self._try_connect, args=(server,)).start() def on_initial_connect(self): """Function performed after all servers have been connected.""" raise NotImplementedError def exit(self, message=u'Bye!'): """Disconnects from every connection in the ConnectionManager with the given QUIT message.""" with self.process_lock: self.connection_manager.exit(message) def get_server_by_name(self, name): for server in self._servers: if server.name == name: return server return None def get_server_channels(self, server): for channel in self.channels: if channel.server == server: yield channel def find_channel(self, server, name): """Searches for a Channel based on the server and the name. Returns None if the Channel is not found.""" search_channel = Channel(server, name) for channel in self.channels: if search_channel == channel: return channel return None def remove_channel(self, server, name): """Tries to remove a Channel based on the server and the name, and fails silently.""" channel = Channel(server, name) try: self.channels.remove(channel) except ValueError: log.debug(u"Channel `%s' not in channels." % name) def find_connection(self, server): """Searches for a Connection based on the server and compares only the host of the server. Returns None if the Connection is not found.""" for connection in self.connection_manager.connections.itervalues(): if connection.server.host == server.host: return connection return None def connect(self, host, port, nick, use_ssl=False): """Used for connecting to a server not given when creating the Client object.""" server = Server(host, port, nick, use_ssl) self._connect(server) def nick(self, connection, new_nick): """Changes the nick in the given connection.""" connection.send('NICK ' + new_nick) def mode(self, connection, target, mode=None): """Performs the IRC MODE command.""" mode_message = 'MODE %s' % target if mode is not None: mode_message += ' %s' % mode connection.send(mode_message) def privmsg(self, connection, target, message): """Sends a PRIVMSG to a target in the given connection.""" connection.send('PRIVMSG %s :%s' % (target, message)) def notice(self, connection, target, message): """Sends a NOTICE to a target in the given connection.""" connection.send('NOTICE %s :%s' % (target, message)) def ctcp_reply(self, connection, target, command, reply): """Sends a CTCP reply to a target in a given connection.""" self.notice(connection, target, '\x01%s %s\x01' % (command, reply)) def join(self, connection, *channels): """Makes the client join a bunch of channels. Example password protected channel argument: '#mathematics love' where 'love' is the password.""" connection.send('JOIN ' + ','.join(channels)) def part(self, connection, *channels): """Makes the client part from a bunch of channels.""" connection.send('PART ' + ','.join(channels)) def kick(self, connection, channel, user, reason=''): """Kicks a user from a channel in a given connection for a given reason.""" connection.send('KICK %s %s :%s' % (channel, user, reason)) def quit(self, connection, message=u''): """Disconnects the given connection with the given message. This is better than just quitting because it also cleans things up with the connection manager.""" with self.process_lock: self.connection_manager.disconnect(connection, message) def on_connect(self, connection): raise NotImplementedError def _on_connect(self, connection): connection.send('NICK ' + connection.server.nick) connection.send('USER %s 0 * :%s' % (connection.server.nick, connection.server.nick)) try: self.on_connect(connection) except NotImplementedError: pass def on_welcome(self, connection, source, target, message): raise NotImplementedError def _on_welcome(self, connection, source, target, message): connection.server.actual_nick = target try: self.on_welcome(connection, source, target, message) except NotImplementedError: pass def on_nickname_in_use(self, connection): connection.server.nick = connection.server.nick + '_' self._on_connect(connection) def _on_join(self, connection, source, message): if source.nick == connection.server.actual_nick: channel = Channel(connection.server, message) self.channels.append(channel) self.mode(connection, channel) else: channel = self.find_channel(connection.server, message) if channel is not None: channel.add_user(User(source.nick, '', '', User.NORMAL)) def _on_name_reply(self, connection, source, target, args, message): channel_name = args[-1] channel = self.find_channel(connection.server, channel_name) if channel is not None: for user in message.split(): channel.add_user(User.parse_user(user)) def _on_part(self, connection, source, target, message): if source.nick == connection.server.actual_nick: self.remove_channel(connection.server, target) else: channel = self.find_channel(connection.server, target) if channel is not None: channel.remove_user(User(source.nick, '', '', User.NORMAL)) def _on_quit(self, connection, source, message): user = User.channel_user(source.nick) for channel in self.get_server_channels(connection.server): channel.remove_user(user) def on_privmsg(self, connection, source, target, message): raise NotImplementedError def _on_privmsg(self, connection, source, target, message): if message[0] == '\x01' and message[-1] == '\x01': self._on_ctcp(connection, source, message[1:-1]) else: try: self.on_privmsg(connection, source, target, message) except NotImplementedError: pass def on_pubmsg(self, connection, source, target, message): raise NotImplementedError def _on_pubmsg(self, connection, source, target, message): try: self.on_pubmsg(connection, source, target, message) except NotImplementedError: pass def _on_channel_mode(self, connection, source, target, args): (name, modes), mode_args = args[:2], args[2:] channel = self.find_channel(connection.server, name) if channel is not None: channel_modes = Mode.parse_modes(modes, mode_args) for mode in channel_modes: if mode.on: channel.add_mode(mode) else: channel.remove_mode(mode) def get_version(self): raise NotImplementedError def _on_ctcp(self, connection, source, message): command, arg = '', '' split_message = message.split(' ', 1) if len(split_message) > 1: command, arg = split_message else: command = split_message[0] reply = '' if command == 'VERSION': try: reply = self.get_version() except NotImplementedError: pass elif command == 'PING' and arg: reply = arg elif command == 'TIME': reply = ':%s' % datetime.now().ctime() if reply: self.ctcp_reply(connection, source.nick, command, reply) def _on_mode(self, connection, source, target, args): channel = self.find_channel(connection.server, target) if channel is not None: modes, mode_args = args[0], args[1:] channel_modes = Mode.parse_modes(modes, mode_args) for mode in channel_modes: user = channel.find_user(mode.param) if user is not None: if mode.on: user.add_mode(mode) else: user.remove_mode(mode) else: if mode.on: channel.add_mode(mode) else: channel.remove_mode(mode) def _on_umode(self, connection, source, target, message): pass def _on_ping(self, connection, message): connection.send('PONG %s :%s' % (connection.server.actual_host, message)) def _on_error(self, connection, message): log.error(message) self._connect(connection.server)
from qrtest import QRCodeManager from connections import ConnectionManager from speaker import Speaker DESC_SERVER_URL = "http://10.11.3.45:8080/hackathon/index.php" man = QRCodeManager() prodCode = man.readQR() #im = cv2.imread("temp_qr.png") #dec = man.decodeQR(im) #man.displayQR(im, dec) CM = ConnectionManager(DESC_SERVER_URL) desc = CM.getDescription(prodCode) print("From server:\n" + desc) #get from wav from server sp = Speaker() sp.getAudioFile(desc, 'product_description') sp.playDescription('product_description.wav')
transports = siptracklib.transport.transports from siptracklib.utils import (object_by_attribute, fetch_device_path) def connect(hostname, username=None, password=None, port=None, session_id=None, transport='default', verify_session_id=False, use_ssl=True): import siptracklib.root if port is None: if use_ssl: port = default_ssl_port else: port = default_port t = transports[transport](hostname, port, use_ssl) t.connect(username, password, session_id, verify_session_id) object_store = siptracklib.root.ObjectStore(t) return object_store from connections import ConnectionManager cm = ConnectionManager(interactive=True) config = cm.config connection_manager = cm fconnect = cm.connect
class Session: def __init__(self): # TODO: consider saving and loading the connections list to a file # to preserve the list between sessions self._con_mgr = ConnectionManager() # The data on the common clipboard. self._clipboard_data = None # Type of data on the clipboard (eg. text, bitmap, etc.) # This should be one of the supported types in info.py self._data_type = None # None will mean that this client is owner, otherwise it should be a # Connection object. self._data_owner = None # TODO add command line switch to change port, which would be passed in # here self._network = Network(con_callback=self._new_connection_request, dis_callback=self._disconnect_request) self._network.start() def _new_connection_request(self, address, port): conn = self._con_mgr.get_connection(address) if conn: #conn.status = Connection.REQUEST conn.status = Connection.CONNECTED else: #self._con_mgr.new_connection("", address, Connection.REQUEST) self._con_mgr.new_connection("", address, Connection.CONNECTED) def _disconnect_request(self, address, port): conn = self._con_mgr.get_connection(address) if conn: conn.status = Connection.NOT_CONNECTED def get_clipboard_data(self): self._clipboard_data = self._network.get_clipboard() return self._clipboard_data def get_clipboard_data_type(self): self._data_type = self._network.get_clipboard_data_type() return self._data_type def get_clipboard_data_owner(self): return self._data_owner def set_clipboard_data(self, data, data_type): """ This is called (by the gui) when the user pastes to the app. """ self._clipboard_data = data self._network.set_clipboard(self._clipboard_data) self._data_type = data_type self._data_owner = None def connections(self): """ Returns a list of all the connections """ return self._con_mgr.connections def get_connection(self, address): """ Returns the Connection object that has the given address """ return self._con_mgr.get_connection(address) def new_connection(self, alias, address): """ Creates a new Connection to the given address and if there is a Syncboard app running at that address then that user will see a new connection appear (with the address on this end) with status REQUEST. After this has executed: New Connection on both ends. Connection on this end status: PENDING Conneciton on other end status: REQUEST """ self._network.connect(address) self._con_mgr.new_connection(alias, address) def accept_connection(self, address): """ Called when the user accepts the request from the Connection with the given address. Meaning, there was a Connection with status REQUEST and user accepted it. Before this is called: Connection on this end status: REQUEST Conneciton on other end status: PENDING After this has executed: Connection on this end status: CONNECTED Conneciton on other end status: CONNECTED """ conn = self.get_connection(address) if conn: print "Connection from %s accepted" % address conn.status = Connection.CONNECTED else: print "Error: no connection from %s exists" % address def request_connection(self, address): """ This is like new_connection except in this case the Connection with the given address already existed and had status NOT_CONNECTED. Before this is called: Connection on this end status: NOT_CONNECTED Conneciton on other end status: NOT_CONNECTED After this has executed: Connection on this end status: PENDING Conneciton on other end status: REQUEST """ self._network.connect(address) conn = self.get_connection(address) if conn: print "Request to connect to %s sent" % address #conn.status = Connection.PENDING conn.status = Connection.CONNECTED else: print "Error: no connection to %s exists" % address def disconnect(self, address): """ This is called when the user has an open connection to the given address and wants to close the connection. Before this is called: Connection on this end status: CONNECTED Conneciton on other end status: CONNECTED After this has executed: Connection on this end status: NOT_CONNECTED Conneciton on other end status: NOT_CONNECTED """ self._network.disconnect(address) conn = self.get_connection(address) if conn: print "Disconnected from %s" % address conn.status = Connection.NOT_CONNECTED else: print "Error: no connection to %s exists" % address def cancel_request(self, address): """ This is called when the user has requested a connection to the given address and the request is still pending but the user wants to cancel the request. Before this is called: Connection on this end status: PENDING Conneciton on other end status: REQUEST After this has executed: Connection on this end status: NOT_CONNECTED Conneciton on other end status: NOT_CONNECTED """ conn = self.get_connection(address) if conn: print "Request to %s canceled" % address conn.status = Connection.NOT_CONNECTED else: print "Error: no connection to %s exists" % address def del_connection(self, address): """ Removes the Connection with the given address from the list of connections. """ conn = self.get_connection(address) if conn: if conn.status == Connection.CONNECTED: self.disconnect(address) self._con_mgr.del_connection(address) else: print "Error: no connection to %s exists" % address def update_alias(self, address, new_alias): """ Changes the alias of the Connection with the given address to be new_alias. """ conn = self.get_connection(address) if conn: print "Updated alias of %s to %s" % (address, new_alias) conn.alias = new_alias else: print "Error: no connection to %s exists" % address def close(self): self._network.stop()