-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pac.java
134 lines (121 loc) · 3.33 KB
/
Pac.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
/**
* Pac.java
* pac server, resource server that holds the files allotted by yac server.
*/
import java.io.*;
import java.net.*;
public class Pac
{
private ObjectInputStream fromYac;
private ObjectOutputStream toYac;
private String pacName;
private PacRequest pacReq;
private PacReply pacRep;
private Socket yacSock;
public static void main(String[] args)
{
new Pac().start(args);
}
public void start(String[] args)
{
if (args.length < 1)
{
System.err.println("Pac: ERROR: please specify server name!");
System.exit(1);
}
pacName = args[0];
File pacDir = new File(pacName);
if (!pacDir.mkdirs())
{
System.err.println("Pac: ERROR: could not create pac directory for " + pacName);
System.exit(1);
}
try
{
System.out.println("Pac: connecting to Yac and creating streams");
yacSock = new Socket(Yac.ADDRESS, Yac.PAC_PORT);
System.out.println("Pac: ... toYac");
toYac = new ObjectOutputStream(yacSock.getOutputStream());
// register pac w/ yac
System.out.println("Pac: registering w/ Yac");
PacRegistration pacReg = new PacRegistration(pacName);
toYac.writeObject(pacReg);
//toYac.close();
System.out.println("Pac: entering main server loop...");
// enter server loop
fromYac = new ObjectInputStream(yacSock.getInputStream());
while (true)
{
//ServerSocket yacListen = new ServerSocket(Yac.PAC_PORT, Yac._BACKLOG);
System.out.println();
System.out.println("Pac: waiting for request");
pacReq = (PacRequest) fromYac.readObject();
System.out.println("Pac: got request!" );
pacRep = doOp();
toYac.writeObject(pacRep);
} // serverLoop
}
catch (Exception e)
{
System.err.println(e);
}
} // main
private PacReply doOp()
{
PacOp op = pacReq.getOp();
if (op == PacOp.PUT) { return pacPut();}
else if (op == PacOp.GET) { return pacGet();}
else if (op == PacOp.RM) { return pacRm();}
else { return null;} //<- this shouldn't happen!
}
private PacReply pacPut()
{
System.out.println("putting " + pacReq.getName());
try
{
FileOutputStream fos = new FileOutputStream(pacName + "/" + pacReq.getName());
fos.write(pacReq.getData());
fos.close();
return new PacReply(0, null);
}
catch (IOException e)
{
System.out.println("put failed!");
return new PacReply(-1, null);
}
}
private PacReply pacGet()
{
System.out.println("getting " + pacReq.getName());
try
{
File f = new File(pacName + "/" + pacReq.getName());
int byteLength = (int) f.length();
byte[] data = new byte[byteLength];
FileInputStream fis = new FileInputStream(f);
fis.read(data,0,byteLength);
fis.close();
return new PacReply(0, data);
}
catch (IOException e)
{
System.out.println("get failed!");
return new PacReply(-1, null);
}
}
private PacReply pacRm()
{
System.out.println("deleting " + pacReq.getName());
try
{
File f = new File(pacName + "/" + pacReq.getName());
f.delete();
return new PacReply(0, null);
}
catch (SecurityException e)
{
System.out.println("rm failed!");
return new PacReply(-1, null);
}
}
}//Pac