-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2PClient.java
295 lines (279 loc) · 8.34 KB
/
P2PClient.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
291
292
293
294
295
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
public class P2PClient implements Runnable {
ServerSocket servsok;
@Override
public void run() {
// TODO Auto-generated method stub
Socket peerSock = null;
try
{
peerSock = servsok.accept();
new Thread(this).start();
ObjectOutputStream out = new ObjectOutputStream(peerSock.getOutputStream());
ObjectInputStream in = new ObjectInputStream(peerSock.getInputStream());
String reply = (String) in.readObject();
System.out.println(reply);
if (reply.contains("GET"))
{
String rfc = in.readObject().toString();
File folder = new File("RFC");
File pathToFile = new File(folder.getCanonicalPath()+"\\"+"RFC"+rfc+".txt");
if(pathToFile.exists())
{
out.writeObject("P2P-CI/1.0 200 OK \n"+
"Date: "+ (new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss")).format(new Date(0)) + " GMT\n"+
"OS: " + System.getProperty("os.name")+ "\n"+
"Last Modified: " + (new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss")).format(new Date(pathToFile.lastModified())) +" GMT \n"+
"Content-Length: " + pathToFile.length() + "\nContent-Type: Text \n");
byte[] content = Files.readAllBytes(pathToFile.toPath());
out.writeObject(content);
}
else
{
out.writeObject("P2P-CI/1.0 404 Not Found \n");
out.flush();
}
out.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
if(args.length==1)
{
String address = args[0];
String hostName = InetAddress.getLocalHost().getHostAddress();
int port = 7734;
Random rand = new Random();
int clservport = 5000 + rand.nextInt(5000);
new P2PClient().createClServSock(clservport);
try
{
//System.out.println("DEBUG");
Socket clsock = new Socket(address,port);
//System.out.println("DEBUG");
ObjectInputStream input = new ObjectInputStream(clsock.getInputStream());
ObjectOutputStream output = new ObjectOutputStream(clsock.getOutputStream());
//System.out.println("DEBUG");
//System.out.println("DEBUG");
output.writeObject(hostName);
int clport=clsock.getLocalPort();
System.out.println(hostName+" "+clport);
BufferedReader ip = new BufferedReader(new InputStreamReader(System.in));
String cmd;
while(true)
{
System.out.println("Enter Command:");
cmd = ip.readLine().trim();
if(cmd.equalsIgnoreCase("ADD"))
{
add(input,output,ip,hostName,clservport,clport);
}
else if(cmd.equalsIgnoreCase("GET"))
{
get(input,output,ip,hostName,clservport,clport);
}
else if(cmd.equalsIgnoreCase("LIST"))
{
list(input,output,ip,hostName,clservport,clport);
}
else if(cmd.equalsIgnoreCase("LOOKUP"))
{
lookup(input,output,ip,hostName,clservport,clport);
}
else if(cmd.equalsIgnoreCase("EXIT"))
{
clsock.close();
System.out.println("Closing Connection....");
System.exit(0);
}
else
{
System.out.println("Please enter a valid Command");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
System.out.println("Please provide hostname of the server !!!");
}
}
void createClServSock(int port) throws IOException {
//System.out.println("DEBUG");
this.servsok = new ServerSocket(port);
System.out.println("Client is Listening");
new Thread(this).start();
}
static void add(ObjectInputStream in, ObjectOutputStream out, BufferedReader br, String host,
int port, int lport)
{
String rfc=null;
String rfct = null;
try
{
System.out.println("Enter RFC:");
rfc = br.readLine();
System.out.println("Enter RFC title:");
rfct = br.readLine();
File dir = new File("RFC");
File fil=null;
fil = new File(dir.getCanonicalPath()+"\\"+"RFC"+rfc+".txt");
System.out.println("RFC"+rfc+".txt");
if((fil.exists()))
{
System.out.println(" ADD RFC " + rfc + "P2P-CI/1.0\n HOST:"+ host + "\n PORT:" + port + "\n TITLE:" + rfct + "\n");
out.writeObject(" ADD RFC " + rfc + "P2P-CI/1.0\n HOST:"+ host + "\n PORT:" + port + "\n TITLE:" + rfct + "\n");
out.writeObject(rfc);
out.writeObject(host);
out.writeObject(new Integer(port).toString());
out.writeObject(rfct);
out.writeObject(new Integer(lport).toString());
System.out.println(in.readObject());
}
else
{
System.out.println("ERROR: File does not exists !!!");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
static void list(ObjectInputStream in, ObjectOutputStream out, BufferedReader br, String host,
int port, int lport)
{
try
{
out.writeObject(" LIST ALL P2P-CI/1.0\n HOST: " + host + "\n PORT: " + port + "\n");
String reply = (in.readObject()).toString().trim();
System.out.println(reply);
if (! reply.startsWith("P2P-CI/1.0"))
{
System.out.println("Error: Version Different !!!");
return;
}
if (!(reply.contains("200 OK")))
{
System.out.println("Error Occured at Server: No list received");
}
else
{
reply = in.readObject().toString();
while (reply.equalsIgnoreCase("finish") == false)
{
//System.out.println("DEBUG");
System.out.print(reply);
reply = (String) in.readObject();
}
return;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
static void lookup(ObjectInputStream in, ObjectOutputStream out, BufferedReader br, String host,
int port, int lport)
{
try
{
System.out.println("Enter the RFC to lookup:");
String rfc = br.readLine().trim();
System.out.println("Enter the title of the RFC:");
String rfct = br.readLine().trim();
out.writeObject(" LOOKUP RFC " + rfc + " P2P-CI/1.0\n HOST: "
+ host + "\n PORT: " + port + "\n TITLE: " + rfct
+ "\n");
out.writeObject(rfc);
out.writeObject(rfct);
String reply = (in.readObject()).toString().trim();
System.out.println(reply);
if ((reply.contains("200 OK")))
{
reply = (String) in.readObject();
while (reply.equalsIgnoreCase("\n") == false)
{
System.out.print(reply);
reply = (String) in.readObject();
}
return;
}
else
{
System.out.println("RFC does not exist");
}
}
catch (Exception e)
{
System.out.println("Error Occured at server");
}
}
static void get(ObjectInputStream in, ObjectOutputStream out, BufferedReader br, String host,
int port, int lport)
{
Socket peersock = null;
String rfc = null;
String peerhost = null;
int peerport = 0;
try
{
System.out.println("Enter the RFC:");
rfc = br.readLine().trim();
System.out.println("Enter the host:");
peerhost = br.readLine().trim();
System.out.println("Enter the port:");
peerport = Integer.parseInt(br.readLine().trim());
peersock = new Socket(peerhost, peerport);
ObjectOutputStream outs = new ObjectOutputStream(peersock.getOutputStream());
ObjectInputStream ins = new ObjectInputStream(peersock.getInputStream());
outs.writeObject(" GET RFC " + rfc + " P2P-CI/1.0\n HOST: "+ host + "\n OS: " + System.getProperty("os.name") + "\n");
outs.writeObject(rfc);
String reply = ins.readObject().toString().trim();
System.out.println(reply);
if (!reply.startsWith("P2P-CI/1.0"))
{
System.out.println("Version difference with peer... Aborting...");
return;
}
if ((reply.contains("200 OK")))
{
File dir = new File("RFC");
File file = new File(dir.getCanonicalPath() + "\\RFC" + rfc + ".txt");
file.createNewFile();
byte[] content = (byte[]) ins.readObject();
Files.write(file.toPath(), content);
}
else
{
System.out.println("Error Occured: RFC not copied !!!");
}
}
catch (Exception e)
{
System.out.println("Peer Unreachable !!!");
}
}
}