-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainServer.java
72 lines (52 loc) · 1.65 KB
/
MainServer.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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainServer {
/* Some constants */
public static final int BASE_PORT = 1250; // do not change
/* local data for the server
* Every main server is defined in terms of the port it
* listens and the database of allowed users
*/
private ServerSocket serverSocket=null; // server Socket for main server
private StockDataBase allowedUsers=null; // who are allowed to chat
public MainServer(int socket, StockDataBase users) {
this.allowedUsers = users;
try {
this.serverSocket = new ServerSocket(socket);
} catch (IOException e) {
System.out.println(e);
}
}
/* each server will provide the following functions to
* the public. Note that these are non-static
*/
public boolean isAuthorized(String symbol) {
return this.allowedUsers.findSymbol(symbol);
}
public double getPrice(String symbol) {
return this.allowedUsers.findPrice(symbol);
}
/* server will define how the messages should be posted
* this will be used by the connection servers
*/
public void postMSG(String msg) {
// all threads print to same screen
System.out.println(msg);
}
public String authorizedOnce(String a) {
// need to implement this.
return null;
}
public void server_loop() {
try {
while(true) {
Socket socket = this.serverSocket.accept();
ConnectionServer worker = new ConnectionServer(this);
worker.handleConnection(socket);
}
} catch(IOException e) {
System.out.println(e);
}
}// end server_loop
}