Exemplo n.º 1
0
class Client():
    def __init__(self, configure, connection_callback, **kwargs):
        assert configure.has_key('remote_address')
        remote_ip, remote_port = configure['remote_address']
        self.connection_append = connection_callback[0]
        self.connection_remove = connection_callback[1]
        self.receiver_cb = connection_callback[2]
        for key, value in kwargs.iteritems():
            setattr(self, key, value)
        self.tcp_client = ALPSTCPClient(remote_ip,
                                        remote_port,
                                        receiver_func=self.__receiver,
                                        background=True)
        tcp_client_connect_thread = ALPSThread(threadfunc=self.__connection_attempt_thread,
                                               threadname="Distributor TCP Client Thread")
        tcp_client_connect_thread.start()

    def __receiver(self, connection, data):
        self.receiver_cb(self, (connection.ip_string, connection.port), data)

    def __connection_attempt_thread(self):
        def send_func(packet):
            self.tcp_client.send(packet)
        while True:
            time.sleep(5)
            try:
                self.tcp_client.connect("Distributor TCP Client Thread", error_except=False)
                break
            except:
                ALPSDebug.alps_print(ALPSDebug.LEVEL.WARN,
                                 'Distributor TCP Client failed to connect to server(%s:%s).' %
                                 (self.tcp_client.ip_string, self.tcp_client.port),
                                 'Try again later.')
        self.connection_append(self,
                               (self.tcp_client.ip_string, self.tcp_client.port),
                               lambda packet:self.tcp_client.send(packet)
                               )
Exemplo n.º 2
0
 def __init__(self, configure, connection_callback, **kwargs):
     assert configure.has_key('remote_address')
     remote_ip, remote_port = configure['remote_address']
     self.connection_append = connection_callback[0]
     self.connection_remove = connection_callback[1]
     self.receiver_cb = connection_callback[2]
     for key, value in kwargs.iteritems():
         setattr(self, key, value)
     self.tcp_client = ALPSTCPClient(remote_ip,
                                     remote_port,
                                     receiver_func=self.__receiver,
                                     background=True)
     tcp_client_connect_thread = ALPSThread(threadfunc=self.__connection_attempt_thread,
                                            threadname="Distributor TCP Client Thread")
     tcp_client_connect_thread.start()