-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloombergServer.java
More file actions
120 lines (107 loc) · 4.55 KB
/
BloombergServer.java
File metadata and controls
120 lines (107 loc) · 4.55 KB
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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.concurrent.*;
import java.util.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedReader;
public class BloombergServer {
private DatagramSocket udpSocket;
private int port;
private ArrayList<Data> bloomberg = new ArrayList<>();
private int ttl = 3;
private int cur = 0;
private HashSet<Integer> bought = new HashSet<>();
public BloombergServer(int port) throws SocketException, IOException {
this.port = port;
this.udpSocket = new DatagramSocket(this.port);
}
private void listen() throws Exception {
System.out.println("-- Running Bloomberg at " + InetAddress.getLocalHost() + "--");
String msg;
while (cur < bloomberg.size() + ttl - 1) {
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
// blocks until a packet is received
udpSocket.receive(packet);
msg = new String(packet.getData()).trim();
String[] split = msg.split("-");
String reply = "";
System.out.println(
"[" + System.currentTimeMillis() + "]" + " Request from " +
packet.getAddress().getHostAddress() + ": " + msg);
if (split[0].charAt(0) == 'b') {
int num = Integer.parseInt(split[1]);
if (cur - ttl + 1 > num) {
reply = "fail";
System.out.println("[" + System.currentTimeMillis() + "]" + " Buy failed, expired " + (cur-ttl+1-num) + " ago" );
} else {
reply = "success";
System.out.println("[" + System.currentTimeMillis() + "]" + " Buy successful");
}
} else if (split[0].charAt(0) == 's') {
int num = Integer.parseInt(split[1]);
if (cur - ttl + 1 > num) {
reply = "fail";
System.out.println("[" + System.currentTimeMillis() + "]" + " Sell failed, expired " + (cur-ttl+1-num) + " ago");
} else {
reply = "success";
System.out.println("[" + System.currentTimeMillis() + "]" + " Sell successful");
}
} else {
reply = "nope";
System.out.println("[" + System.currentTimeMillis() + "]" + " Do nothing");
}
System.out.println("--------------------------------");
byte[] rep = reply.getBytes();
DatagramPacket resp = new DatagramPacket(
rep, rep.length, packet.getAddress(), packet.getPort());
this.udpSocket.send(resp);
byte[] message;
if (cur < bloomberg.size()) {
message = bloomberg.get(cur).toString().getBytes();
} else {
message = "None".getBytes();
}
DatagramPacket first = new DatagramPacket(
message, message.length, packet.getAddress(), packet.getPort());
cur++;
this.udpSocket.send(first);
}
}
private ArrayList<Data> readDataFromCsv(String file) {
ArrayList<Data> data = new ArrayList<>();
Path pathToFile = Paths.get(file);
try (
BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.UTF_8)) {
String line = br.readLine();
while (line != null) {
String[] attributes = line.split(",");
attributes[0] = attributes[0].trim();
if (attributes[0].charAt(0) == '\uFEFF') {
attributes[0] = attributes[0].substring(1);
}
data.add(new Data(attributes[0], Double.parseDouble(attributes[3]), Double.parseDouble(attributes[2]), 3));
line = br.readLine();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return data;
}
private void generateData(String file) {
bloomberg = readDataFromCsv(file);
}
public static void main(String[] args) throws Exception {
BloombergServer client = new BloombergServer(8001);
client.generateData(args[0]);
client.listen();
System.out.println("[" + System.currentTimeMillis() + "]" + " No more data");
}
}