-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMain.java
More file actions
213 lines (166 loc) · 6.62 KB
/
Main.java
File metadata and controls
213 lines (166 loc) · 6.62 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
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
public class Main {
static int regPort = Configurations.REG_PORT;
static Registry registry ;
/**
* respawns replica servers and register replicas at master
* @param master
* @throws IOException
*/
static void respawnReplicaServers(Master master)throws IOException{
System.out.println("[@main] respawning replica servers ");
// TODO make file names global
BufferedReader br = new BufferedReader(new FileReader("repServers.txt"));
int n = Integer.parseInt(br.readLine().trim());
ReplicaLoc replicaLoc;
String s;
for (int i = 0; i < n; i++) {
s = br.readLine().trim();
replicaLoc = new ReplicaLoc(i, s.substring(0, s.indexOf(':')) , true);
ReplicaServer rs = new ReplicaServer(i, "./");
ReplicaInterface stub = (ReplicaInterface) UnicastRemoteObject.exportObject(rs, 0);
registry.rebind("ReplicaClient"+i, stub);
master.registerReplicaServer(replicaLoc, stub);
System.out.println("replica server state [@ main] = "+rs.isAlive());
}
br.close();
}
public static void launchClients(){
try {
Client c = new Client();
char[] ss = "File 1 test test END ".toCharArray();
byte[] data = new byte[ss.length];
for (int i = 0; i < ss.length; i++)
data[i] = (byte) ss[i];
c.write("file1", data);
byte[] ret = c.read("file1");
System.out.println("file1: " + ret);
c = new Client();
ss = "File 1 Again Again END ".toCharArray();
data = new byte[ss.length];
for (int i = 0; i < ss.length; i++)
data[i] = (byte) ss[i];
c.write("file1", data);
ret = c.read("file1");
System.out.println("file1: " + ret);
c = new Client();
ss = "File 2 test test END ".toCharArray();
data = new byte[ss.length];
for (int i = 0; i < ss.length; i++)
data[i] = (byte) ss[i];
c.write("file2", data);
ret = c.read("file2");
System.out.println("file2: " + ret);
} catch (NotBoundException | IOException | MessageNotFoundException e) {
e.printStackTrace();
}
}
/**
* runs a custom test as follows
* 1. write initial text to "file1"
* 2. reads the recently text written to "file1"
* 3. writes a new message to "file1"
* 4. while the writing operation in progress read the content of "file1"
* 5. the read content should be = to the initial message
* 6. commit the 2nd write operation
* 7. read the content of "file1", should be = initial messages then second message
*
* @throws IOException
* @throws NotBoundException
* @throws MessageNotFoundException
*/
public static void customTest() throws IOException, NotBoundException, MessageNotFoundException{
Client c = new Client();
String fileName = "file1";
char[] ss = "[INITIAL DATA!]".toCharArray(); // len = 15
byte[] data = new byte[ss.length];
for (int i = 0; i < ss.length; i++)
data[i] = (byte) ss[i];
c.write(fileName, data);
c = new Client();
ss = "File 1 test test END".toCharArray(); // len = 20
data = new byte[ss.length];
for (int i = 0; i < ss.length; i++)
data[i] = (byte) ss[i];
byte[] chunk = new byte[Configurations.CHUNK_SIZE];
int seqN =data.length/Configurations.CHUNK_SIZE;
int lastChunkLen = Configurations.CHUNK_SIZE;
if (data.length%Configurations.CHUNK_SIZE > 0) {
lastChunkLen = data.length%Configurations.CHUNK_SIZE;
seqN++;
}
WriteAck ackMsg = c.masterStub.write(fileName);
ReplicaServerClientInterface stub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+ackMsg.getLoc().getId());
FileContent fileContent;
@SuppressWarnings("unused")
ChunkAck chunkAck;
// for (int i = 0; i < seqN; i++) {
System.arraycopy(data, 0*Configurations.CHUNK_SIZE, chunk, 0, Configurations.CHUNK_SIZE);
fileContent = new FileContent(fileName, chunk);
chunkAck = stub.write(ackMsg.getTransactionId(), 0, fileContent);
System.arraycopy(data, 1*Configurations.CHUNK_SIZE, chunk, 0, Configurations.CHUNK_SIZE);
fileContent = new FileContent(fileName, chunk);
chunkAck = stub.write(ackMsg.getTransactionId(), 1, fileContent);
// read here
List<ReplicaLoc> locations = c.masterStub.read(fileName);
System.err.println("[@CustomTest] Read1 started ");
// TODO fetch from all and verify
ReplicaLoc replicaLoc = locations.get(0);
ReplicaServerClientInterface replicaStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+replicaLoc.getId());
fileContent = replicaStub.read(fileName);
System.err.println("[@CustomTest] data:");
System.err.println(new String(fileContent.getData()));
// continue write
for(int i = 2; i < seqN-1; i++){
System.arraycopy(data, i*Configurations.CHUNK_SIZE, chunk, 0, Configurations.CHUNK_SIZE);
fileContent = new FileContent(fileName, chunk);
chunkAck = stub.write(ackMsg.getTransactionId(), i, fileContent);
}
// copy the last chuck that might be < CHUNK_SIZE
System.arraycopy(data, (seqN-1)*Configurations.CHUNK_SIZE, chunk, 0, lastChunkLen);
fileContent = new FileContent(fileName, chunk);
chunkAck = stub.write(ackMsg.getTransactionId(), seqN-1, fileContent);
//commit
ReplicaLoc primaryLoc = c.masterStub.locatePrimaryReplica(fileName);
ReplicaServerClientInterface primaryStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+primaryLoc.getId());
primaryStub.commit(ackMsg.getTransactionId(), seqN);
// read
locations = c.masterStub.read(fileName);
System.err.println("[@CustomTest] Read3 started ");
replicaLoc = locations.get(0);
replicaStub = (ReplicaServerClientInterface) registry.lookup("ReplicaClient"+replicaLoc.getId());
fileContent = replicaStub.read(fileName);
System.err.println("[@CustomTest] data:");
System.err.println(new String(fileContent.getData()));
}
static Master startMaster() throws AccessException, RemoteException{
Master master = new Master();
MasterServerClientInterface stub =
(MasterServerClientInterface) UnicastRemoteObject.exportObject(master, 0);
registry.rebind("MasterServerClientInterface", stub);
System.err.println("Server ready");
return master;
}
public static void main(String[] args) throws IOException {
try {
LocateRegistry.createRegistry(regPort);
registry = LocateRegistry.getRegistry(regPort);
Master master = startMaster();
respawnReplicaServers(master);
// customTest();
launchClients();
} catch (RemoteException e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}