-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2pws.java
More file actions
200 lines (171 loc) · 5.23 KB
/
p2pws.java
File metadata and controls
200 lines (171 loc) · 5.23 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
/*
* @arthur Alexio Mota
* @arthur Hua Yang
*/
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Object;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class p2pws implements Runnable{
// global hash table to store the file
static Hashtable<String, String> files;
Socket conn;
int current_port;
// constructor
p2pws(Socket sock, int socket){
this.conn = sock;
//this.files = new Hashtable<String, String>();
this.current_port = socket;
}
public static void main(String[] args){
int port;
files = new Hashtable<String, String>();
if(args.length != 1){
System.out.println("Invalid number of arguments");
System.out.println("Format:");
System.out.println("java p2pws [port number]");
return;
}
try{
port = Integer.parseInt(args[0]);
System.out.println("Port: " + port);
}
catch(NumberFormatException nfe){
System.out.println(nfe);
return;
}
try{
//Create server socket on provided port
ServerSocket server_sock = new ServerSocket(port);
for(;;){
Socket conn = server_sock.accept();
System.out.println("Connection Established");
//create thread for each new connection
new Thread(new p2pws(conn, port)).start();
}
}
catch(IOException e){
System.out.println(e);
return;
}
}
public void run(){
try {
// input and output to client
BufferedReader fromClient = new BufferedReader(new InputStreamReader(conn.getInputStream()));
DataOutputStream toClient = new DataOutputStream(conn.getOutputStream());
// stack to store the client inputs
String line;
// boolean for closing the connectiong
boolean quit = false;
// continues to read client inputs till 'end' to end the connection
while ((line = fromClient.readLine()) != null) {
//line = line.replace("\n", ""); //get rid of newline chars
String tokens[] = line.split(" ");
//Check to see if the line contains a valid request
if((tokens[0].equals("PUT") ||
tokens[0].equals("DELETE") ||
tokens[0].equals("GET")) && tokens.length == 3){
HTTP_Request(tokens, fromClient, toClient);
if (tokens[0].equals("GET")) {
break;
}
}
}
System.out.println("Closing the connection.");
fromClient.close();
toClient.close();
conn.close();
} catch (IOException e) {
System.out.println(e);
}
}
//Evaluates received request from client/peer
public void HTTP_Request(String[] input, BufferedReader fromClient, DataOutputStream toClient) {
switch(input[0]){
case "PUT":
try{
wsPUT(input, fromClient, toClient);
}
catch(IOException e){
System.out.println(e);
return;
}
break;
case "GET":
try {
functions.HTTP_Get(input[1], toClient, current_port, files);
} catch (Exception e) {
System.out.println(e);
return;
}
break;
case "DELETE":
wsDELETE(input, fromClient, toClient);
break;
default:
System.out.println("Incorrect Input");
break;
}
}
public String response(int cmd) {
switch(cmd){
case 1: //if Content not found
return "HTTP/1.1 404 Not Found\nContent-Length: 0";
default: //request was a success
return "HTTP/1.1 200 OK \nContent-Length: 0";
}
}
public void wsPUT(String[] input, BufferedReader fromClient, DataOutputStream toClient) throws IOException {
//removes content-length line
System.out.println("In put!");
String line = fromClient.readLine();
String size[] = line.split(" ");
int limit = Integer.parseInt(size[1]); //Get the Content-Length: ?
String content = "";
while (limit > 0) { //Loop will run until the Content-length is 0 or less
line = fromClient.readLine();
limit-=line.length();
limit--;
content+=line;
}
String key = md5Hash(input[1]);
if(!files.contains(key)){
files.put(key, content);
}
toClient.writeBytes(response(0));
}
public void wsDELETE(String[] input, BufferedReader fromClient, DataOutputStream toClient) {
//Remove file content from hash map
try{
System.out.println("HERE: "+ input[1] + " " + files.get(md5Hash(input[1])));
if(files.remove(md5Hash(input[1])) == null){
toClient.writeBytes(response(1));
}
toClient.writeBytes(response(0));
System.out.println("Check: "+ input[1] + " " + files.get(md5Hash(input[1])));
}
catch(IOException e){
System.out.println(e);
}
}
public static String md5Hash(String input) {
String md5 = null;
if(null == input) return null;
try {
//Create MessageDigest object for MD5
MessageDigest hash = MessageDigest.getInstance("MD5");
//Update input string in message digest
hash.update(input.getBytes(), 0, input.length());
//Converts message digest value in base 16 (hex)
md5 = new BigInteger(1, hash.digest()).toString(16);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return md5;
}
}