-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceThread.java
More file actions
73 lines (54 loc) · 1.9 KB
/
ServiceThread.java
File metadata and controls
73 lines (54 loc) · 1.9 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
/*
*
* XMU CNNS Class Demo
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class ServiceThread extends Thread {
Socket welcomeSocket;
Map<String, String> fileCache = new HashMap<>();
int cacheSize = 8096;
Map<String, String> cfgMap = new HashMap<>();
public ServiceThread(Socket welcomeSocket, Map<String, String> cfgMap, Map<String, String> fileCache, int cacheSize) {
this.welcomeSocket = welcomeSocket;
this.cfgMap = cfgMap;
this.fileCache = fileCache;
this.cacheSize = cacheSize;
}
public void run() {
System.out.println("Thread " + this + " started.");
// synchronized (welcomeSocket) {
try {
// process a request
WebRequestHandler wrh =
new WebRequestHandler( welcomeSocket, cfgMap, fileCache, cacheSize);
wrh.processRequest();
} catch (Exception e) {
}
// } // end of extract a request
// serveARequest( s );
} // end run
private void serveARequest(Socket connSock) {
try {
// create read stream to get input
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connSock.getInputStream()));
String clientSentence = inFromClient.readLine();
// process input
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
// send reply
DataOutputStream outToClient =
new DataOutputStream(connSock.getOutputStream());
outToClient.writeBytes(capitalizedSentence);
connSock.close();
System.out.println("Finish a request");
} catch (Exception e) {
System.out.println("Exception happened in Thread " + this);
} // end of catch
} // end of serveARequest
} // end ServiceThread