-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
313 lines (261 loc) · 7.34 KB
/
Copy pathClient.java
File metadata and controls
313 lines (261 loc) · 7.34 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
// this class handles all the client side architecture
public class Client
{
// this client always listens to this port for requests
public static final int listeningPort = 2737;
// 1)catch the server requests
// 2)create a thread to handle the request
public static void main(String [] args) throws Exception
{
// create a socket to accept requests at port specified
ServerSocket ss = new ServerSocket(listeningPort);
System.out.println("Client is listening to port " + listeningPort);
while(true)
{
Socket cs = ss.accept();
System.out.println("Connection Accepted from " + cs.getRemoteSocketAddress().toString());
Thread new_thread = new ProcessRequest(cs);
new_thread.start();
}
}
// this method sends the error message to the server
public static void sendErrorMsg(Socket soc, String msg) throws Exception
{
String response = "Error " + msg + "\n";
soc.getOutputStream().write(response.getBytes("UTF-8"));
soc.close();
}
}
// this thread is for processing a particular request
class ProcessRequest extends Thread
{
private Socket soc;
// constructor
public ProcessRequest(Socket soc)
{
this.soc = soc;
}
@Override
public void run()
{
try
{
System.out.println("Entering the thread0");
// Read the request from the server
BufferedReader reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
String request = reader.readLine();
if(request == null) throw new Exception("The server has lost the connection.");
System.out.println("Incoming Request: " + request);
// request is of the format "blank_file_name integer"
String requestFormat = "[a-zA-Z0-9.]+ \\d+";
if(!request.matches(requestFormat))
{
String msg = "Invalid request format.";
System.out.println(msg);
Client.sendErrorMsg(soc, msg);
return ;
}
// get the blank file name and integer n
String blankFileName = request.split(" ")[0];
int n = Integer.parseInt(request.split(" ")[1]);
// array to be shared by this and the next thread
int[] sequence = new int[n];
// generating fibbonacci sequence
Thread sequenceThread = new GenerateSequence(n, sequence, soc);
sequenceThread.start();
sequenceThread.join();
// file to which the sequence has to be written
File blankFile = new File(blankFileName);
if(blankFile.exists())
{
String msg = "This file temporarily exists in the system. Try again sometime later.";
System.out.println(msg);
Client.sendErrorMsg(soc, msg);
return ;
}
blankFile.createNewFile();
// writing to the blank file
Thread writeThread = new WriteSequence(blankFile, sequence);
writeThread.start();
writeThread.join();
// sending the file to the server
Thread sendThread = new SendFile(blankFile, soc);
sendThread.start();
sendThread.join();
// after sending the file to the server client should delete the temporary file
blankFile.delete();
// thread for acknowledgement
Thread ackThread = new HandleAcknowledgement(soc);
ackThread.start();
ackThread.join();
Thread.sleep(2000);
System.out.println("Exiting the thread0 - Request successfully served");
}
catch (Exception e)
{
System.out.println(e.getMessage());
return ;
}
}
}
// this thread generates the fibbonacci sequence of length n
class GenerateSequence extends Thread
{
private int n;
private int[] sequence;
private Socket soc;
// constructor
public GenerateSequence(int n, int[] sequence, Socket soc)
{
this.n = n;
this.sequence = sequence;
this.soc = soc;
}
@Override
public void run()
{
try
{
System.out.println("Entering thread1 - Generating sequence");
// if n <= 0 send error msg
if(n <= 0)
{
String msg = "n must be a positive integer";
System.out.println(msg);
Client.sendErrorMsg(soc, msg);
return ;
}
sequence[0] = 0;
if(n == 1)
return ;
sequence[1] = 1;
// generating fibbonacci sequence
for(int i = 2;i < n;i++)
sequence[i] = sequence[i-1] + sequence[i-2];
Thread.sleep(4000);
System.out.println("Exiting thread1 - Generated sequence");
return ;
}
catch(Exception e)
{
System.out.println("Something unknown occured in GenerateSequence");
return ;
}
}
}
//this thread writes the generated fibbonacci sequence of length n to the recieved file
class WriteSequence extends Thread
{
private File blankFile;
private int[] sequence;
// constructor
public WriteSequence(File blankFile, int[] sequence)
{
this.blankFile = blankFile;
this.sequence = sequence;
}
@Override
public void run()
{
try
{
System.out.println("Entering thread2- Writing Sequence to blank file");
// writing the fibbnacci sequence to the file
PrintStream ps = new PrintStream(blankFile);
for(int i = 0;i < sequence.length;i++)
ps.println(sequence[i]);
ps.close();
Thread.sleep(4000);
System.out.println("Exiting thread2- Wrote Sequence to blank file");
}
catch(Exception e)
{
System.out.println("Something unknown occured in WriteSequence.");
return ;
}
}
}
// this thread sends the updated file back to the server
class SendFile extends Thread
{
private File toSend;
private Socket soc;
// constructor
public SendFile(File toSend, Socket soc)
{
this.toSend = toSend;
this.soc = soc;
}
@Override
public void run()
{
try
{
System.out.println("Entering thread3- Sending file back to the server");
// sending success message indicating arrival of the file containing the fibbonacci sequence
String response = "Ok. Sending file...\n";
String size = String.valueOf(toSend.length())+"\n";
soc.getOutputStream().write(response.getBytes("UTF-8"));
soc.getOutputStream().write(size.getBytes("UTF-8"));
// getting file input stream
FileInputStream fis = new FileInputStream(toSend);
// sending file to the server
byte[] sendData = new byte[1024];
int lengthRead;
while((lengthRead = fis.read(sendData)) != -1)
{
System.out.println(lengthRead);
soc.getOutputStream().write(sendData, 0, lengthRead);
}
soc.getOutputStream().flush();
fis.close();
Thread.sleep(4000);
System.out.println("Exiting thread3 - Sent file back to the server");
}
catch(Exception e)
{
System.out.println("Something unknown occured in SendFile");
return ;
}
}
}
// this thread handles the acknowledgement from the server
class HandleAcknowledgement extends Thread
{
private Socket soc;
public HandleAcknowledgement(Socket soc)
{
this.soc = soc;
}
@Override
public void run()
{
try
{
System.out.println("Entering thread4 - Handling the acknowledgment");
// reading the ack
BufferedReader reader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
String response = reader.readLine();
if(response.equals("SUCCESS"))
System.out.println("Request successfully served. Closing the connection...");
else
System.out.println("Positive ack not recieved. Closing the connection...");
soc.close();
Thread.sleep(4000);
System.out.println("Exiting thread4 - Handeled the acknowledgment");
}
catch(Exception e)
{
System.out.println("Something unknown occured in HandleAcknowledgement");
return ;
}
}
}