com.paritytrading.nassau.MessageListener Java Examples

The following examples show how to use com.paritytrading.nassau.MessageListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Recorder.java    From nassau with Apache License 2.0 6 votes vote down vote up
private static void main(Config config, File file) throws IOException {
    addShutdownHook();

    try (final BinaryFILEWriter writer = BinaryFILEWriter.open(file)) {

        MessageListener listener = new MessageListener() {

            @Override
            public void message(ByteBuffer buffer) throws IOException {
                writer.write(buffer);
            }

        };

        if (config.hasPath("session.multicast-interface")) {
            try (MoldUDP64Client client = join(config, listener)) {
                receive(client);
            }
        } else {
            try (SoupBinTCPClient client = connect(config, listener)) {
                receive(client);
            }
        }
    }
}
 
Example #2
Source File: TradeReporter.java    From parity with Apache License 2.0 6 votes vote down vote up
private static void main(Config config, boolean tsv) throws IOException {
    Instruments instruments = Instruments.fromConfig(config, "instruments");

    MessageListener listener = new PMRParser(new TradeProcessor(tsv ?
                new TSVFormat(instruments) : new DisplayFormat(instruments)));

    if (config.hasPath("trade-report.multicast-interface")) {
        NetworkInterface multicastInterface = Configs.getNetworkInterface(config, "trade-report.multicast-interface");
        InetAddress      multicastGroup     = Configs.getInetAddress(config, "trade-report.multicast-group");
        int              multicastPort      = Configs.getPort(config, "trade-report.multicast-port");
        InetAddress      requestAddress     = Configs.getInetAddress(config, "trade-report.request-address");
        int              requestPort        = Configs.getPort(config, "trade-report.request-port");

        MoldUDP64.receive(multicastInterface, new InetSocketAddress(multicastGroup, multicastPort),
                new InetSocketAddress(requestAddress, requestPort), listener);
    } else {
        InetAddress address  = Configs.getInetAddress(config, "trade-report.address");
        int         port     = Configs.getPort(config, "trade-report.port");
        String      username = config.getString("trade-report.username");
        String      password = config.getString("trade-report.password");

        SoupBinTCP.receive(new InetSocketAddress(address, port), username, password, listener);
    }
}
 
Example #3
Source File: StockTicker.java    From parity with Apache License 2.0 6 votes vote down vote up
private static void listen(Config config, MessageListener listener) throws IOException {
    if (config.hasPath("market-data.multicast-interface")) {
        NetworkInterface multicastInterface = Configs.getNetworkInterface(config, "market-data.multicast-interface");
        InetAddress      multicastGroup     = Configs.getInetAddress(config, "market-data.multicast-group");
        int              multicastPort      = Configs.getPort(config, "market-data.multicast-port");
        InetAddress      requestAddress     = Configs.getInetAddress(config, "market-data.request-address");
        int              requestPort        = Configs.getPort(config, "market-data.request-port");

        MoldUDP64.receive(multicastInterface, new InetSocketAddress(multicastGroup, multicastPort),
                new InetSocketAddress(requestAddress, requestPort), listener);
    } else {
        InetAddress address  = Configs.getInetAddress(config, "market-data.address");
        int         port     = Configs.getPort(config, "market-data.port");
        String      username = config.getString("market-data.username");
        String      password = config.getString("market-data.password");

        SoupBinTCP.receive(new InetSocketAddress(address, port), username, password, listener);
    }
}
 
Example #4
Source File: MoldUDP64DownstreamPacket.java    From nassau with Apache License 2.0 6 votes vote down vote up
/**
 * Apply the message listener to each message in this downstream packet.
 *
 * @param listener a message listener
 * @throws IOException if an I/O error occurs in the message listener
 */
public void apply(MessageListener listener) throws IOException {
    while (true) {
        if (payload.remaining() < 2)
            break;

        int messageLength = payload.getShort() & 0xffff;

        int limit = payload.limit();

        payload.limit(payload.position() + messageLength);

        listener.message(payload);

        payload.position(payload.limit());
        payload.limit(limit);
    }
}
 
Example #5
Source File: SoupBinTCPClient.java    From nassau with Apache License 2.0 6 votes vote down vote up
/**
 * Create a client. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param clock a clock
 * @param channel the underlying socket channel
 * @param maxPayloadLength maximum inbound message length
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPClient(Clock clock, SocketChannel channel, int maxPayloadLength,
        MessageListener listener, SoupBinTCPClientStatusListener statusListener) {
    super(clock, channel, Math.max(MIN_MAX_PAYLOAD_LENGTH, maxPayloadLength),
            PACKET_TYPE_CLIENT_HEARTBEAT);

    this.loginAccepted = new LoginAccepted();
    this.loginRejected = new LoginRejected();

    /*
     * This built-in payload transmit buffer is used for Login Request
     * packets.
     */
    this.txPayload = ByteBuffer.allocateDirect(46);

    this.listener = listener;

    this.statusListener = statusListener;
}
 
