Exemplo n.º 1
0
    def start(self, timeout=30):
        """
        Start the FIX server.
        """
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        self._socket.bind((self._input_host, self._input_port))
        self._ip, self._port = self._socket.getsockname()

        self.log_callback('Started server on {}:{}'.format(
            self.host, self.port))

        self._recv_thread = threading.Thread(target=self._listen)
        self._recv_thread.daemon = True
        self._recv_thread.start()

        timeout_info = TimeoutExceptionInfo()
        wait(lambda: self._listening, timeout=timeout, interval=0.1)

        if not self._listening:
            raise TimeoutException(
                'Could not start server: timed out on listening. {}'.format(
                    timeout_info.msg()))

        self.log_callback('Listening for socket events.')
Exemplo n.º 2
0
    def receive(self, timeout=None):
        """
        Receive message.

        :param timeout: Timeout in seconds.
        :type timeout: ``int``

        :return: received ``FixMessage`` object
        :rtype: ``FixMessage``
        """
        timeout = timeout if timeout is not None else self.cfg.receive_timeout
        timeout_info = TimeoutExceptionInfo()
        try:
            received = self._client.receive(timeout=timeout)
        except socket.timeout:
            self.logger.error(
                "Timed out waiting for message for {} seconds.".format(timeout)
            )
            raise TimeoutException(
                "Timed out waiting for message on {0}. {1}".format(
                    self.cfg.name, timeout_info.msg()
                )
            )
        self.logger.debug("Received msg {}.".format(received))
        return received
Exemplo n.º 3
0
 def receive(self, size=1024, timeout=30):
     """Receive bytes from the given connection."""
     received = None
     timeout_info = TimeoutExceptionInfo()
     try:
         received = self._client.receive(size, timeout=timeout or 0)
     except socket.timeout:
         if timeout is not None:
             raise TimeoutException(
                 "Timed out waiting for message on {0}. {1}".format(
                     self.cfg.name, timeout_info.msg()))
     return received
Exemplo n.º 4
0
    def receive(self, conn_name=(None, None), timeout=60):
        """
        Receive a FIX message from the given connection.

        The connection name defaults to ``(None, None)``. In this case,
        the server will try to find the one and only available connection. This
        will fail if there are more connections available or if the initial
        connection is no longer active.

        :param conn_name:  Connection name (sender and target ids) to receive
          message from.
        :type conn_name: ``tuple`` of ``str`` and ``str``
        :param timeout: timeout in seconds or ``None``

          - Specifying ``None`` as timeout will turn receive into a
            non-blocking call. In such case, if no message is immediately
            available, ``None`` is returned and no exception is raised.
          - Specifying a numeric value will make receive a blocking call.
            If no message is received within the timeframe, a TimeoutException
            is raised.

        :type timeout: ``int`` or ``NoneType``

        :return: received FixMessage object
        :rtype: ``FixMessage``
        """
        received = None
        timeout_info = TimeoutExceptionInfo()
        try:
            received = self._server.receive(conn_name, timeout=timeout or 0)
        except queue.Empty:
            self.logger.debug(
                "Timed out waiting for message for {} seconds".format(
                    timeout or 0
                )
            )
            if timeout is not None:
                raise TimeoutException(
                    "Timed out waiting for message on {0}. {1}".format(
                        self.cfg.name, timeout_info.msg()
                    )
                )

        self.logger.debug(
            "Received from connection {} msg {}".format(conn_name, received)
        )
        return received
Exemplo n.º 5
0
    def receive(self, size=None, conn_idx=None, timeout=30):
        """Receive bytes from the given connection."""
        received = None
        timeout_info = TimeoutExceptionInfo()
        try:
            receive_kwargs = dict(conn_idx=conn_idx, timeout=timeout or 0)
            if size is None:
                receive_kwargs['size'] = 1024
                receive_kwargs['wait_full_size'] = False
            else:
                receive_kwargs['size'] = size
                receive_kwargs['wait_full_size'] = True

            received = self._server.receive(**receive_kwargs)
        except socket.timeout:
            if timeout is not None:
                raise TimeoutException(
                    'Timed out waiting for message on {0}. {1}'.format(
                        self.cfg.name, timeout_info.msg()))
        return received