-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerThreadWebServer.java
More file actions
executable file
·70 lines (60 loc) · 2.35 KB
/
PerThreadWebServer.java
File metadata and controls
executable file
·70 lines (60 loc) · 2.35 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
/**
** Sequential and Per-thread HTTP Servers: service threads competing on welcome socket
** Usage: java BasicWebServer -config <config_file_name>
**
**/
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
class PerThreadWebServer {
public static int serverPort = 6789;
//public static String WWW_ROOT = "/home/httpd/html/zoo/classes/cs433/";
public static String WWW_ROOT = "./";
// private WebRequestHandlerThread[] threads;
public static int cacheSize = 8096;
public static int reqCnt = 0;
static HashMap<String, String> cfgMap = new HashMap<String, String>();
static Map<String, String> fileCache = new HashMap<>();
public static void main(String args[]) throws Exception {
// see if there is .conf
cfgMap.put("vb_default", "./");
String confName = "httpd.conf";
if (args.length >= 2)
if (args[0].equals("-config")) {
confName = args[1];
FileTest cfg = new FileTest();
String cfgFileContent = cfg.cfgRead(confName);
cfgMap = cfg.generateCfgMap(cfgFileContent);
serverPort = Integer.parseInt(cfgMap.get("Listen"));
cacheSize = Integer.parseInt(cfgMap.get("CacheSize"));
System.out.println(cfgMap.get("vb_yunxi.site"));
System.out.println(cfgMap.toString());
}
HeartbeatMonitor cm = new HeartbeatMonitor();
// create server socket
ServerSocket listenSocket = new ServerSocket(serverPort);
System.out.println("server listening at: " + listenSocket);
System.out.println("server www root: " + WWW_ROOT);
WebRequestHandlerThread[] threads = new WebRequestHandlerThread[1000];
while (true) {
try {
// take a ready connection from the accepted queue
Socket connectionSocket = listenSocket.accept();
System.out.println("\nReceive request from " + connectionSocket);
threads[reqCnt] = new WebRequestHandlerThread(connectionSocket, cfgMap, fileCache, cacheSize);
System.out.println("1");
System.out.println("Thread " + reqCnt + " starts!");
threads[reqCnt].start();
reqCnt++;
threads[reqCnt-1].join();
// process a request
// WebRequestHandler wrh =
// new WebRequestHandler( connectionSocket, cfgMap, fileCache, cacheSize);
// wrh.processRequest();
} catch (Exception e)
{
}
} // end of while (true)
} // end of main
} // end of class WebServer