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.throughput;
00042
00043 import com.epam.benchmark.Util;
00044 import com.epam.fix.message.FIXFieldList;
00045 import com.epam.fixengine.*;
00046
00047 import java.io.IOException;
00048 import java.util.ArrayList;
00049 import java.util.List;
00050
00051
00052
00053
00054
00055
00056 public class SimpleServer implements FIXServerListener {
00057 private List<FIXSession> activeSessions = new ArrayList<FIXSession>();
00058 private FIXServer server;
00059
00060
00061 private int port;
00062
00063 public SimpleServer(int port) {
00064 this.port = port;
00065 }
00066
00067 public static void main(String[] args) throws IOException {
00068 if (args.length != 1) {
00069 printUsageAndExit();
00070 }
00071
00072 int port = -1;
00073 try {
00074 port = Integer.parseInt(args[0]);
00075 } catch (NumberFormatException e) {
00076 System.out.println("Incorrect port");
00077 printUsageAndExit();
00078 }
00079
00080 new SimpleServer(port).start();
00081 }
00082
00083 private void start() throws IOException {
00084 this.server = new FIXServer();
00085 this.server.setPort(this.port);
00086 this.server.setListener(this);
00087 this.server.start();
00088 System.out.println("Started");
00089 }
00090
00091 public void newFIXSession(FIXSession session) {
00092 System.out.println("New session");
00093 try {
00094 activeSessions.add(session);
00095 session.setFIXSessionListener(new MyFIXSessionListener(session));
00096 session.connect();
00097 } catch (IOException e) {
00098 e.printStackTrace();
00099 }
00100 }
00101
00102
00103 private class MyFIXSessionListener implements FIXSessionListener {
00104 private FIXSession session;
00105 private int messageCount;
00106 private long startTime;
00107
00108 public MyFIXSessionListener(FIXSession session) {
00109 this.session = session;
00110 }
00111
00112
00113 public void onSessionStateChange(SessionState sessionState) {
00114 if (SessionState.DISCONNECTED == sessionState) {
00115 activeSessions.remove(session);
00116 if (activeSessions.isEmpty()) {
00117 try {
00118 server.stop();
00119 System.out.println("Stoped");
00120 } catch (IOException e) {
00121 e.printStackTrace();
00122 }
00123 System.exit(0);
00124 }
00125 }
00126 }
00127
00128 public void onNewMessage(FIXFieldList message) {
00129 messageCount++;
00130 if (messageCount == 1) {
00131
00132 startTime = System.currentTimeMillis();
00133 } else if (messageCount == ThroughputTester.MESSAGES_COUNT) {
00134
00135 final long endTime = System.currentTimeMillis();
00136 printResults(startTime, endTime);
00137 session.disconnect("User request");
00138 }
00139 }
00140
00141 private void printResults(long start, long end) {
00142 long ms = end - start;
00143
00144 Util.writeToOut("Send took:" + ms + " ms");
00145
00146 Util.writeToOut("Throughput:" + (((double) ThroughputTester.MESSAGES_COUNT) / ms * 1000)
00147 + " msg/sec");
00148 }
00149 }
00150
00151 private static void printUsageAndExit() {
00152 System.out.println("SimpleServer port");
00153 System.exit(1);
00154 }
00155 }