コード例 #1
0
    def recv_thread(self, channel_server_address, channel):
        def channel_message_received(command, payload):
            if command == 'connect':
                message = json.loads(payload)
                tx_channel = message['tx_channel']
                rx_channel = message['rx_channel']
                new_channel_server_address = message['channel_server_address']

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                s.connect(self.__server_address)

                proxy = ChannelProxy(s, rx_channel, tx_channel,
                                     new_channel_server_address)
                proxy.start()

                with self.__lock:
                    self.__channel_proxies.append(proxy)

            elif command == 'quit':
                return False

            return self.__running

        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__pubsubsocket.recv(channel, channel_message_received)

        print 'recv_thread terminates.'
コード例 #2
0
    def recv_thread(self, channel_server_address, channel):
        def channel_message_received(command, payload):
            if command == 'connect':
                message = json.loads(payload)
                tx_channel = message['tx_channel']
                rx_channel = message['rx_channel']
                new_channel_server_address = message['channel_server_address']

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                s.connect(self.__server_address)

                proxy = ChannelProxy(s, rx_channel, tx_channel, new_channel_server_address)
                proxy.start()

                with self.__lock:
                    self.__channel_proxies.append(proxy)

            elif command == 'quit':
                return False

            return self.__running

        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__pubsubsocket.recv(channel, channel_message_received)

        print 'recv_thread terminates.'
コード例 #3
0
    def run_main_loop(self):  # TODO: check channel proxies health, if not synchronized too long, should kill it.
        assert not self.__running

        self.__running = True

        try:
            self.__socket.listen(5)
            while self.__running:
                s, addr = self.__socket.accept()
                if not self.__running:
                    break

                try:
                    ret = apiclient.connect(conf.USER_NAME, conf.PASSWORD, self.__sensor_name)
                except Exception:
                    traceback.print_exc()
                    time.sleep(3.0)
                    continue

                if ret:
                    channel_server_address, channel, transfer_channel_server_address = ret
                    rx_channel = 'rx-' + str(uuid.uuid4())
                    tx_channel = 'tx-' + str(uuid.uuid4())

                    proxy = ChannelProxy(s, rx_channel, tx_channel, transfer_channel_server_address)
                    proxy.start()
                    self.__proxies.append(proxy)

                    s = PubSubSocket(channel_server_address)
                    s.send(channel, 'connect',
                           json.dumps({'tx_channel': rx_channel,
                                       'rx_channel': tx_channel,
                                       'channel_server_address': transfer_channel_server_address}))

                else:
                    s.close()

        finally:
            print 'stopping channel proxies...'
            for p in self.__proxies:
                if p.running:
                    p.stop()

            self.__proxies = []
コード例 #4
0
    def __init__(self, sock, rx_channel, tx_channel, channel_server_address):
        assert isinstance(sock, socket.socket)
        assert isinstance(rx_channel, (str, unicode))
        assert isinstance(tx_channel, (str, unicode))
        assert isinstance(channel_server_address, (str, unicode))

        super(ChannelProxy, self).__init__()

        self.__socket = sock
        self.__rx_channel = rx_channel
        self.__tx_channel = tx_channel
        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__running = False
        self.__socket_receiving_thread = None
        self.__channel_receiving_thread = None
        self.__lock = threading.Lock()
        self.__subscribed = False
        self.__other_ready = False
        self.__start_time = None
コード例 #5
0
    def run_main_loop(
        self
    ):  # TODO: check channel proxies health, if not synchronized too long, should kill it.
        assert not self.__running

        self.__running = True

        try:
            self.__socket.listen(5)
            while self.__running:
                s, addr = self.__socket.accept()
                if not self.__running:
                    break

                try:
                    ret = apiclient.connect(conf.USER_NAME, conf.PASSWORD,
                                            self.__sensor_name)
                except Exception:
                    traceback.print_exc()
                    time.sleep(3.0)
                    continue

                if ret:
                    channel_server_address, channel, transfer_channel_server_address = ret
                    rx_channel = 'rx-' + str(uuid.uuid4())
                    tx_channel = 'tx-' + str(uuid.uuid4())

                    proxy = ChannelProxy(s, rx_channel, tx_channel,
                                         transfer_channel_server_address)
                    proxy.start()
                    self.__proxies.append(proxy)

                    s = PubSubSocket(channel_server_address)
                    s.send(
                        channel, 'connect',
                        json.dumps({
                            'tx_channel':
                            rx_channel,
                            'rx_channel':
                            tx_channel,
                            'channel_server_address':
                            transfer_channel_server_address
                        }))

                else:
                    s.close()

        finally:
            print 'stopping channel proxies...'
            for p in self.__proxies:
                if p.running:
                    p.stop()

            self.__proxies = []
