00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 package com.epam.benchmark.latency;
00042
00043 import com.epam.fix.message.FIXFieldList;
00044 import com.epam.fixengine.*;
00045 import org.apache.commons.logging.Log;
00046 import org.apache.commons.logging.LogFactory;
00047
00048 import java.io.IOException;
00049 import java.util.ArrayList;
00050 import java.util.List;
00051
00052
00053
00054
00055
00056
00057
00058 public class SimpleServer implements FIXServerListener {
00059 private static Log log = LogFactory.getLog(SimpleServer.class);
00060 private List<FIXSession> activeSessions = new ArrayList<FIXSession>();
00061 static FIXServer server;
00062 int count;
00063
00064 public static void main(String[] args) throws IOException {
00065 if (args.length != 1) {
00066 printUsageAndExit();
00067 }
00068
00069 int port = -1;
00070 try {
00071 port = Integer.parseInt(args[0]);
00072 } catch (NumberFormatException e) {
00073 System.out.println("Incorrect port");
00074 printUsageAndExit();
00075 }
00076 init(port);
00077 }
00078
00079 private static void init(int port) throws IOException {
00080 server = new FIXServer();
00081 server.setPort(port);
00082 server.setListener(new SimpleServer());
00083 server.start();
00084 log.info("Started");
00085 }
00086
00087 public void newFIXSession(FIXSession session) {
00088 log.info("New session");
00089 try {
00090 activeSessions.add(session);
00091 session.setFIXSessionListener(new MyFIXSessionListener(session));
00092 session.connect();
00093 } catch (IOException e) {
00094 log.warn(e, e);
00095 }
00096 }
00097
00098 private class MyFIXSessionListener implements FIXSessionListener {
00099 private FIXSession session;
00100
00101 public MyFIXSessionListener(FIXSession session) {
00102 this.session = session;
00103 }
00104
00105 public void onSessionStateChange(SessionState sessionState) {
00106 if (SessionState.DISCONNECTED == sessionState) {
00107 activeSessions.remove(session);
00108 if (activeSessions.isEmpty()) {
00109 try {
00110 server.stop();
00111 log.info("Stopped");
00112 } catch (IOException e) {
00113 e.printStackTrace();
00114 }
00115 System.exit(0);
00116 }
00117 }
00118 }
00119
00120 public void onNewMessage(FIXFieldList message) {
00121 session.sendMessage(message);
00122 }
00123 }
00124
00125 private static void printUsageAndExit() {
00126 System.out.println("SimpleServer port");
00127 System.exit(1);
00128 }
00129 }