-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.java
94 lines (79 loc) · 2.16 KB
/
Server.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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class Server extends Thread{
public String ip;
public int port;
public ArrayList<Jobs> jobs;
public ArrayList<Connections> workers;
Connections con;
public boolean alive;
DatagramSocket socket;
byte[] buf;
DatagramPacket packet;
DatagramSocket psocket;
int pport;
byte[] pbuf;
DatagramPacket ppacket;
TimerTask task = new TimerTask(){
public void run() {
}
};
public Server(String ip, int port) {
this.ip = ip;
this.port = port;
this.alive = true;
try {
socket = new DatagramSocket(port);
buf = new byte[256];
this.pport = port+1;
psocket = new DatagramSocket(pport);
pbuf = new byte[256];
} catch (SocketException e) {
e.printStackTrace();
}
}
public void run() {
// check for number of connections
while (alive) {
try {
int size = workers.size();
for (int i=0; i<size; i++) {
Timer timer = new Timer();
timer.schedule(task, 5*1000);
this.con = workers.get(i);
String str = "ping";
pbuf = str.getBytes();
InetAddress address = InetAddress.getByName(con.ip);
packet = new DatagramPacket(pbuf, pbuf.length, address, port);
psocket.receive(packet);
timer.cancel();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("please enter the IP for the server");
Scanner in = new Scanner(System.in);
String ip = new String(in.nextLine());
System.out.println("please enter port number");
int port = in.nextInt();
Server server = new Server(ip, port);
Runnable runnable = server;
Thread thread = new Thread(runnable);
thread.start();
while(server.alive) {
// keep listening for UDP packets and divide work and respond accordingly
}
in.close();
thread.join();
}
}