Ejemplo n.º 1
0
 def receive_event(self):
     event = None
     events = yield from self.monitor_poller.poll(timeout=2000)
     if self.monitor_socket in dict(events):
         msg = yield from self.monitor_socket.recv_multipart(
             flags=NOBLOCK)
         event = parse_monitor_message(msg)
     return event
Ejemplo n.º 2
0
 async def _cothread_connection_monitor(self,
                                        physical_addr: PhysicalAddress,
                                        connection: Socket):
     monitor_socket: Socket = connection.get_monitor_socket()
     while True:
         messages = await monitor_socket.recv_multipart()
         evd = parse_monitor_message(messages)
         evtype = evd["event"]
         if evtype == zmq.EVENT_DISCONNECTED:
             physical_addr.status = PhysicalAddressStatus.OFFLINE
         elif evtype == zmq.EVENT_CONNECTED:
             physical_addr.status = PhysicalAddressStatus.CONNECTED
         elif evtype == zmq.EVENT_CLOSED:
             physical_addr.status = PhysicalAddressStatus.OFFLINE
             self.physical_connections.pop(physical_addr.address)
             return
Ejemplo n.º 3
0
async def send_out(ctx, msg, socket_id):
    # Setup a socket and its monitor
    socket = ctx.socket(zmq.DEALER)
    s = socket.get_monitor_socket()

    # Try to connect
    socket.connect(str(socket_id))

    # See if the connection was successful
    evnt = await s.recv_multipart()
    evnt_dict = monitor.parse_monitor_message(evnt)

    # If so, shoot out the message
    if evnt_dict['event'] == 1:
        socket.send(msg, flags=zmq.NOBLOCK)
        socket.disconnect(str(socket_id))
        return True, evnt_dict['endpoint'].decode()

    # Otherwise, close the socket. Return result and the socket for further processing / updating sockets
    socket.disconnect(str(socket_id))
    return False, evnt_dict['endpoint'].decode()
Ejemplo n.º 4
0
async def secure_send_out(wallet,
                          ctx,
                          msg,
                          socket_id,
                          server_vk,
                          cert_dir='cilsocks'):
    # Setup a socket and its monitor
    socket = ctx.socket(zmq.DEALER)

    socket.curve_secretkey = wallet.curve_sk
    socket.curve_publickey = wallet.curve_vk

    cert_dir = pathlib.Path.home() / cert_dir
    cert_dir.mkdir(parents=True, exist_ok=True)

    server_pub, _ = load_certificate(str(cert_dir / f'{server_vk}.key'))

    socket.curve_serverkey = server_pub

    s = socket.get_monitor_socket()

    # Try to connect
    socket.connect(str(socket_id))

    event = 2
    evnt_dict = {}
    while event == 2:
        evnt = await s.recv_multipart()
        evnt_dict = monitor.parse_monitor_message(evnt)
        event = evnt_dict['event']

    # If so, shoot out the message
    if event == 1:
        socket.send(msg, flags=zmq.NOBLOCK)
        socket.disconnect(str(socket_id))
        return True, evnt_dict['endpoint'].decode()

    # Otherwise, close the socket. Return result and the socket for further processing / updating sockets
    socket.disconnect(str(socket_id))
    return False, evnt_dict['endpoint'].decode()
Ejemplo n.º 5
0
    def __io_loop(self):
        processor = self.proc_class()

        while self.running.is_set():
            socks = dict(self.poller.poll())

            # Received a message on exchange's socket
            if self.client_sock in socks:
                msg = self.client_sock.recv()
                obj = api.MessageParser.parse(msg)
                self.__submit_thpool(processor.handle, obj)

            # Received an exchange's socket state event
            if self.client_state_sock in socks:
                msg = self.client_state_sock.recv_multipart()
                state = parse_monitor_message(msg)
                self.__submit_thpool(processor.handle_state_event, state)

            # Received a message from exchange's publisher network
            if self.publisher_sock in socks:
                topic, msg = self.publisher_sock.recv_multipart()
                obj = api.MessageParser.parse(msg)
                self.__submit_thpool(processor.handle, obj)

            # Received a message on command socket
            if self.cmd_sock in socks:
                sid = self.cmd_sock.recv()
                msg = self.cmd_sock.recv_pyobj()

                # Handle exit interrupt
                if msg == "EXIT":
                    self.running.clear()

                # Handle other messages to send to the exchange
                elif isinstance(msg, list) and msg[0] == "SEND":
                    self.client_sock.send(msg[1])
Ejemplo n.º 6
0
 def _internal_monitor_callback(self, msg):
     if self._monitor_callback is not None:
         self._monitor_callback(parse_monitor_message(msg))