-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
84 lines (71 loc) · 2.78 KB
/
Copy pathClient.java
File metadata and controls
84 lines (71 loc) · 2.78 KB
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.StringTokenizer;
public class Client {
public static final int msgSize = 2; // in characters
public static void main(String[] args) throws NotBoundException,
FileNotFoundException, IOException, MessageNotFoundException {
Configurator config = Configurator.getInstance();
// Get Registry Server
Registry registryMaster = LocateRegistry.getRegistry(
config.getMasterHostname(), config.getMasterPort());
MasterServerClientInterface masterHandler = (MasterServerClientInterface) registryMaster
.lookup(Global.MASTER_LOOKUP);
// Start Client Parser
BufferedReader buff = new BufferedReader(new InputStreamReader(
System.in));
StringTokenizer st;
String command, fileName, data;
while (true) {
st = new StringTokenizer(buff.readLine());
command = st.nextToken();
fileName = st.nextToken();
if (command.equals("W")) {// write only
data = st.nextToken();
// WRITE FILE:
FileContent fc = new FileContent(fileName, data);
WriteMsg wMsg = masterHandler.write(fc);
String writeReplicaLoc = wMsg.getLoc().location;
int writeReplicaPort = wMsg.getLoc().replicaPort;
// Writing in replica
Registry registryReplica2 = LocateRegistry.getRegistry(
writeReplicaLoc, writeReplicaPort);
ReplicaServerClientInterface replicaHandler = (ReplicaServerClientInterface) registryReplica2
.lookup(Global.REPLICA_LOOKUP);
int msgNum = (int) Math.ceil(1d * fc.fileContent.length()
/ msgSize);
int start = 0, end = msgSize;
AckMsg msg = new AckMsg(wMsg.getTransactionId(), 1);
for (int i = 0; i < msgNum; i++) {
int temp = end < fc.fileContent.length() ? end
: fc.fileContent.length();
String msgContent = fc.fileContent.substring(start, temp);
msg = replicaHandler.write(msg.getTxnID(), msg
.getMsgSeqNum(), new FileContent(fc.fileName,
msgContent));
start = temp;
end += msgSize;
}
replicaHandler.commit(wMsg.getTransactionId(), msgNum);
} else {
// READ FILE:
// Get primary replica from Master server
ReplicaLoc[] rlocs = masterHandler.read(fileName);
String replicaLoc = rlocs[0].location;
int replicaPort = rlocs[0].replicaPort;
// Reading from replica
Registry registryReplica1 = LocateRegistry.getRegistry(
replicaLoc, replicaPort);
ReplicaServerClientInterface replicaHandler = (ReplicaServerClientInterface) registryReplica1
.lookup(Global.REPLICA_LOOKUP);
FileContent fileContent = replicaHandler.read(fileName);
System.out.println(fileContent.toString());
}
}
}
}