コード例 #6
0
class ClientProxy(object):
    def __init__(self, id, sensor_name, server_address):
        assert isinstance(id, int)
        assert isinstance(server_address, (tuple, list))
        assert len(server_address) == 2
        assert isinstance(server_address[0], str)
        assert isinstance(server_address[1], int)
        assert isinstance(sensor_name, str)

        super(ClientProxy, self).__init__()

        self.__id = id
        self.__server_address = tuple(server_address)
        self.__receive_thread = None
        self.__sensor_name = sensor_name
        self.__running = False
        self.__channel_proxies = []
        self.__lock = threading.Lock()
        self.__thread = None
        self.__pubsubsocket = None
        self.__main_thread = None

    @property
    def id(self):
        return self.__id

    @property
    def sensor_name(self):
        return self.__sensor_name

    @property
    def server_address(self):
        return self.__server_address

    def start(self):
        assert not self.__main_thread
        self.__main_thread = threading.Thread(target=self.run_main_loop)
        self.__main_thread.start()

    def stop(self):
        assert self.__main_thread
        self.__running = False
        self.__main_thread.join()
        self.__main_thread = None

    def run_main_loop(self):
        assert not self.__running

        self.__running = True

        try:
            while self.__running:
                try:
                    ret = apiclient.listen(conf.USER_NAME, conf.PASSWORD, self.__sensor_name)
                except Exception:
                    traceback.print_exc()
                    time.sleep(3.0)
                    continue

                if ret:
                    channel_server_address, channel = ret
                    self.__thread = threading.Thread(target=self.recv_thread, args=(channel_server_address, channel))
                    self.__thread.start()

                    # check channel continously
                    while self.__running:
                        try:
                            ret = apiclient.check_listen_channel(conf.USER_NAME, conf.PASSWORD,
                                                                 self.__sensor_name, channel)

                        except Exception:
                            traceback.print_exc()
                            continue

                        if ret:
                            for i in range(10):
                                time.sleep(1.0)
                                if not self.__running:
                                    break

                        else:
                            with self.__lock:
                                self.__pubsubsocket.request_stop_receiving()
                                self.__pubsubsocket.send(channel, 'quit')
                                self.__thread.join()
                                self.__pubsubsocket = None
                                self.__thread = None

                            break

                else:
                    time.sleep(3.0)

        except Exception:
            traceback.print_exc()

        finally:
            with self.__lock:
                if self.__pubsubsocket and self.__thread:
                    self.__pubsubsocket.request_stop_receiving()
                    self.__pubsubsocket.send(channel, 'quit')
                    self.__thread.join()
                    self.__pubsubsocket = None
                    self.__thread = None

            print 'stopping channel proxies...'
            with self.__lock:
                for p in self.__channel_proxies:
                    if p.running:
                        p.stop()

                self.__channel_proxies = []

            print 'run_loop terminates.'

    def recv_thread(self, channel_server_address, channel):
        def channel_message_received(command, payload):
            if command == 'connect':
                message = json.loads(payload)
                tx_channel = message['tx_channel']
                rx_channel = message['rx_channel']
                new_channel_server_address = message['channel_server_address']

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                s.connect(self.__server_address)

                proxy = ChannelProxy(s, rx_channel, tx_channel, new_channel_server_address)
                proxy.start()

                with self.__lock:
                    self.__channel_proxies.append(proxy)

            elif command == 'quit':
                return False

            return self.__running

        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__pubsubsocket.recv(channel, channel_message_received)

        print 'recv_thread terminates.'
コード例 #7
0
class ChannelProxy(object):
    def __init__(self, sock, rx_channel, tx_channel, channel_server_address):
        assert isinstance(sock, socket.socket)
        assert isinstance(rx_channel, (str, unicode))
        assert isinstance(tx_channel, (str, unicode))
        assert isinstance(channel_server_address, (str, unicode))

        super(ChannelProxy, self).__init__()

        self.__socket = sock
        self.__rx_channel = rx_channel
        self.__tx_channel = tx_channel
        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__running = False
        self.__socket_receiving_thread = None
        self.__channel_receiving_thread = None
        self.__lock = threading.Lock()
        self.__subscribed = False
        self.__other_ready = False
        self.__start_time = None

    def start(self):
        assert not self.__running

        self.__running = True
        self.__start_time = time.time()

        self.__socket_receiving_thread = threading.Thread(target=self._socket_receiver_thread_main)
        self.__socket_receiving_thread.setDaemon(True)
        self.__socket_receiving_thread.start()

        self.__channel_receiving_thread = threading.Thread(target=self._channel_receiver_thread_main)
        self.__channel_receiving_thread.setDaemon(True)
        self.__channel_receiving_thread.start()

    def stop(self):
        if self.__running:
            self.__running = False

            with self.__lock:
                if self.__socket:
                    self.__socket.shutdown(socket.SHUT_RDWR)
                    self.__socket = None

                self.__pubsubsocket.send(self.__rx_channel, 'quit')

            self.__socket_receiving_thread.join()
            self.__socket_receiving_thread = None

            self.__channel_receiving_thread.join()
            self.__channel_receiving_thread = None

    def _socket_receiver_thread_main(self):
        while not self.__other_ready:
            time.sleep(0.01)

        while self.__running:
            try:
                raw = self.__socket.recv(1024 * 1024)  # 1MB
            except socket.error:
                traceback.print_exc()
                raw = ''

            with self.__lock:
                if len(raw):
                    self.__pubsubsocket.send(self.__tx_channel, 'send', raw)

                else:
                    self.__pubsubsocket.send(self.__tx_channel, 'close')
                    if self.__socket:
                        self.__socket.close()
                        self.__socket = None

                    self.__running = False
                    self.__pubsubsocket.send(self.__rx_channel, 'quit')
                    break

        print 'socket receiver thread exits.'

    def _channel_receiver_thread_main(self):
        def callback(command, payload):
            if command is None and payload is None:
                print 'im subscribed channel.'
                if not self.__subscribed:
                    self.__subscribed = True
                    self.__pubsubsocket.send(self.__tx_channel, 'imready')
                    print ' and send message.'

                return True

            else:
                with self.__lock:
                    if command == 'send':
                        if self.__socket:
                            self.__socket.sendall(payload)

                        return True

                    elif command == 'close':
                        print 'close socket...'
                        if self.__socket:
                            self.__socket.shutdown(socket.SHUT_RDWR)
                            self.__socket = None

                        self.__running = False
                        return False

                    elif command == 'quit':
                        return False

                    elif command == 'imready':
                        if not self.__other_ready:
                            print 'other side has subscirbed channel.'
                            self.__other_ready = True

                            # send again
                            if self.__subscribed:
                                self.__pubsubsocket.send(self.__tx_channel, 'imready')
                                print ' send imready message again.'

                    return True

        self.__pubsubsocket.recv(self.__rx_channel, callback)
        print 'channel receiver thread exits.'

    @property
    def running(self):
        return self.__running

    @property
    def handshaked(self):
        return self.__subscribed and self.__other_ready
