root / src / peer / Peer.java @ 1
History | View | Annotate | Download (5.35 KB)
1 |
package peer; |
---|---|
2 |
|
3 |
import channels.McChannel; |
4 |
import channels.MdbChannel; |
5 |
import protocols.BackupProtocol; |
6 |
import protocols.RestoreProtocol; |
7 |
import utils.Utils; |
8 |
|
9 |
import java.io.File; |
10 |
import java.io.IOException; |
11 |
import java.net.*; |
12 |
import java.security.NoSuchAlgorithmException; |
13 |
|
14 |
import utils.*; |
15 |
|
16 |
public class Peer { |
17 |
|
18 |
private static int senderId; //id of the server that has sent the message |
19 |
|
20 |
private static InetAddress mcChannel; |
21 |
private static InetAddress mdbChannel; |
22 |
private static InetAddress mdrChannel; |
23 |
|
24 |
private static int mcPort; |
25 |
private static int mdbPort; |
26 |
private static int mdrPort; |
27 |
|
28 |
|
29 |
|
30 |
//BACKUP, DELETE, RECLAIM, RESTORE;
|
31 |
|
32 |
|
33 |
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InterruptedException { |
34 |
if(!parseArgs(args)) return; |
35 |
|
36 |
joinMulticastGroup(); |
37 |
startThreads(); |
38 |
getRequests(); |
39 |
|
40 |
} |
41 |
|
42 |
private static void getRequests() throws IOException, NoSuchAlgorithmException, InterruptedException { |
43 |
DatagramSocket request = new DatagramSocket(); |
44 |
int portNumber = request.getLocalPort();
|
45 |
System.out.println("Socket Peer opened in port number: " + portNumber); |
46 |
|
47 |
while(true){ |
48 |
byte[] buffer = new byte[Utils.BUFFER_SIZE]; |
49 |
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); |
50 |
|
51 |
request.receive(packet); |
52 |
|
53 |
InetAddress clientAddress = packet.getAddress();
|
54 |
String requestString = new String(packet.getData(), 0, packet.getLength()); |
55 |
|
56 |
System.out.println();
|
57 |
System.out.println("---------------------------------------------------------------------------"); |
58 |
System.out.println("Request from TestApp: " + requestString); |
59 |
System.out.println();
|
60 |
|
61 |
String[] requestArgs = requestString.split("\\s+"); |
62 |
|
63 |
switch(requestArgs[0]){ |
64 |
case "BACKUP": |
65 |
new BackupProtocol(requestArgs);
|
66 |
break;
|
67 |
case "RESTORE": |
68 |
new RestoreProtocol(requestArgs);
|
69 |
break;
|
70 |
default:
|
71 |
break;
|
72 |
} |
73 |
|
74 |
int port = packet.getPort();
|
75 |
String answer = "Got your request"; |
76 |
buffer = answer.getBytes(); |
77 |
packet = new DatagramPacket(buffer, 0, buffer.length, clientAddress, port); |
78 |
|
79 |
request.send(packet); |
80 |
} |
81 |
} |
82 |
|
83 |
public static void startThreads(){ //only subscribed mc channel |
84 |
Utils.mcChannel = new McChannel();
|
85 |
new Thread(Utils.mcChannel).start(); |
86 |
|
87 |
Utils.mdbChannel = new MdbChannel();
|
88 |
new Thread(Utils.mdbChannel).start(); |
89 |
|
90 |
Utils.mdrChannel = new channels.MdrChannel();
|
91 |
new Thread(Utils.mdrChannel).start(); |
92 |
} |
93 |
|
94 |
public static void joinMulticastGroup() throws IOException{ |
95 |
//mc channel
|
96 |
Utils.mcSocket = new MulticastSocket(getMcPort()); |
97 |
Utils.mcSocket.joinGroup(getMcAddress()); |
98 |
//mdb channel
|
99 |
Utils.mdbSocket = new MulticastSocket(getMdbPort()); |
100 |
Utils.mdbSocket.joinGroup(getMdbAddress()); |
101 |
//mdr channel
|
102 |
Utils.mdrSocket = new MulticastSocket(getMdrPort()); |
103 |
Utils.mdrSocket.joinGroup(getMdrAddress()); |
104 |
} |
105 |
|
106 |
public static boolean parseArgs(String[] args) throws UnknownHostException{ |
107 |
if(args == null || args.length != 4){ |
108 |
System.out.println("Usage: java peer.peer <SenderId> <McAddress>:<McPortNumber> <MdbAddress>:<MdbPortNumber> <MdrAddress>:<MdrPortNumber>"); |
109 |
return false; |
110 |
} |
111 |
|
112 |
//sets sender id
|
113 |
setSenderId(Integer.parseInt(args[0])); |
114 |
|
115 |
//sets address and port for channel MC
|
116 |
String[] mcPart = args[1].split(":"); |
117 |
setMcAddress(InetAddress.getByName(mcPart[0])); |
118 |
setMcPort(Integer.parseInt(mcPart[1])); |
119 |
|
120 |
//sets address and port for channel MDB
|
121 |
String[] mdbPart = args[2].split(":"); |
122 |
setMdbAddress(InetAddress.getByName(mdbPart[0])); |
123 |
setMdbPort(Integer.parseInt(mdbPart[1])); |
124 |
|
125 |
//sets address and port for channel MDR
|
126 |
String[] mdrPart = args[3].split(":"); |
127 |
setMdrAddress(InetAddress.getByName(mdrPart[0])); |
128 |
setMdrPort(Integer.parseInt(mdrPart[1])); |
129 |
|
130 |
return true; |
131 |
} |
132 |
|
133 |
//getters and setters
|
134 |
|
135 |
public static InetAddress getMcAddress(){ |
136 |
return mcChannel;
|
137 |
} |
138 |
|
139 |
public static InetAddress getMdbAddress(){ |
140 |
return mdbChannel;
|
141 |
} |
142 |
|
143 |
public static InetAddress getMdrAddress(){ |
144 |
return mdrChannel;
|
145 |
} |
146 |
|
147 |
public static int getMcPort(){ |
148 |
return mcPort;
|
149 |
} |
150 |
|
151 |
public static int getMdbPort(){ |
152 |
return mdbPort;
|
153 |
} |
154 |
|
155 |
public static int getMdrPort(){ |
156 |
return mdrPort;
|
157 |
} |
158 |
|
159 |
public static void setMcAddress(InetAddress mcAddress){ |
160 |
mcChannel = mcAddress; |
161 |
} |
162 |
|
163 |
public static void setMdbAddress(InetAddress mdbAddress){ |
164 |
mdbChannel = mdbAddress; |
165 |
} |
166 |
|
167 |
public static void setMdrAddress(InetAddress mdrAddress){ |
168 |
mdrChannel = mdrAddress; |
169 |
} |
170 |
|
171 |
public static void setMcPort(int port){ |
172 |
mcPort = port; |
173 |
} |
174 |
|
175 |
public static void setMdbPort(int port){ |
176 |
mdbPort = port; |
177 |
} |
178 |
|
179 |
public static void setMdrPort(int port){ |
180 |
mdrPort = port; |
181 |
} |
182 |
|
183 |
public static void setSenderId(int id){ |
184 |
senderId = id; |
185 |
} |
186 |
|
187 |
public static int getSenderId(){ |
188 |
return senderId;
|
189 |
} |
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
} |
198 |
|