static Session open(InetSocketAddress address, FIXConfig config, FIXMessageListener listener, FIXConnectionStatusListener statusListener) throws IOException { SocketChannel channel = SocketChannel.open(); channel.connect(address); channel.configureBlocking(false); Selector selector = Selector.open(); channel.register(selector, SelectionKey.OP_READ); FIXConnection connection = new FIXConnection(channel, config, listener, statusListener); return new Session(selector, connection); }
@Override public void run() { try { while (!closed) { int numKeys = selector.select(TIMEOUT_MILLIS); if (numKeys > 0) { synchronized (lock) { if (connection.receive() < 0) break; } selector.selectedKeys().clear(); } synchronized (lock) { connection.updateCurrentTimestamp(); connection.keepAlive(); } } } catch (IOException e) { } try { connection.close(); } catch (IOException e) { } try { selector.close(); } catch (IOException e) { } }
void send(Message message) throws IOException { synchronized (lock) { connection.updateCurrentTimestamp(); } connection.prepare(txMessage, message.getMsgType()); message.put(txMessage); synchronized (lock) { connection.send(txMessage); } }
private void sendOrderRejected(String clOrdId, int ordRejReason, String account, String symbol, char side, double orderQty) throws IOException { fix.prepare(txMessage, ExecutionReport); Instrument config = instruments.get(symbol); int priceFractionDigits = 0; int sizeFractionDigits = 0; if (config != null) { priceFractionDigits = config.getPriceFractionDigits(); sizeFractionDigits = config.getSizeFractionDigits(); } txMessage.addField(OrderID).setString(UNKNOWN_ORDER_ID); txMessage.addField(ClOrdID).setString(clOrdId); txMessage.addField(ExecID).setString(fix.getCurrentTimestamp()); txMessage.addField(ExecType).setChar(ExecTypeValues.Rejected); txMessage.addField(OrdStatus).setChar(OrdStatusValues.Rejected); txMessage.addField(OrdRejReason).setInt(ordRejReason); if (account != null) txMessage.addField(Account).setString(account); txMessage.addField(Symbol).setString(symbol); txMessage.addField(Side).setChar(side); txMessage.addField(OrderQty).setFloat(orderQty, sizeFractionDigits); txMessage.addField(LeavesQty).setFloat(0.0, sizeFractionDigits); txMessage.addField(CumQty).setFloat(0.0, sizeFractionDigits); txMessage.addField(AvgPx).setFloat(0.0, priceFractionDigits); fix.send(txMessage); }
FIXMessage message = initiator.getTransport().create(); initiator.getTransport().prepare(message, OrderSingle); transactTime.setString(initiator.getTransport().getCurrentTimestamp()); initiator.getTransport().close();
Session(SocketChannel channel) { connection = new FIXConnection(channel, CONFIG, this, new FIXConnectionStatusListener() { report = connection.create(); connection.prepare(report, ExecutionReport);
@Override public void loginRejected(SoupBinTCPClient session, SoupBinTCP.LoginRejected payload) throws IOException { switch (payload.rejectReasonCode) { case SoupBinTCP.LOGIN_REJECT_CODE_NOT_AUTHORIZED: fix.sendLogout("Not authorized"); break; case SoupBinTCP.LOGIN_REJECT_CODE_SESSION_NOT_AVAILABLE: fix.sendLogout("Session not available"); break; } }
connection.sendReject(message.getMsgSeqNum(), 1, "ClOrdID(11) not found"); return; connection.sendReject(message.getMsgSeqNum(), 1, "Symbol(55) not found"); return; connection.sendReject(message.getMsgSeqNum(), 1, "OrderQty(38) not found"); return; connection.sendReject(message.getMsgSeqNum(), 1, "Price(44) not found"); return; leavesQty.set(orderQty); connection.update(report); connection.send(report);
/** * <p>Prepare a message.</p> * * @param message a message * @param msgType the MsgType(35) * @see #prepare(FIXMessage, char) */ public void prepare(FIXMessage message, CharSequence msgType) { message.reset(); message.addField(MsgType).setString(msgType); prepare(message); }
private void invalidMsgType(FIXMessage message) throws IOException { fix.sendReject(message.getMsgSeqNum(), SessionRejectReasonValues.InvalidMsgType, "Invalid MsgType(35)"); }
@Override public void heartbeatTimeout(FIXConnection connection) throws IOException { connection.close(); }
continue; session.getFIX().getChannel().register(selector, SelectionKey.OP_READ, new Receiver() { Session session = toKeepAlive.get(i); session.getFIX().updateCurrentTimestamp(); session.getFIX().keepAlive(); } catch (IOException e) { toCleanUp.add(session);
void receive() throws IOException { while (!received) { if (connection.receive() < 0) return; } received = false; }
private Session(Selector selector, FIXConnection connection) { this.txMessage = connection.create(); this.selector = selector; this.connection = connection; this.closed = false; this.lock = new Object(); Thread receiver = new Thread(new Receiver()); receiver.setDaemon(true); receiver.start(); }
private void sendOrderAccepted(Order order) throws IOException { fix.prepare(txMessage, ExecutionReport); String symbol = order.getSymbol(); Instrument config = instruments.get(symbol); int priceFractionDigits = config.getPriceFractionDigits(); int sizeFractionDigits = config.getSizeFractionDigits(); txMessage.addField(OrderID).setInt(order.getOrderID()); txMessage.addField(ClOrdID).setString(order.getClOrdID()); txMessage.addField(ExecID).setString(fix.getCurrentTimestamp()); txMessage.addField(ExecType).setChar(ExecTypeValues.New); txMessage.addField(OrdStatus).setChar(order.getOrdStatus()); if (order.getAccount() != null) txMessage.addField(Account).setString(order.getAccount()); txMessage.addField(Symbol).setString(symbol); txMessage.addField(Side).setChar(order.getSide()); txMessage.addField(OrderQty).setFloat(order.getOrderQty(), sizeFractionDigits); txMessage.addField(LeavesQty).setFloat(order.getLeavesQty(), sizeFractionDigits); txMessage.addField(CumQty).setFloat(order.getCumQty(), sizeFractionDigits); txMessage.addField(AvgPx).setFloat(order.getAvgPx(), priceFractionDigits); fix.send(txMessage); }
@Override public void endOfSession(SoupBinTCPClient session) throws IOException { fix.sendLogout(); }
/** * <p>Prepare a message.</p> * * @param message a message * @param msgType the MsgType(35) * @see #prepare(FIXMessage, char) */ public void prepare(FIXMessage message, CharSequence msgType) { message.reset(); message.addField(MsgType).setString(msgType); prepare(message); }
private void incorrectDataFormatForValue(FIXMessage message, String text) throws IOException { fix.sendReject(message.getMsgSeqNum(), SessionRejectReasonValues.IncorrectDataFormatForValue, text); }