Example #6
Source File: SoupBinTCPServer.java    From nassau with Apache License 2.0 6 votes vote down vote up
/**
 * Create a server. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param clock a clock
 * @param channel the underlying socket channel
 * @param maxPayloadLength the maximum inbound message length
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPServer(Clock clock, SocketChannel channel, int maxPayloadLength,
        MessageListener listener, SoupBinTCPServerStatusListener statusListener) {
    super(clock, channel, Math.max(MIN_MAX_PAYLOAD_LENGTH, maxPayloadLength),
            PACKET_TYPE_SERVER_HEARTBEAT);

    this.loginRequest = new LoginRequest();

    /*
     * This built-in payload transmit buffer is used for Login Accepted
     * and Login Rejected packets.
     */
    this.txPayload = ByteBuffer.allocateDirect(30);

    this.listener = listener;

    this.statusListener = statusListener;
}
 
Example #7
Source File: OrderEntry.java    From parity-extras with Apache License 2.0 5 votes vote down vote up
public static OrderEntry open(InetSocketAddress address) throws IOException {
    SocketChannel channel = SocketChannel.open();

    channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
    channel.connect(address);
    channel.configureBlocking(false);

    MessageListener listener = new POEClientParser(new Listener());

    SoupBinTCPClient transport = new SoupBinTCPClient(channel, POE.MAX_OUTBOUND_MESSAGE_LENGTH,
            listener, new StatusListener());

    return new OrderEntry(transport);
}
 
Example #8
Source File: SoupBinTCP.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Receive messages. Invoke the message listener on each message. Continue
 * until an End of Session packet is received or the end-of-stream is
 * reached.
 *
 * @param address the address
 * @param username the username
 * @param password the password
 * @param listener a message listener
 * @throws IOException if an I/O error occurs
 */
public static void receive(InetSocketAddress address, String username,
        String password, MessageListener listener) throws IOException {
    SocketChannel channel = SocketChannel.open();

    channel.connect(address);
    channel.configureBlocking(false);

    StatusListener statusListener = new StatusListener();

    try (Selector selector = Selector.open();
            SoupBinTCPClient client = new SoupBinTCPClient(channel, listener, statusListener)) {
        channel.register(selector, SelectionKey.OP_READ);

        LoginRequest message = new LoginRequest();

        message.setUsername(username);
        message.setPassword(password);
        message.setRequestedSession("");
        message.setRequestedSequenceNumber(1);

        client.login(message);

        while (statusListener.receive) {
            int numKeys = selector.select(TIMEOUT_MILLIS);

            if (numKeys > 0) {
                if (client.receive() < 0)
                    break;

                selector.selectedKeys().clear();
            }

            client.keepAlive();
        }
    }
}
 
Example #9
Source File: MoldUDP64DefaultMessageStore.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Create a message store.
 */
public MoldUDP64DefaultMessageStore() {
    this.messages = new ArrayList<>();
    this.listener = new MessageListener() {

        @Override
        public void message(ByteBuffer buffer) {
            put(buffer);
        }

    };
}
 
Example #10
Source File: BinaryFILE.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Read messages. Invoke the message listener on each message. Continue
 * until either a payload with length of zero indicating the End of Session
 * is encountered or the end-of-file is reached.
 *
 * @param file a file
 * @param listener a message listener
 * @throws IOException if an I/O error occurs
 */
public static void read(File file, MessageListener listener) throws IOException {
    StatusListener statusListener = new StatusListener();

    BinaryFILEStatusParser statusParser = new BinaryFILEStatusParser(listener, statusListener);

    try (BinaryFILEReader reader = BinaryFILEReader.open(file, statusParser)) {
        while (statusListener.receive && reader.read() >= 0);
    }
}
 
Example #11
Source File: MoldUDP64.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Receive messages. Invoke the message listener on each message. Continue
 * until a packet indicating the End of Session is received.
 *
 * @param multicastInterface the multicast interface
 * @param multicastGroup the multicast group
 * @param requestAddress the request address
 * @param listener a message listener
 * @throws IOException if an I/O error occurs
 */
