-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yac.java
290 lines (272 loc) · 9.79 KB
/
Yac.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
290
import java.io.*;
import java.net.*;
import java.util.*;
/*
* Main interfacing point between the user and the infrastructure.
* Spawns thread for each connection request.
*/
public class Yac
{
public static final String ADDRESS = "localhost"; // when using on multimachines, replace w/ host name eg pug, afghan...
public static final int YAC_PORT = 4000;
public static final int PAC_PORT = 5000;
public static final int CAT_PORT = 6000;
public static final int _BACKLOG = 5;
private List<YacThread> clients =
Collections.synchronizedList(new ArrayList<YacThread>());
private List<PacEntry> pacs =
Collections.synchronizedList(new ArrayList<PacEntry>());
private ServerSocket catListen;
private ServerSocket clientListen;
private ServerSocket pacListen;
private Socket catSock;
private ObjectInputStream fromCat;
private ObjectOutputStream toCat;
public static void main(String[] args)
{
try
{
new Yac().start();
}
catch (IOException e)
{
System.err.println(e);
}
} // main
private void start() throws IOException
{
try
{
clientListen = new ServerSocket(YAC_PORT,_BACKLOG);
pacListen = new ServerSocket(PAC_PORT,_BACKLOG);
catListen = new ServerSocket(CAT_PORT,_BACKLOG);
System.out.println("Yac: waiting for catalog server on port " + CAT_PORT);
catSock = catListen.accept();
toCat = new ObjectOutputStream(catSock.getOutputStream());
fromCat = new ObjectInputStream(catSock.getInputStream());
System.out.println("Yac: listening for clients on " + YAC_PORT);
System.out.println("Yac: listening for pacs on " + PAC_PORT);
(new Thread()
{
public void run()
{
while (true)
{
try
{
System.out.println();
System.out.println("Yac: waiting for client connection");
Socket clientSock = clientListen.accept();
System.out.println("Yac: creating thread for client request");
YacThread yacThr = new YacThread(clientSock);
System.out.println("Yac: starting thread");
yacThr.start();
clients.add(yacThr);
}
catch (Exception e)
{
System.err.println("Yac: " + e);
}
}
}
}).start(); // this thread listens for client requests.
(new Thread()
{
public void run()
{
while (true)
{
try
{
System.out.println();
System.out.println("Yac: waiting for pac registration");
Socket pacSock = pacListen.accept();
ObjectInputStream pacRegIn = new ObjectInputStream(pacSock.getInputStream());
System.out.println("Yac: waiting for pac message");
PacRegistration pacReg = (PacRegistration) pacRegIn.readObject();
System.out.println("Yac: creating pacEntry");
PacEntry newPac = new PacEntry(pacSock, pacRegIn , pacReg.getName());
System.out.println("Yac: adding pac to our collection");
pacs.add(newPac);
System.out.println("Yac: messaging cat to register pac");
toCat.writeObject(new CatRequest(CatOp.CAT_REGPAC, pacReg.getName(), null, 0));
}
catch (Exception e) { System.err.print(e); }
}
}
}).start(); // this thread listens for pac registrations.
}
catch (Exception e)
{
System.err.println(e);
}
} // start
public class PacEntry // our pacs list is a collection of these entries.
{
private Socket s;
private ObjectInputStream fromPac;
private ObjectOutputStream toPac;
private String name;
public PacEntry(Socket sock, ObjectInputStream in, String pacName) throws IOException
{
this.s = sock;
this.fromPac = in;
this.toPac = new ObjectOutputStream(this.s.getOutputStream());
this.name = pacName;
} // entry constructor
// getters
public Socket getSocket() { return this.s; }
public String getName() { return this.name; }
public ObjectInputStream getInput() { return this.fromPac; }
public ObjectOutputStream getOutput() { return this.toPac; }
}
public class YacThread extends Thread
{
private Socket yacSock;
private String owner;
private ObjectInputStream input;
private ObjectOutputStream output;
public YacThread(Socket s)
{
this.yacSock = s;
try
{
System.out.println("YacThread: creating i/o streams\n...from client");
this.input = new ObjectInputStream(this.yacSock.getInputStream());
System.out.println("... to client");
this.output = new ObjectOutputStream(this.yacSock.getOutputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
} // constructor
private CatReply catMessage(CatOp c, String name, String owner, int size)
{
System.out.println("Yac: messaging cat");
try
{
CatRequest catReq = new CatRequest( c, name, owner, size);
toCat.writeObject(catReq);
return (CatReply) fromCat.readObject();
}
catch (Exception e)
{
return new CatReply(-1, e.toString());
}
}
public void run()
{
// handle yac Requests.
try
{
YacRequest yacReq = (YacRequest) input.readObject();
YacOp op = yacReq.getOp();
CatReply catRep;
YacReply yacRep;
PacRequest pacReq;
PacReply pacRep;
ObjectOutputStream toPac;
ObjectInputStream fromPac;
if (op == YacOp.PUT) // PUT //////////////////////////////////////////////////
{
System.out.println("Yac: PUT");
catRep = catMessage(CatOp.CAT_PUT, yacReq.getFileName(),
yacReq.getOwner(), yacReq.getSize());
System.out.println("Yac: got reply from cat !");
if (catRep.getStatus() != 0)
{
System.out.println("Yac: cat reported an error! Messaging client..");
yacRep = new YacReply(catRep.getStatus(), catRep.getMessage().getBytes());
}
else
{
System.out.println("Yac: got cat info, messaging appropriate pac");
pacReq = new PacRequest(PacOp.PUT, yacReq.getFileName(), yacReq.getData());
PacEntry target = getPac(catRep.getMessage());
toPac = target.getOutput();
fromPac = target.getInput();
System.out.println("Yac: writing request to pac " + catRep.getMessage());
toPac.writeObject(pacReq);
pacRep = (PacReply) fromPac.readObject();
System.out.println("Yac: packing client reply..");
yacRep = new YacReply(pacRep.getStatus(), pacRep.getData());
}
output.writeObject(yacRep);
}
else if (op == YacOp.GET) // GET /////////////////////////////////////////////
{
System.out.println("Yac: GET");
catRep = catMessage(CatOp.CAT_GET, yacReq.getFileName(),
yacReq.getOwner(), 0);
if (catRep.getStatus() != 0)
{
yacRep = new YacReply(catRep.getStatus(), catRep.getMessage().getBytes());
}
else
{
System.out.println("Yac: got cat info, messaging appropriate pac");
pacReq = new PacRequest(PacOp.GET, yacReq.getFileName(), null);
PacEntry target = getPac(catRep.getMessage());
toPac = target.getOutput();
fromPac = target.getInput();
toPac.writeObject(pacReq);
pacRep = (PacReply) fromPac.readObject();
yacRep = new YacReply(pacRep.getStatus(), pacRep.getData());
}
output.writeObject(yacRep);
}
else if (op == YacOp.LS) // LS //////////////////////////////////////////////
{
System.out.println("Yac: LS");
catRep = catMessage(CatOp.CAT_LS, null, yacReq.getOwner(), 0);
yacRep = new YacReply(catRep.getStatus(), catRep.getMessage().getBytes());
output.writeObject(yacRep);
}
else if (op == YacOp.RM) // RM //////////////////////////////////////////////
{
System.out.println("Yac: RM");
catRep = catMessage(CatOp.CAT_RM, yacReq.getFileName(),
yacReq.getOwner(), 0);
if (catRep.getStatus() != 0)
{
yacRep = new YacReply(catRep.getStatus(), catRep.getMessage().getBytes());
}
else
{
System.out.println("Yac: got cat info, messaging appropriate pac");
pacReq = new PacRequest(PacOp.RM, yacReq.getFileName(), null);
PacEntry target = getPac(catRep.getMessage());
toPac = target.getOutput();
fromPac = target.getInput();
System.out.println("Yac: writing request to pac " + catRep.getMessage());
toPac.writeObject(pacReq);
pacRep = (PacReply) fromPac.readObject();
yacRep = new YacReply(pacRep.getStatus(), pacRep.getData());
}
output.writeObject(yacRep);
}
else
{
System.err.println("Yac: received unknown operation request from client!");
}
System.out.println("Yac: Completed request! Closing streams and killing thread");
this.input.close();
this.output.close();
clients.remove(this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private PacEntry getPac(String name)
{
for (PacEntry p : pacs)
{
if (p.getName().equals(name)) { return p; }
}
return null; // this shouldn't happen!
}
} // YacThread
} // Yac