/
MarketPrice.java
210 lines (184 loc) · 7.65 KB
/
MarketPrice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//|-----------------------------------------------------------------------------
//| This source code is provided under the Apache 2.0 license --
//| and is provided AS IS with no warranty or guarantee of fit for purpose. --
//| See the project's LICENSE.md for details. --
//| Copyright (C) 2017-2020 Refinitiv. All rights reserved. --
//|-----------------------------------------------------------------------------
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketExtension;
import com.neovisionaries.ws.client.WebSocketFactory;
import com.neovisionaries.ws.client.WebSocketFrame;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import org.json.*;
import org.apache.commons.cli.*;
public class MarketPrice {
public static String server;
public static String hostname = "127.0.0.1";
public static String port = "15000";
public static String user = "root";
public static String position = "";
public static String appId = "256";
public static WebSocket ws = null;
public static boolean snapshot = false;
public static void main(String[] args) {
Options options = new Options();
options.addOption(Option.builder().longOpt("hostname").hasArg().desc("hostname").build());
options.addOption(Option.builder().longOpt("port").hasArg().desc("port").build());
options.addOption(Option.builder().longOpt("app_id").hasArg().desc("app_id").build());
options.addOption(Option.builder().longOpt("user").hasArg().desc("user").build());
options.addOption(Option.builder().longOpt("position").hasArg().desc("position").build());
options.addOption(Option.builder().longOpt("snapshot").desc("snapshot").build());
options.addOption(Option.builder().longOpt("help").desc("help").build());
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("MarketPrice", options);
System.exit(1);
return;
}
if(cmd.hasOption("help"))
{
formatter.printHelp("MarketPrice", options);
System.exit(0);
}
if(cmd.hasOption("hostname"))
hostname = cmd.getOptionValue("hostname");
if(cmd.hasOption("port"))
port = cmd.getOptionValue("port");
if(cmd.hasOption("app_id"))
appId = cmd.getOptionValue("app_id");
if(cmd.hasOption("user"))
user = cmd.getOptionValue("user");
if(cmd.hasOption("position"))
{
position = cmd.getOptionValue("position");
}
else
{
try {
position = Inet4Address.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
if(cmd.hasOption("snapshot"))
{
snapshot = true;
}
server = String.format("ws://%s:%s/WebSocket", hostname, port);
System.out.println("Connecting to WebSocket " + server + " ...");
try {
ws = connect();
while(true) {
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (WebSocketException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Connect to the server.
*/
public static WebSocket connect() throws IOException, WebSocketException
{
return new WebSocketFactory()
//.setConnectionTimeout(TIMEOUT)
.createSocket(server)
.addProtocol("tr_json2")
.addListener(new WebSocketAdapter() {
/**
* Called when message received, parse message into JSON for processing
*/
public void onTextMessage(WebSocket websocket, String message) throws JSONException {
if(!message.isEmpty()) {
System.out.println("RECEIVED:");
JSONArray jsonArray = new JSONArray(message);
System.out.println(jsonArray.toString(2));
for (int i = 0; i < jsonArray.length(); ++i)
processMessage(websocket, jsonArray.getJSONObject(i));
}
}
/**
* Called when handshake is complete and websocket is open, send login
*/
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws JSONException {
System.out.println("WebSocket successfully connected!");
sendLoginRequest(websocket);
}
})
.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE)
.connect();
}
/**
* Generate a login request from command line data (or defaults) and send
* @param websocket
* @throws JSONException
*/
public static void sendLoginRequest(WebSocket websocket) throws JSONException {
String loginJsonString = "{\"ID\":1,\"Domain\":\"Login\",\"Key\":{\"Name\":\"\",\"Elements\":{\"ApplicationId\":\"\",\"Position\":\"\"}}}";
JSONObject loginJson = new JSONObject(loginJsonString);
loginJson.getJSONObject("Key").put("Name", user);
loginJson.getJSONObject("Key").getJSONObject("Elements").put("ApplicationId", appId);
loginJson.getJSONObject("Key").getJSONObject("Elements").put("Position", position);
websocket.sendText(loginJson.toString());
System.out.println("SENT:\n" + loginJson.toString(2));
}
/**
* Parse at high level and output JSON of message
* @param websocket
* @param messageJson
* @throws JSONException
*/
public static void processMessage(WebSocket websocket, JSONObject messageJson) throws JSONException {
String messageType = messageJson.getString("Type");
switch(messageType)
{
case "Refresh":
if(messageJson.has("Domain")) {
String messageDomain = messageJson.getString("Domain");
if(messageDomain.equals("Login")) {
sendRequest(websocket);
}
}
break;
case "Ping":
String pongJsonString = "{\"Type\":\"Pong\"}";
JSONObject pongJson = new JSONObject(pongJsonString);
websocket.sendText(pongJsonString);
System.out.println("SENT:\n" + pongJson.toString(2));
break;
default:
break;
}
}
/**
* Create and send simple Market Price request
* @param websocket
* @throws JSONException
*/
public static void sendRequest(WebSocket websocket) throws JSONException {
String requestJsonString = "";
requestJsonString = "{"
+ "\"ID\":2,"
+ "\"Key\":{\"Name\":\"TRI.N\"},"
+ "\"Streaming\":" + Boolean.toString(!snapshot)
+ "}";
JSONObject mpRequestJson = new JSONObject(requestJsonString);
websocket.sendText(requestJsonString);
System.out.println("SENT:\n" + mpRequestJson.toString(2));
}
}