forked from reorder/cgminer_heavy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCast.java
333 lines (279 loc) · 6.07 KB
/
MCast.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
/*
*
* Copyright (C) Andrew Smith 2013
*
* Usage: java MCast [-v] code toaddr port replyport wait
*
* If any are missing or blank they use the defaults:
*
* -v means report how long the last reply took
*
* code = 'FTW'
* toaddr = '224.0.0.75'
* port = '4028'
* replyport = '4027'
* wait = '1000'
*
*/
import java.net.*;
import java.io.*;
import java.util.*;
class MCast implements Runnable
{
static private final String MCAST_CODE = "FTW";
static private final String MCAST_ADDR = "224.0.0.75";
static private final int MCAST_PORT = 4028;
static private final int MCAST_REPORT = 4027;
static private final int MCAST_WAIT4 = 1000;
static private String code = MCAST_CODE;
static private String addr = MCAST_ADDR;
static private int port = MCAST_PORT;
static private int report = MCAST_REPORT;
static private int wait4 = MCAST_WAIT4;
private InetAddress mcast_addr = null;
static private final Integer lock = new Integer(666);
static private boolean ready = false;
static private Thread listen = null;
static public boolean verbose = false;
static private Date start = null;
static private Date last = null;
static boolean got_last = false;
static public void usAge()
{
System.err.println("usAge: java MCast [-v] [code [toaddr [port [replyport [wait]]]]]");
System.err.println(" -v=report elapsed ms to last reply");
System.err.println(" Anything below missing or blank will use it's default");
System.err.println(" code=X in cgminer-X-Port default="+MCAST_CODE);
System.err.println(" toaddr=multicast address default="+MCAST_ADDR);
System.err.println(" port=multicast port default="+MCAST_PORT);
System.err.println(" replyport=local post to listen for replies default="+MCAST_REPORT);
System.err.println(" wait=how long to wait for replies default="+MCAST_WAIT4+"ms");
System.exit(1);
}
private int port(String _port, String name)
{
int tmp = 0;
try
{
tmp = Integer.parseInt(_port);
}
catch (NumberFormatException nfe)
{
System.err.println("Invalid " + name + " - must be a number between 1 and 65535");
usAge();
System.exit(1);
}
if (tmp < 1 || tmp > 65535)
{
System.err.println("Invalid " + name + " - must be a number between 1 and 65535");
usAge();
System.exit(1);
}
return tmp;
}
public void set_code(String _code)
{
if (_code.length() > 0)
code = _code;
}
public void set_addr(String _addr)
{
if (_addr.length() > 0)
{
addr = _addr;
try
{
mcast_addr = InetAddress.getByName(addr);
}
catch (Exception e)
{
System.err.println("ERR: Invalid multicast address");
usAge();
System.exit(1);
}
}
}
public void set_port(String _port)
{
if (_port.length() > 0)
port = port(_port, "port");
}
public void set_report(String _report)
{
if (_report.length() > 0)
report = port(_report, "reply port");
}
public void set_wait(String _wait4)
{
if (_wait4.length() > 0)
{
try
{
wait4 = Integer.parseInt(_wait4);
}
catch (NumberFormatException nfe)
{
System.err.println("Invalid wait - must be a number between 0ms and 60000ms");
usAge();
System.exit(1);
}
if (wait4 < 0 || wait4 > 60000)
{
System.err.println("Invalid wait - must be a number between 0ms and 60000ms");
usAge();
System.exit(1);
}
}
}
public void run() // listen
{
byte[] message = new byte[1024];
DatagramSocket socket = null;
DatagramPacket packet = null;
try
{
socket = new DatagramSocket(report);
packet = new DatagramPacket(message, message.length);
synchronized (lock)
{
ready = true;
}
while (true)
{
socket.receive(packet);
synchronized (lock)
{
last = new Date();
}
int off = packet.getOffset();
int len = packet.getLength();
System.out.println("Got: '" + new String(message, off, len) + "' from" + packet.getSocketAddress());
}
}
catch (Exception e)
{
socket.close();
}
}
public void sendMCast()
{
try
{
String message = new String("cgminer-" + code + "-" + report);
MulticastSocket socket = null;
DatagramPacket packet = null;
socket = new MulticastSocket();
packet = new DatagramPacket(message.getBytes(), message.length(), mcast_addr, port);
System.out.println("About to send " + message + " to " + mcast_addr + ":" + port);
start = new Date();
socket.send(packet);
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void init()
{
MCast lis = new MCast();
listen = new Thread(lis);
listen.start();
while (true)
{
synchronized (lock)
{
if (ready)
break;
}
try
{
Thread.sleep(100);
}
catch (Exception sl1)
{
}
}
try
{
Thread.sleep(500);
}
catch (Exception sl2)
{
}
sendMCast();
try
{
Thread.sleep(wait4);
}
catch (Exception sl3)
{
}
listen.interrupt();
if (verbose)
{
try
{
Thread.sleep(100);
}
catch (Exception sl4)
{
}
synchronized (lock)
{
if (last == null)
System.out.println("No replies received");
else
{
long diff = last.getTime() - start.getTime();
System.out.println("Last reply took " + diff + "ms");
}
}
}
System.exit(0);
}
public MCast()
{
}
public static void main(String[] params) throws Exception
{
int p = 0;
MCast mcast = new MCast();
mcast.set_addr(MCAST_ADDR);
if (params.length > p)
{
if (params[p].equals("-?")
|| params[p].equalsIgnoreCase("-h")
|| params[p].equalsIgnoreCase("-help")
|| params[p].equalsIgnoreCase("--help"))
MCast.usAge();
else
{
if (params[p].equals("-v"))
{
mcast.verbose = true;
p++;
}
if (params.length > p)
{
mcast.set_code(params[p++]);
if (params.length > p)
{
mcast.set_addr(params[p++]);
if (params.length > p)
{
mcast.set_port(params[p++]);
if (params.length > p)
{
mcast.set_report(params[p++]);
if (params.length > p)
mcast.set_wait(params[p]);
}
}
}
}
}
}
mcast.init();
}
}