-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.java
More file actions
74 lines (68 loc) · 2.58 KB
/
Copy pathWebServer.java
File metadata and controls
74 lines (68 loc) · 2.58 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
package httpwebserver.server;
import httpwebserver.Main;
import httpwebserver.util.Tools;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javasearchengine.objects.Datas;
import javasearchengine.objects.Page;
import javasearchengine.objects.PageManager;
public class WebServer {
public List<String> BanIps = new ArrayList<>();
public String WorkPath = System.getProperty("user.dir");
public ServerSocket ss;
public String path_main = "/search.html";
public HashMap<String, Integer> map = new HashMap<>();
public Datas datasForSearch = new Datas();
public WebServer(int i) {
try {
ss = new ServerSocket(i);
} catch (IOException ex) {
Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void stop() {
PageManager.savePages();
System.exit(0);
}
public void start() {
new Thread() {
@Override
public void run() {
Scanner s = new Scanner(System.in);
while (s.hasNextLine()) {
String[] line = s.nextLine().split(" ");
if (line.length == 1 && line[0].equalsIgnoreCase("stop")) {
Main.wb.stop();
} else if (line.length == 2 && line[0].equalsIgnoreCase("ban")) {
Main.wb.BanIps.add(line[1]);
} else if (line.length == 2 && line[0].equalsIgnoreCase("unban")) {
Main.wb.BanIps.remove(line[1]);
} else if (line.length == 1 && line[0].equalsIgnoreCase("tps")) {
System.out.println("内存使用率:" + Tools.getMemory());
} else if (line.length == 2 && line[0].equalsIgnoreCase("addPage")) {
Page p = new Page(line[1]);
PageManager.LIST.add(p);
p.save(new File(WorkPath, "save"));
}
}
}
}.start();
Socket s;
while (true) {
try {
s = ss.accept();
new SocketThread(s).start();
} catch (IOException ex) {
Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}