-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
126 lines (95 loc) · 2.7 KB
/
main.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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
import config.config;
import network.*;
import peerTable.*;
import services.*;
import self.Self;
public class main {
public static void main(String[] args) {
Self.init();
String ID = Self.getId();
DatagramSocket mySocket= null;
try {
mySocket = new DatagramSocket(4242);
//mySocket.setBroadcast(true);
} catch (SocketException e1) {
System.out.println("Can not bind socket");
e1.printStackTrace();
}
SimpleMessageHandler[] handlers = new SimpleMessageHandler[1];
handlers[0]= new HelloHandler();
//handlers[1]= new HelloHandler();
//handlers[2]= new DebugHandler();
//handlers[1]= new SynHandler();
//handlers[2]= new GlobalListHandler();
PeerTable table = new PeerTable(config.helloInt);
MuxDemux dm = new MuxDemux(handlers, mySocket);
dm.ID=ID;
table.setMux(dm);
HelloSender helloSender =new HelloSender(dm);
helloSender.table =table;
//ListSender listSender =new ListSender();
//listSender.table =table;
/* to add
FileHandler fh = new FileHandler();
fh.table=table;
ServingService ss = new ServingService();
ss.f=fh;
*/
//DownloadService dw = new DownloadService();
//dw.table=table;
//table.dw=dw;
//Thread dwt = new Thread(dw);
//dwt.start();
/*
* Thread sst =new Thread(ss);
Thread fht =new Thread(fh);
sst.start();
fht.start();
*/
//((SynHandler)handlers[1]).lst= listSender;
//((SynHandler)handlers[1]).ID= ID;
((HelloHandler)handlers[0]).ID= ID;
((HelloHandler)handlers[0]).table = table;
dm.table=table;
new Thread(handlers[0]).start();
//new Thread(handlers[1]).start();
//new Thread(handlers[2]).start();
//new Thread(handlers[3]).start();
//new Thread(handlers[4]).start();
new Thread(dm).start();
HTTPServer http = new HTTPServer();
http.table = table;
http.start();
while (true) {
String payload;
try {
payload = dm.getNext();
byte[] byteArray = payload.getBytes();
try {
DatagramPacket dp = new DatagramPacket(byteArray, byteArray.length, InetAddress.getByName("255.255.255.255"), 4242);
try {
mySocket.send(dp);
if (config.pmessageOut)System.out.println("Message send : "+payload);
//System.out.println("Message send : ");
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}