Samples :: Dynamic Acceptor

Dynamic Acceptor Sample

Description

This is a Dynamic FIX Acceptor that waits for incoming connection and accepting FIX session without a prior creation of session object.

Usage

  • Run the sample:
    • win: 1-DynamicAcceptor.bat
    • linux: 1-DynamicAcceptor.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.engine.SessionId;
import biz.onixs.fix.engine.storage.SessionStorageType;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.Utils;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
/**
 * This is a Dynamic FIX Acceptor that waits for incoming connection and accepting
 * FIX session without a prior creation of session object or rejects it.
 */
public class DynamicAcceptor implements Engine.DynamicAcceptorListener, Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(DynamicAcceptor.class);
    private static final String SETTINGS_RESOURCE = "sample/DynamicAcceptor.properties";
    private Settings settings = null;
  
    @Override
    public void run() {
        try {
            LOG.info("DynamicAcceptor");
            LOG.info("The application is starting...");
            //
            LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
            settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
            //
            LOG.info("Starting the Engine...");
            final Engine engine = Engine.init(settings);
            //
            LOG.info("Register dynamic acceptor listener ...");
            //
            engine.addDynamicAcceptorListener(this);
            //
            Utils.waitForEnterToTerminateApp();
            //
            LOG.info("Engine shutdown...");
            engine.shutdown();
        catch (final RuntimeException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
  
    @Override
    public void onDynamicAcceptor(final Object sender, final Engine.DynamicAcceptorArgs args) {
        final SessionId sessionId = args.getSessionId();
        LOG.info("Dynamic acceptor: {}, logon message {}", sessionId, args.getIncomingLogonMessage());
        if ("CorrectPassword".equals(args.getIncomingLogonMessage().getFlatMessage().get(Tag.Password))) {
            final Session session = new Session(sessionId,
                    settings.getBoolean("keepSequenceNumbersBetweenFixConnections"), SessionStorageType.FileBasedStorage);
            LOG.info("Session created: {}", session);
            args.setCreatedSession(session);
        } else {
            args.setRejectReason("Face control failed");
        }
    }
  
    private static class RejectingLogic implements Engine.DynamicAcceptorListener {
  
        @Override
        public void onDynamicAcceptor(final Object sender, final Engine.DynamicAcceptorArgs args) {
            final SessionId sessionId = args.getSessionId();
            LOG.info("Dynamic acceptor: {}, logon message {}", sessionId, args.getIncomingLogonMessage());
        }
    }
  
    public static void main(final String[] args) {
        (new DynamicAcceptor()).run();
    }
}

Accepted Buy Side Sample

Description

This is a FIX initiator that connects to the pre-defined host and port. It will be accepted by dynamic acceptor.

Usage

  • Run the sample:
    • win: 2-AcceptedBuySide.bat
    • linux: 2-AcceptedBuySide.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.engine.SessionState;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.FIX40;
import biz.onixs.fix.tag.FIX44;
import biz.onixs.util.TimestampFormat;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.util.concurrent.Semaphore;
  
/**
 * FIX Initiator - Buy Side which will be accepted by dynamic acceptor.
 */
public class AcceptedBuySide implements Session.StateChangeListener, Session.InboundSessionMessageListener,
        Session.WarningListener, Session.ErrorListener, Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(AcceptedBuySide.class);
    private static final String SETTINGS_RESOURCE = "sample/BuySide.properties";
    private final Semaphore sessionIsEstablished = new Semaphore(0);
    private Settings settings = null;
    private Version fixVersion = null;
    private Session session = null;
  
    public void run() {
        try {
            LOG.info("AcceptedBuySide");
            LOG.info("The application is starting...");
            //
            LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
            settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
            //
            LOG.info("Starting the Engine...");
            final Engine engine = Engine.init(settings);
            //
            createSession();
            //
            establishConnection();
            //
            LOG.info("Waiting for session establishment...");
            sessionIsEstablished.acquire();
            //
            session.logout("The session is disconnected by AcceptedBuySide");
            session.dispose();
            //
            LOG.info("Engine shutdown...");
            engine.shutdown();
        } catch (final Exception e) {
            LOG.error(e.getMessage(), e);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
  
    private void createSession() {
        fixVersion = Version.getByNumber(settings.getString("FIXVersion"));
        session = new Session("Accepted" + settings.getString("SenderCompID"),
                settings.getString("TargetCompID"), fixVersion,
                settings.getBoolean("keepSequenceNumbersBetweenFixConnections"));
        //
        session.setSenderSubID("SenderSubID (50) field")
                .setSendingTimeFormat(TimestampFormat.YYYYMMDDHHMMSSMsec)
                .setInboundSessionMessageListener(this)
                .addStateChangeListener(this)
                .setErrorListener(this)
                .setWarningListener(this);
    }
  
    private void establishConnection() {
        final String host = settings.getString("CounterpartyHost");
        final int port = settings.getInteger("CounterpartyPort");
        final Message logon = Message.create(FIX40.MsgType.Logon, fixVersion);
        logon.set(FIX44.Tag.Password, "CorrectPassword");
        LOG.info("Establishing connection to {}:{}...", host, port);
        session.logonAsInitiator(host, port, true, logon);
    }
  
    @Override
    public void onInboundSessionMessage(final Object sender, final Session.InboundSessionMessageArgs args) {
        final Message message = args.getMsg();
        LOG.info("Incoming session-level message: {}", message);
        // Processing of the incoming session-level message...
    }
  
    @Override
    public void onStateChange(final Object sender, final Session.StateChangeArgs args) {
        final SessionState newState = args.getNewState();
        final SessionState prevState = args.getPrevState();
        LOG.info("Session state changed from {} to {}", prevState, newState);
        if (SessionState.ESTABLISHED == newState) {
            sessionIsEstablished.release();
        }
    }
  
    @Override
    public void onError(final Object sender, final Session.ErrorArgs args) {
        LOG.error("{}", args);
    }
  
    @Override
    public void onWarning(final Object sender, final Session.WarningArgs args) {
        LOG.warn("{}", args);
    }
  
    public static void main(final String[] args) {
        (new AcceptedBuySide()).run();
    }
}

Rejected Buy Side Sample

Description

This is a FIX initiator that connects to the pre-defined host and port. It will be rejected by dynamic acceptor.

Usage

  • Run the sample:
    • win: 2-RejectedBuySide.bat
    • linux: 2-RejectedBuySide.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.LinkErrorException;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.engine.SessionState;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.FIX40;
import biz.onixs.fix.tag.FIX44;
import biz.onixs.util.TimestampFormat;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.util.concurrent.Semaphore;
  
/**
 * FIX Initiator - Buy Side which will be rejected by dynamic acceptor.
 */
public class RejectedBuySide implements Session.StateChangeListener, Session.InboundSessionMessageListener, 
        Session.WarningListener, Session.ErrorListener, Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(RejectedBuySide.class);
    private static final String SETTINGS_RESOURCE = "sample/BuySide.properties";
    private final Semaphore sessionIsDisconnected = new Semaphore(0);
    private Settings settings = null;
    private Version fixVersion = null;
    private Session session = null;
  
    public void run() {
        try {
            LOG.info("RejectedBuySide");
            LOG.info("The application is starting...");
            //
            LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
            settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
            //
            LOG.info("Starting the Engine...");
            final Engine engine = Engine.init(settings);
            //
            createSession();
            //
            establishConnection();
            //
            LOG.info("Waiting for session establishment...");
            sessionIsDisconnected.acquire();
            //
            session.logout("The session is disconnected by RejectedBuySide");
            session.dispose();
            //
            LOG.info("Engine shutdown...");
            engine.shutdown();
        } catch (final Exception e) {
            LOG.error(e.getMessage(), e);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
  
    private void createSession() {
        fixVersion = Version.getByNumber(settings.getString("FIXVersion"));
        session = new Session("Rejected" + settings.getString("SenderCompID"),
                settings.getString("TargetCompID"), fixVersion,
                settings.getBoolean("keepSequenceNumbersBetweenFixConnections"));
        //
        session.setSenderSubID("SenderSubID (50) field")
                .setSendingTimeFormat(TimestampFormat.YYYYMMDDHHMMSSMsec)
                .setInboundSessionMessageListener(this)
                .addStateChangeListener(this)
                .setErrorListener(this)
                .setWarningListener(this);
    }
  
    private void establishConnection() {
        final String host = settings.getString("CounterpartyHost");
        final int port = settings.getInteger("CounterpartyPort");
        final Message logon = Message.create(FIX40.MsgType.Logon, fixVersion);
        logon.set(FIX44.Tag.Password, "IncorrectPassword");
        LOG.info("Establishing connection to {}:{}...", host, port);
        try {
            session.logonAsInitiator(host, port, true, logon);
        } catch (final LinkErrorException e) {
            LOG.info("The logon rejected.");
        }
    }
  
    @Override
    public void onInboundSessionMessage(final Object sender, final Session.InboundSessionMessageArgs args) {
        final Message message = args.getMsg();
        LOG.info("Incoming session-level message: {}", message);
        // Processing of the incoming session-level message...
    }
  
    @Override
    public void onStateChange(final Object sender, final Session.StateChangeArgs args) {
        final SessionState newState = args.getNewState();
        final SessionState prevState = args.getPrevState();
        LOG.info("Session state changed from {} to {}", prevState, newState);
        if (SessionState.DISCONNECTED == newState) {
            sessionIsDisconnected.release();
        }
    }
  
    @Override
    public void onError(final Object sender, final Session.ErrorArgs args) {
        LOG.error("{}", args);
    }
  
    @Override
    public void onWarning(final Object sender, final Session.WarningArgs args) {
        LOG.warn("{}", args);
    }
  
    public static void main(final String[] args) {
        (new RejectedBuySide()).run();
    }
}