-
Notifications
You must be signed in to change notification settings - Fork 30
/
RunServer.java
93 lines (76 loc) · 2.84 KB
/
RunServer.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server;
import server.controller.Admin;
import server.controller.Client;
import server.controller.ClientManager;
import server.controller.RoomManager;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import shared.security.RSA;
/**
*
* @author Hoang Tran < hoang at [email protected] >
*/
public class RunServer {
public static volatile ClientManager clientManager;
public static volatile RoomManager roomManager;
public static volatile RSA serverSide;
public static boolean isShutDown = false;
public static ServerSocket ss;
public RunServer() {
try {
int port = 5056;
ss = new ServerSocket(port);
System.out.println("Created Server at port " + port + ".");
// init rsa key
serverSide = new RSA()
.preparePrivateKey("src/Server/rsa_keypair/privateKey");
// init managers
clientManager = new ClientManager();
roomManager = new RoomManager();
// create threadpool
ThreadPoolExecutor executor = new ThreadPoolExecutor(
10, // corePoolSize
100, // maximumPoolSize
10, // thread timeout
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(8) // queueCapacity
);
// admin
executor.execute(new Admin());
// server main loop - listen to client's connection
while (!isShutDown) {
try {
// socket object to receive incoming client requests
Socket s = ss.accept();
// System.out.println("+ New Client connected: " + s);
// create new client runnable object
Client c = new Client(s);
clientManager.add(c);
// execute client runnable
executor.execute(c);
} catch (IOException ex) {
// Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
isShutDown = true;
}
}
System.out.println("shutingdown executor...");
executor.shutdownNow();
} catch (IOException ex) {
Logger.getLogger(RunServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
new RunServer();
}
}