コード例 #8
0
class ClientProxy(object):
    def __init__(self, id, sensor_name, server_address):
        assert isinstance(id, int)
        assert isinstance(server_address, (tuple, list))
        assert len(server_address) == 2
        assert isinstance(server_address[0], str)
        assert isinstance(server_address[1], int)
        assert isinstance(sensor_name, str)

        super(ClientProxy, self).__init__()

        self.__id = id
        self.__server_address = tuple(server_address)
        self.__receive_thread = None
        self.__sensor_name = sensor_name
        self.__running = False
        self.__channel_proxies = []
        self.__lock = threading.Lock()
        self.__thread = None
        self.__pubsubsocket = None
        self.__main_thread = None

    @property
    def id(self):
        return self.__id

    @property
    def sensor_name(self):
        return self.__sensor_name

    @property
    def server_address(self):
        return self.__server_address

    def start(self):
        assert not self.__main_thread
        self.__main_thread = threading.Thread(target=self.run_main_loop)
        self.__main_thread.start()

    def stop(self):
        assert self.__main_thread
        self.__running = False
        self.__main_thread.join()
        self.__main_thread = None

    def run_main_loop(self):
        assert not self.__running

        self.__running = True

        try:
            while self.__running:
                try:
                    ret = apiclient.listen(conf.USER_NAME, conf.PASSWORD,
                                           self.__sensor_name)
                except Exception:
                    traceback.print_exc()
                    time.sleep(3.0)
                    continue

                if ret:
                    channel_server_address, channel = ret
                    self.__thread = threading.Thread(
                        target=self.recv_thread,
                        args=(channel_server_address, channel))
                    self.__thread.start()

                    # check channel continously
                    while self.__running:
                        try:
                            ret = apiclient.check_listen_channel(
                                conf.USER_NAME, conf.PASSWORD,
                                self.__sensor_name, channel)

                        except Exception:
                            traceback.print_exc()
                            continue

                        if ret:
                            for i in range(10):
                                time.sleep(1.0)
                                if not self.__running:
                                    break

                        else:
                            with self.__lock:
                                self.__pubsubsocket.request_stop_receiving()
                                self.__pubsubsocket.send(channel, 'quit')
                                self.__thread.join()
                                self.__pubsubsocket = None
                                self.__thread = None

                            break

                else:
                    time.sleep(3.0)

        except Exception:
            traceback.print_exc()

        finally:
            with self.__lock:
                if self.__pubsubsocket and self.__thread:
                    self.__pubsubsocket.request_stop_receiving()
                    self.__pubsubsocket.send(channel, 'quit')
                    self.__thread.join()
                    self.__pubsubsocket = None
                    self.__thread = None

            print 'stopping channel proxies...'
            with self.__lock:
                for p in self.__channel_proxies:
                    if p.running:
                        p.stop()

                self.__channel_proxies = []

            print 'run_loop terminates.'

    def recv_thread(self, channel_server_address, channel):
        def channel_message_received(command, payload):
            if command == 'connect':
                message = json.loads(payload)
                tx_channel = message['tx_channel']
                rx_channel = message['rx_channel']
                new_channel_server_address = message['channel_server_address']

                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                s.connect(self.__server_address)

                proxy = ChannelProxy(s, rx_channel, tx_channel,
                                     new_channel_server_address)
                proxy.start()

                with self.__lock:
                    self.__channel_proxies.append(proxy)

            elif command == 'quit':
                return False

            return self.__running

        self.__pubsubsocket = PubSubSocket(channel_server_address)
        self.__pubsubsocket.recv(channel, channel_message_received)

        print 'recv_thread terminates.'