-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPC.java
executable file
·289 lines (251 loc) · 9.72 KB
/
RPC.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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.UUID;
class RPC extends Thread{
public static final int portProj1bRPC = 5001;
public static final int maxPacketSize = 30000;
public static final int operationSESSIONREAD = 1;
public static final int operationSESSIONWRITE = 2;
public static final int operationSESSIONEXCHANGE = 3;
public static final int rpcTimeOut = 1000;
//Exchange View
//exchange view with specific server and using view and the server id as input
//return the merged view from the server
public View exchangeViews (View view,String SvrID) throws IOException, ClassNotFoundException{
System.out.println("Merge Client");
DatagramSocket rpcSocket = new DatagramSocket();
rpcSocket.setSoTimeout(rpcTimeOut);
String callID = UUID.randomUUID().toString();
RPCData recvRpc=null;
//add view to the RPCData and translate to byte array
RPCData tempOut =new RPCData(callID,operationSESSIONEXCHANGE,My_Server.view);
byte[] outBuf= Serializer.serialize(tempOut);
System.out.println(outBuf.length);
//get server address by the serverip string
InetAddress addr = (InetAddress.getByName(SvrID));
//send out pkt to the specific server
DatagramPacket sendPkt = new DatagramPacket(outBuf, outBuf.length, addr, portProj1bRPC);
rpcSocket.send(sendPkt);
System.out.println("after send to server");
byte [] inBuf = new byte[maxPacketSize];
// receive response from server
DatagramPacket recvPkt = new DatagramPacket(inBuf, inBuf.length);
try {
do {
recvPkt.setLength(inBuf.length);
rpcSocket.receive(recvPkt);
recvRpc=(RPCData)Serializer.deserialize(recvPkt.getData());
} while(!callID.equals(recvRpc.callID));
} catch(SocketTimeoutException stoe) {
// timeout
recvPkt = null;
System.out.println("Time Out");
}
catch(IOException ioe) {
// other error
System.out.println("IOException");
}
rpcSocket.close();
System.out.println("out of while loop");
if(recvPkt!=null){
return recvRpc.view;
}else return null;
};
// Write session data via RPC
// sessData contains the session data and the write server list
// reqNewServer denote if need to request new server when timeout
// we dont need to request new server when write logout to the sessions
public ArrayList<String> SessionWriteClient(SessionData sessData, int reqNewServer) throws IOException, ClassNotFoundException{
DatagramSocket rpcSocket = new DatagramSocket();
rpcSocket.setSoTimeout(rpcTimeOut);
String callID = UUID.randomUUID().toString();
ArrayList<String> retList=new ArrayList<String>();
RPCData recvRpc=null;
//add readArgs SessionID
System.out.println("write Client");
int kServer = My_Server.data_copies_num;
System.out.println("ori list num:"+sessData.SvrIDs.size()+" kserver:"+kServer);
if(sessData.SvrIDs.size()<kServer){
ArrayList<String> newSvrs;
newSvrs = My_Server.view.availableNodes(kServer-sessData.SvrIDs.size(),sessData.SvrIDs);
System.out.println("get new server num:"+newSvrs.size());
sessData.SvrIDs.addAll(newSvrs);
}
kServer = sessData.SvrIDs.size();
ArrayList<String> newSvrs = sessData.SvrIDs;
RPCData tempOut =new RPCData(callID,operationSESSIONWRITE,sessData);
byte[] outBuf= Serializer.serialize(tempOut);
System.out.println("Write Svr Num:"+sessData.SvrIDs.size()+",session ID:"+sessData.sessID);
byte [] inBuf = new byte[maxPacketSize];
DatagramPacket recvPkt = new DatagramPacket(inBuf, inBuf.length);
// send out the packets and wait for response
while(kServer!=0){
//send out packets
for( String backup:newSvrs ) {
DatagramPacket sendPkt = new DatagramPacket(outBuf, outBuf.length, InetAddress.getByName(backup), portProj1bRPC);
rpcSocket.send(sendPkt);
System.out.println(sendPkt.getAddress().getHostAddress());
}
//try to get response
try {
System.out.println(" send "+kServer+" servers");
do {
recvPkt.setLength(inBuf.length);
rpcSocket.receive(recvPkt);
recvRpc=(RPCData)Serializer.deserialize(recvPkt.getData());
System.out.println("recv:"+recvRpc.callID+",callID:"+callID+","+recvRpc.op);
if(recvRpc.op == 1&& callID.equals(recvRpc.callID)){
kServer--;
System.out.println("recvsucc");
retList.add(recvPkt.getAddress().getHostAddress());
My_Server.view.updateView(recvPkt.getAddress().getHostAddress(),"up");
}
System.out.println("write while loop");
} while(kServer>0);
} catch(SocketTimeoutException stoe) {
// process timeout
// mark the down server
// check if need to request new server
for (String svr:newSvrs){
int match=0;
for(String x:retList){
if (svr.equals(x)){
match=1;
}
}
if(match == 0){
//mark server down
My_Server.view.updateView(svr,"down");
}
}
if(reqNewServer==1){
newSvrs = My_Server.view.availableNodes(kServer,retList);
kServer = newSvrs.size();
}else
kServer=0;
}
catch(IOException ioe) {
// other error
System.out.println("IOException");
}
}
rpcSocket.close();
System.out.println("retList:"+retList);
return retList;
}
//
// SessionReadClient(sessionID)
// sending to multiple destAddrs, all at port = portProj1bRPC
// using a single pre-existing DatagramSocket object rpcSocket
//
public session SessionReadClient(SessionData sessData) throws IOException, ClassNotFoundException{
System.out.println("read Client");
DatagramSocket rpcSocket = new DatagramSocket();
rpcSocket.setSoTimeout(rpcTimeOut);
String callID = UUID.randomUUID().toString();
RPCData recvRpc=null;
//add readArgs SessionID
RPCData tempOut =new RPCData(callID,operationSESSIONREAD,sessData);
byte[] outBuf= Serializer.serialize(tempOut);
//add primary SvrID and backupSvrID
//send out pkt to all Svr: primary and backups
System.out.println("Read Svr Num:"+sessData.SvrIDs.size()+",session ID:"+sessData.sessID);
for( String backup:sessData.SvrIDs ) {
DatagramPacket sendPkt = new DatagramPacket(outBuf, outBuf.length, InetAddress.getByName(backup), portProj1bRPC);
rpcSocket.send(sendPkt);
}
//recv reponse
byte [] inBuf = new byte[maxPacketSize];
DatagramPacket recvPkt = new DatagramPacket(inBuf, inBuf.length);
try {
while(true){
recvPkt.setLength(inBuf.length);
rpcSocket.receive(recvPkt);
recvRpc=(RPCData)Serializer.deserialize(recvPkt.getData());
if(recvRpc.sess!=null && callID.equals(recvRpc.callID)&&recvRpc.sess.version>=sessData.versionNum){
My_Server.dataSourceIP = recvPkt.getAddress().getHostAddress();
break;
}
}
} catch(SocketTimeoutException stoe) {
// timeout
recvPkt = null;
System.out.println("Time Out");
}
catch(IOException ioe) {
// other error
System.out.println("IOException");
}
rpcSocket.close();
System.out.println("read end");
if(recvPkt!=null){
String SvrID = recvPkt.getAddress().getHostAddress();
System.out.println("read get response from :"+SvrID);
My_Server.view.updateView(SvrID,"up");
return recvRpc.sess;
}
else
return null;
}
// Continually running server for listening the socket for operation
@SuppressWarnings("resource")
public void SessionServer() throws IOException, ClassNotFoundException{
DatagramSocket rpcSocket = new DatagramSocket(portProj1bRPC);
RPCData newRpc = null;
System.out.println("Server started");
while(true) {
byte[] inBuf = new byte[maxPacketSize];
DatagramPacket recvPkt = new DatagramPacket(inBuf, inBuf.length);
rpcSocket.receive(recvPkt);
InetAddress returnAddr = recvPkt.getAddress();
int returnPort = recvPkt.getPort();
// here inBuf contains the callID and operationCode
System.out.println("pktLen:"+recvPkt.getData().length);
RPCData rpcData=(RPCData)Serializer.deserialize(recvPkt.getData());
System.out.println( "rpc operation code:"+rpcData.op);
byte[] outBuf = null;
switch( rpcData.op ) {
// 3 kinds operations
case operationSESSIONREAD:
System.out.println("operationSESSIONREAD");
// SessionRead accepts call args and returns call results
newRpc = new RPCData(rpcData.callID,0,My_Server.retrieve(rpcData.sessData));
outBuf= Serializer.serialize(newRpc);
break;
case operationSESSIONWRITE:
System.out.println("operationSESSIONWRITE");
if (My_Server.write(rpcData.sessData)) newRpc = new RPCData(rpcData.callID,1);
else newRpc = new RPCData(rpcData.callID,0);
outBuf= Serializer.serialize(newRpc);
break;
case operationSESSIONEXCHANGE:
System.out.println("operationSESSIONEXCHANGE");
My_Server.view.mergeView(rpcData.view);
newRpc = new RPCData(rpcData.callID,0,My_Server.view);
outBuf= Serializer.serialize(newRpc);
break;
}
// here outBuf should contain the callID and results of the call
DatagramPacket sendPkt = new DatagramPacket(outBuf, outBuf.length,
returnAddr, returnPort);
System.out.println("send Back : "+newRpc.callID+newRpc.op);
rpcSocket.send(sendPkt);
}
}
public void run(){
//RPC Server
try {
SessionServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}