public static void receive(NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup, InetSocketAddress requestAddress,
        MessageListener listener) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.bind(new InetSocketAddress(multicastGroup.getPort()));
    channel.join(multicastGroup.getAddress(), multicastInterface);
    channel.configureBlocking(false);

    DatagramChannel requestChannel = DatagramChannel.open(StandardProtocolFamily.INET);

    requestChannel.configureBlocking(false);

    StatusListener statusListener = new StatusListener();

    try (Selector selector = Selector.open();
            MoldUDP64Client client = new MoldUDP64Client(channel, requestChannel,
                requestAddress, listener, statusListener)) {
        SelectionKey channelKey = channel.register(selector, SelectionKey.OP_READ);

        SelectionKey requestChannelKey = requestChannel.register(selector, SelectionKey.OP_READ);

        while (statusListener.receive) {
            while (selector.select() == 0);

            Set<SelectionKey> selectedKeys = selector.selectedKeys();

            if (selectedKeys.contains(channelKey))
                client.receive();

            if (selectedKeys.contains(requestChannelKey))
                client.receiveResponse();

            selectedKeys.clear();
        }
    }
}
 
Example #12
Source File: BinaryFILEReader.java    From nassau with Apache License 2.0 5 votes vote down vote up
/**
 * Create a BinaryFILE reader.
 *
 * @param channel an input channel
 * @param listener a message listener
 */
public BinaryFILEReader(ReadableByteChannel channel, MessageListener listener) {
    this.channel  = channel;
    this.listener = listener;

    this.buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
}
 
Example #13
Source File: MoldUDP64Client.java    From nassau with Apache License 2.0 3 votes vote down vote up
MoldUDP64Client(Clock clock, DatagramChannel channel,
        DatagramChannel requestChannel, SocketAddress requestAddress,
        MessageListener listener, MoldUDP64ClientStatusListener statusListener,
        long requestedSequenceNumber) {
    this.clock = clock;

    this.channel = channel;

    this.requestChannel = requestChannel;

    this.requestAddress = requestAddress;

    this.listener = listener;

    this.statusListener = statusListener;

    this.rxBuffer = ByteBuffer.allocateDirect(RX_BUFFER_LENGTH);
    this.txBuffer = ByteBuffer.allocateDirect(HEADER_LENGTH);

    this.session = new byte[SESSION_LENGTH];

    this.nextExpectedSequenceNumber = Math.max(0, requestedSequenceNumber);

    this.requestUntilSequenceNumber = REQUEST_UNTIL_SEQUENCE_NUMBER_UNKNOWN;

    this.requestSentMillis = 0;
}
 
Example #14
Source File: MoldUDP64Client.java    From nassau with Apache License 2.0 3 votes vote down vote up
/**
 * Create a MoldUDP64 client. Use the underlying datagram channel for
 * receiving downstream packets and the underlying request datagram
 * channel for sending request packets. Both the underlying datagram
 * channel and the underlying request datagram channel must be
 * non-blocking.
 *
 * <p>Set the requested initial sequence number to 0 to start from the
 * first received message.</p>
 *
 * @param channel the underlying datagram channel
 * @param requestChannel the underlying request datagram channel
 * @param requestAddress the request address
 * @param listener the message listener
 * @param statusListener the status listener
 * @param requestedSequenceNumber the requested initial sequence number
 */
public MoldUDP64Client(DatagramChannel channel, DatagramChannel requestChannel,
        SocketAddress requestAddress, MessageListener listener,
        MoldUDP64ClientStatusListener statusListener,
        long requestedSequenceNumber) {
    this(System::currentTimeMillis, channel, requestChannel, requestAddress,
            listener, statusListener, requestedSequenceNumber);
}
 
Example #15
Source File: MoldUDP64Client.java    From nassau with Apache License 2.0 3 votes vote down vote up
/**
 * Create a MoldUDP64 client. Use the underlying datagram channel for
 * receiving downstream packets and the underlying request datagram
 * channel for sending request packets. Both the underlying datagram
 * channel and the underlying request datagram channel must be
 * non-blocking.
 *
 * @param channel the underlying datagram channel
 * @param requestChannel the underlying request datagram channel
 * @param requestAddress the request address
 * @param listener the message listener
 * @param statusListener the status listener
 */
public MoldUDP64Client(DatagramChannel channel, DatagramChannel requestChannel,
        SocketAddress requestAddress, MessageListener listener,
        MoldUDP64ClientStatusListener statusListener) {
    this(System::currentTimeMillis, channel, requestChannel, requestAddress,
            listener, statusListener, 1);
}
 
