コード例 #1
0
ファイル: lib_messaging.py プロジェクト: dustinfast/PTC-Sim
    def run(self):
        Receiver(self.outgoing_queues).start()
        MsgServer(self.outgoing_queues).start()
        broker_log.info('BOS Started.')

        # Stay alive (there is likely a better way to do this)
        while True:
            sleep(10)
コード例 #2
0
ファイル: lib_messaging.py プロジェクト: dustinfast/PTC-Sim
    def run(self):
        # Init TCP/IP listener
        try:
            sock = socket.socket()
            sock.bind((BROKER, SEND_PORT))
            sock.listen(1)
        except:
            print('!!ERROR 1!! - See issue #9')
            exit()

        while True:
            # Block until timeout or a send request is received
            try:
                conn, client = sock.accept()
            except:
                continue

            # Receive the msg from sender, responding with either OK or FAIL
            log_str = 'Incoming msg from ' + str(client[0]) + ' gave: '
            try:
                raw_msg = conn.recv(MAX_MSG_SIZE).decode()
                msg = Message(raw_msg.decode('hex'))
                conn.send('OK'.encode())
                conn.close()
            except Exception as e:
                log_str += 'Msg recv failed due to ' + str(e)
                broker_log.error(log_str)

                try:
                    conn.send('FAIL'.encode())
                except:
                    pass

                conn.close()
                continue

            # Add msg to outgoing queue dict, keyed by dest_addr
            if not self.outgoing_queues.get(msg.dest_addr):
                self.outgoing_queues[msg.dest_addr] = Queue.Queue()
            self.outgoing_queues[msg.dest_addr].put(msg)
            log_str = 'Msg served: ' + msg.sender_addr + ' '
            log_str += 'to ' + msg.dest_addr
            broker_log.info(log_str)

        # Do cleanup
        sock.close()
コード例 #3
0
ファイル: lib_messaging.py プロジェクト: dustinfast/PTC-Sim
    def run(self):
        # Init listener
        try:
            sock = socket.socket()
            sock.bind((BROKER, FETCH_PORT))
            sock.listen(1)
        except:
            print('!!ERROR 2!! - See issue #9')
            exit()

        while True:
            # Block until timeout or a send request is received
            try:
                conn, client = sock.accept()
            except:
                continue

            # Process the request
            log_str = 'Fetch request from ' + str(client[0]) + ' '
            try:
                queue_name = conn.recv(MAX_MSG_SIZE).decode()
                log_str += 'for ' + queue_name + ' gave: '

                msg = None
                try:
                    msg = self.outgoing_queues[queue_name].get(timeout=.5)
                except:
                    log_str += 'Queue empty.'
                    conn.send('EMPTY'.encode())

                if msg:
                    conn.send(msg.raw_msg.encode('hex'))  # Send msg
                    log_str += 'Msg served.'

                broker_log.info(log_str)
                conn.close()
            except:
                continue

        # Do cleanup
        sock.close()