-
Notifications
You must be signed in to change notification settings - Fork 67
/
WallServer.java
351 lines (311 loc) · 11.9 KB
/
WallServer.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* The "Most Pixels Ever" Wallserver.
* This server can accept two values from the command line:
* -port<port number> Defines the port. Defaults to 9002
* -ini<Init file path> File path to mpeServer.ini. Defaults to directory of server.
* @author Shiffman and Kairalla
*
*/
package mpe.server;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class WallServer {
private ArrayList<Connection> connections = new ArrayList<Connection>();
private int port;
private boolean running = false;
public boolean[] connected; // When the clients connect, they switch their slot to true
public boolean[] ready; // When the clients are ready for next frame, they switch their slot to true
public boolean allConnected = false; // When true, we are off and running
int frameCount = 0;
private long before;
// Server will add a message to the frameEvent
public boolean newMessage = false;
public String message = "";
// Server can send a byte array!
public boolean newBytes = false;
public byte[] bytes = null;
// Server can send an int array!
public boolean newInts = false;
public int[] ints = null;
public boolean dataload = false;
// Back door connection stuff
ListenerConnection backDoorConnection = null;
private static boolean listener = false;
private int listenPort;
private boolean backDoorConnected = false;
BackDoor backdoor;
public WallServer(int _screens, int _framerate, int _port, int _listenPort) {
mpePrefs.setScreens(_screens);
mpePrefs.setFramerate(_framerate);
port = _port;
listenPort = _listenPort;
out("framerate = " + mpePrefs.FRAMERATE + ", screens = " + mpePrefs.SCREENS + ", debug = " + mpePrefs.DEBUG);
connected = new boolean[mpePrefs.SCREENS]; // default to all false
ready = new boolean[mpePrefs.SCREENS]; // default to all false
}
public void run() {
running = true;
if (listener) startListener();
before = System.currentTimeMillis(); // Getting the current time
ServerSocket frontDoor;
try {
frontDoor = new ServerSocket(port);
System.out.println("Starting server: " + InetAddress.getLocalHost() + " " + frontDoor.getLocalPort());
// Wait for connections (could thread this)
while (running) {
Socket socket = frontDoor.accept(); // BLOCKING!
System.out.println(socket.getRemoteSocketAddress() + " connected.");
// Make and start connection object
Connection conn = new Connection(socket,this);
conn.start();
// Add to list of connections
connections.add(conn);
}
} catch (IOException e) {
System.out.println("Zoinks!" + e);
}
}
// Synchronize?!!!
public synchronized void triggerFrame() {
if (frameCount % 10 == 0) {
//System.out.println("Framecount: " + frameCount);
}
// We can't go on if the server is still loading data from a client
/*while (dataload) {
System.out.println("Data loading!");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
int desired = (int) ((1.0f / (float) mpePrefs.FRAMERATE) * 1000.0f);
long now = System.currentTimeMillis();
int diff = (int) (now - before);
if (diff < desired) {
// Where do we max out a framerate? Here?
try {
long sleepTime = desired-diff;
if (sleepTime >= 0){
Thread.sleep(sleepTime);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
long sleepTime = 2;
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Reset everything to false
for (int i = 0; i < ready.length; i++) {
ready[i] = false;
}
frameCount++;
String send = "G,"+(frameCount-1);
// Adding a data message to the frameEvent
//substring removes the ":" at the end.
if (newMessage) send += ":" + message.substring(0, message.length()-1);
newMessage = false;
message = "";
if (newBytes) {
send = "B" + send;
sendAll(send);
sendAllBytes();
newBytes = false;
} else if (newInts) {
send = "I" + send;
sendAll(send);
sendAllInts();
newInts = false;
} else {
sendAll(send);
}
before = System.currentTimeMillis();
}
private void print(String string) {
System.out.println("MPEServer: "+string);
}
public synchronized void sendAll(String msg){
//System.out.println("Sending " + msg + " to clients: " + connections.size());
for (int i = 0; i < connections.size(); i++){
Connection conn = connections.get(i);
conn.send(msg);
}
}
public synchronized void sendAllBytes(){
//System.out.println("Sending " + msg + " to clients: " + connections.size());
for (int i = 0; i < connections.size(); i++){
Connection conn = connections.get(i);
conn.sendBytes(bytes);
}
}
public synchronized void sendAllInts(){
//System.out.println("Sending " + msg + " to clients: " + connections.size());
for (int i = 0; i < connections.size(); i++){
Connection conn = connections.get(i);
conn.sendInts(ints);
}
}
public void killConnection(Connection conn){
connections.remove(conn);
}
boolean allDisconected(){
if (connections.size() < 1){
return true;
} else return false;
}
void resetFrameCount(){
frameCount = 0;
newMessage = false;
message = "";
print ("resetting frame count.");
}
public void killServer() {
running = false;
}
public static void main(String[] args) {
// set default values
int screens = 2;
int framerate = 30;
int port = 9002;
int listenPort = 9003;
boolean help = false;
// see if info is given on the command line
for (int i = 0; i < args.length; i++) {
if (args[i].contains("-screens")) {
args[i] = args[i].substring(8);
try{
screens = Integer.parseInt(args[i]);
} catch (Exception e) {
out("ERROR: I can't parse the # of screens " + args[i] + "\n" + e);
help = true;
}
}
else if (args[i].contains("-framerate")) {
args[i] = args[i].substring(10);
try{
framerate = Integer.parseInt(args[i]);
} catch (Exception e) {
out("ERROR: I can't parse the frame rate " + args[i] + "\n" + e);
help = true;
}
}
else if (args[i].contains("-port")) {
args[i] = args[i].substring(5);
try {
port = Integer.parseInt(args[i]);
} catch (Exception e) {
out("ERROR: I can't parse the port number " + args[i] + "\n" + e);
help = true;
}
}
else if (args[i].contains("-listener")){
listener = true;
}
else if (args[i].contains("-listenPort")) {
args[i] = args[i].substring(11);
try{
listenPort = Integer.parseInt(args[i]);
} catch (Exception e) {
out("ERROR: I can't parse the listening port number " + args[i] + "\n" + e);
help = true;
}
}
else if (args[i].contains("-debug")) {
mpePrefs.DEBUG = true;
}
else {
help = true;
}
}
if (help) {
// if anything unrecognized is sent to the command line, help is
// displayed and the server quits
System.out.println(" * The \"Most Pixels Ever\" Wallserver.\n" +
" * This server can accept the following parameters from the command line:\n" +
" * -screens<number of screens> Total # of expected clients. Defaults to 2\n" +
" * -framerate<framerate> Desired frame rate. Defaults to 30\n" +
" * -port<port number> Defines the port. Defaults to 9002\n" +
" * -listener Turns on an optional port listener so that other apps can send data to the screens.\n" +
" * -listenPort<port number> Defines listening port. Defaults to 9003.\n" +
" * -debug Turns debugging messages on.\n" +
" * -ini<INI file path.> Path to initialization file. Defaults to \"mpeServer.ini\".\n" +
" Please note the use of an INI file with the server is now deprecated.");
System.exit(1);
}
else {
WallServer ws = new WallServer(screens, framerate, port, listenPort);
ws.run();
}
}
private static void out(String s){
System.out.println("WallServer: "+ s);
}
public void drop(int i) {
connected[i] = false;
ready[i] = false;
}
// synchronize??
public synchronized void setReady(int clientID) {
ready[clientID] = true;
if (isReady()) triggerFrame();
}
// synchronize?
public synchronized boolean isReady() {
boolean allReady = true;
for (int i = 0; i < ready.length; i++){ //if any are false then wait
if (ready[i] == false) allReady = false;
}
return allReady;
}
//********************** BACKDOOR LISTENER METHODS **********************************
private void startListener(){
backdoor = new BackDoor(this);
Thread t= new Thread(backdoor);
t.start();
}
public void killListenerConnection(){
backDoorConnected = false;
}
class BackDoor implements Runnable{
WallServer parent;
BackDoor(WallServer _parent){
parent = _parent;
}
public void run() {
ServerSocket backDoor;
ListenerConnection backDoorConnection;
try {
backDoor= new ServerSocket(listenPort);
System.out.println("Starting backdoor Listener: " + InetAddress.getLocalHost() + " " + backDoor.getLocalPort());
while (running) {
if (!backDoorConnected){
System.out.println("Waiting for backdoor connection");
Socket socket = backDoor.accept(); // BLOCKING!
System.out.println("backdoor port "+socket.getRemoteSocketAddress() + " connected.");
// Make and start connection object
backDoorConnection = new ListenerConnection(socket,parent);
backDoorConnection.start();
backDoorConnected= true;
} else {
Thread.currentThread().sleep(500);
}
}
} catch (IOException e) {
System.out.println("Zoinks, Backdoor Style!" + e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}