Example #16
Source File: MoldUDP64Client.java    From nassau with Apache License 2.0 3 votes vote down vote up
/**
 * Create a MoldUDP64 client. Use the underlying datagram channel both for
 * receiving downstream packets and sending request packets. The underlying
 * datagram channel can be either blocking or non-blocking.
 *
 * <p>Set the requested initial sequence number to 0 to start from the
 * first received message.</p>
 *
 * @param channel the underlying datagram channel
 * @param requestAddress the request address
 * @param listener the message listener
 * @param statusListener the status listener
 * @param requestedSequenceNumber the requested initial sequence number
 */
public MoldUDP64Client(DatagramChannel channel, SocketAddress requestAddress,
        MessageListener listener, MoldUDP64ClientStatusListener statusListener,
        long requestedSequenceNumber) {
    this(System::currentTimeMillis, channel, channel, requestAddress, listener,
            statusListener, requestedSequenceNumber);
}
 
Example #17
Source File: BinaryFILEReader.java    From nassau with Apache License 2.0 3 votes vote down vote up
/**
 * Open a BinaryFILE reader. The input file can be either uncompressed or
 * compressed with the GZIP file format.
 *
 * @param file the input file
 * @param listener the message listener
 * @return a BinaryFILE reader
 * @throws IOException if an I/O error occurs
 */
public static BinaryFILEReader open(File file, MessageListener listener) throws IOException {
    FileInputStream stream = new FileInputStream(file);

    if (file.getName().endsWith(".gz"))
        return new BinaryFILEReader(new GZIPInputStream(stream, GZIP_BUFFER_SIZE), listener);
    else
        return new BinaryFILEReader(stream.getChannel(), listener);
}
 
Example #18
Source File: MoldUDP64Client.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a MoldUDP64 client. Use the underlying datagram channel both for
 * receiving downstream packets and sending request packets. The underlying
 * datagram channel can be either blocking or non-blocking.
 *
 * @param channel the underlying datagram channel
 * @param requestAddress the request address
 * @param listener the message listener
 * @param statusListener the status listener
 */
public MoldUDP64Client(DatagramChannel channel, SocketAddress requestAddress,
        MessageListener listener, MoldUDP64ClientStatusListener statusListener) {
    this(System::currentTimeMillis, channel, channel, requestAddress, listener,
            statusListener, 1);
}
 
Example #19
Source File: SoupBinTCPClient.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a client. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param channel the underlying socket channel
 * @param maxPayloadLength maximum inbound message length
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPClient(SocketChannel channel, int maxPayloadLength,
        MessageListener listener, SoupBinTCPClientStatusListener statusListener) {
    this(System::currentTimeMillis, channel, maxPayloadLength, listener, statusListener);
}
 
Example #20
Source File: SoupBinTCPClient.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a client. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param channel the underlying socket channel
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPClient(SocketChannel channel, MessageListener listener,
        SoupBinTCPClientStatusListener statusListener) {
    this(System::currentTimeMillis, channel, MAX_PACKET_LENGTH - 1, listener, statusListener);
}
 
Example #21
Source File: BinaryFILEReader.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a BinaryFILE reader.
 *
 * @param stream an input stream
 * @param listener a message listener
 */
public BinaryFILEReader(InputStream stream, MessageListener listener) {
    this(Channels.newChannel(stream), listener);
}
 
Example #22
Source File: SoupBinTCPServer.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a server. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param channel the underlying socket channel
 * @param maxPayloadLength the maximum inbound message length
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPServer(SocketChannel channel, int maxPayloadLength,
        MessageListener listener, SoupBinTCPServerStatusListener statusListener) {
    this(System::currentTimeMillis, channel, maxPayloadLength, listener, statusListener);
}
 
Example #23
Source File: SoupBinTCPServer.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a server. The underlying socket channel can be either blocking
 * or non-blocking.
 *
 * @param channel the underlying socket channel
 * @param listener the inbound message listener
 * @param statusListener the inbound status event listener
 */
public SoupBinTCPServer(SocketChannel channel, MessageListener listener,
        SoupBinTCPServerStatusListener statusListener) {
    this(channel, MAX_PACKET_LENGTH - 1, listener, statusListener);
}
 
Example #24
Source File: BinaryFILEStatusParser.java    From nassau with Apache License 2.0 2 votes vote down vote up
/**
 * Create a parser for status events. The parser passes payloads that do
 * not correspond to status events to the message listener.
 *
 * @param listener a message listener
 * @param statusListener a status listener
 */
public BinaryFILEStatusParser(MessageListener listener, BinaryFILEStatusListener statusListener) {
    this.listener = listener;

    this.statusListener = statusListener;
}