-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClient.java
More file actions
125 lines (102 loc) · 4.65 KB
/
Client.java
File metadata and controls
125 lines (102 loc) · 4.65 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
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
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;
public class Client {
MasterServerClientInterface masterStub;
static Registry registry;
int regPort = Configurations.REG_PORT;
String regAddr = Configurations.REG_ADDR;
int chunkSize = Configurations.CHUNK_SIZE; // in bytes
public Client() {
try {
registry = LocateRegistry.getRegistry(regAddr, regPort);
masterStub = (MasterServerClientInterface) registry.lookup("MasterServerClientInterface");
System.out.println("[@client] Master Stub fetched successfuly");
} catch (RemoteException | NotBoundException e) {
// fatal error .. no registry could be linked
e.printStackTrace();
}
}
public byte[] read(String fileName) throws IOException, NotBoundException{
List<ReplicaLoc> locations = masterStub.read(fileName);
System.out.println("[@client] Master Granted read operation");
// TODO fetch from all and verify
ReplicaLoc replicaLoc = locations.get(0);
ReplicaServerClientInterface replicaStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+replicaLoc.getId());
FileContent fileContent = replicaStub.read(fileName);
System.out.println("[@client] read operation completed successfuly");
System.out.println("[@client] data:");
System.out.println(new String(fileContent.getData()));
return fileContent.getData();
}
public ReplicaServerClientInterface initWrite(String fileName, Long txnID) throws IOException, NotBoundException{
WriteAck ackMsg = masterStub.write(fileName);
txnID = new Long(ackMsg.getTransactionId());
return (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+ackMsg.getLoc().getId());
}
public void writeChunk (long txnID, String fileName, byte[] chunk, long seqN, ReplicaServerClientInterface replicaStub) throws RemoteException, IOException{
FileContent fileContent = new FileContent(fileName, chunk);
ChunkAck chunkAck;
do {
chunkAck = replicaStub.write(txnID, seqN, fileContent);
} while(chunkAck.getSeqNo() != seqN);
}
public void write (String fileName, byte[] data) throws IOException, NotBoundException, MessageNotFoundException{
WriteAck ackMsg = masterStub.write(fileName);
ReplicaServerClientInterface replicaStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+ackMsg.getLoc().getId());
System.out.println("[@client] Master granted write operation");
int segN = (int) Math.ceil(1.0*data.length/chunkSize);
FileContent fileContent = new FileContent(fileName);
ChunkAck chunkAck;
byte[] chunk = new byte[chunkSize];
for (int i = 0; i < segN-1; i++) {
System.arraycopy(data, i*chunkSize, chunk, 0, chunkSize);
fileContent.setData(chunk);
do {
chunkAck = replicaStub.write(ackMsg.getTransactionId(), i, fileContent);
} while(chunkAck.getSeqNo() != i);
}
// Handling last chunk of the file < chunk size
int lastChunkLen = chunkSize;
if (data.length%chunkSize > 0)
lastChunkLen = data.length%chunkSize;
chunk = new byte[lastChunkLen];
System.arraycopy(data, segN-1, chunk, 0, lastChunkLen);
fileContent.setData(chunk);
do {
chunkAck = replicaStub.write(ackMsg.getTransactionId(), segN-1, fileContent);
} while(chunkAck.getSeqNo() != segN-1 );
System.out.println("[@client] write operation complete");
replicaStub.commit(ackMsg.getTransactionId(), segN);
System.out.println("[@client] commit operation complete");
}
public void commit(String fileName, long txnID, long seqN) throws MessageNotFoundException, IOException, NotBoundException{
ReplicaLoc primaryLoc = masterStub.locatePrimaryReplica(fileName);
ReplicaServerClientInterface primaryStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+primaryLoc.getId());
primaryStub.commit(txnID, seqN);
System.out.println("[@client] commit operation complete");
}
public void batchOperations(String[] cmds){
System.out.println("[@client] batch operations started");
String cmd ;
String[] tokens;
for (int i = 0; i < cmds.length; i++) {
cmd = cmds[i];
tokens = cmd.split(", ");
try {
if (tokens[0].trim().equals("read"))
this.read(tokens[1].trim());
else if (tokens[0].trim().equals("write"))
this.write(tokens[1].trim(), tokens[2].trim().getBytes());
else if (tokens[0].trim().equals("commit"))
this.commit(tokens[1].trim(), Long.parseLong(tokens[2].trim()), Long.parseLong(tokens[3].trim()));
}catch (IOException | NotBoundException | MessageNotFoundException e){
System.err.println("Operation "+i+" Failed");
}
}
System.out.println("[@client] batch operations completed");
}
}