-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfLoader.java
executable file
·73 lines (65 loc) · 2.05 KB
/
ConfLoader.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
package myServer2;
/*
* AUTHOR : Min Gao
* Project1-Multi-Server Chat System
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ConfLoader {
public Conf loadConf(String serverid, String confPath) {
Conf conf = null;
String[] serverConf = readServerConf(confPath);
for (int i = 0; i < serverConf.length; i++) {
if (serverid.equals(serverConf[i].split("\t")[0])) {
String serverAddress = serverConf[i].split("\t")[1];
int clientsPort = Integer.parseInt(serverConf[i].split("\t")[2]);
int coordinationPort = Integer.parseInt(serverConf[i].split("\t")[3]);
conf = new Conf(serverid, serverAddress, clientsPort, coordinationPort);
}
}
return conf;
}
//initialize other server state
public void initializeServerState(String serverid, String confPath) {
String[] serverConf = readServerConf(confPath);
for (int i = 0; i < serverConf.length; i++) {
if(!serverid.equals(serverConf[i].split("\t")[0])) {
ServerState.getInstance().serverConnected(loadConf((serverConf[i].split("\t")[0]), confPath));
RoomManager.getInstance().addOtherServerRoom(
"MainHall-" + serverConf[i].split("\t")[0] , serverConf[i].split("\t")[0]);
}
}
}
private String[] readServerConf(String confPath) {
String[] serverConf = new String[totalServerNum(confPath)];
int i = 0;
try {
Scanner inputServerConf = new Scanner(new FileInputStream(confPath));
while (inputServerConf.hasNextLine()) {
serverConf[i] = inputServerConf.nextLine();
i++;
}
inputServerConf.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return serverConf;
}
private int totalServerNum(String confPath) {
int i= 0;
try {
Scanner inputServerConf = new Scanner(new FileInputStream(confPath));
while (inputServerConf.hasNextLine()) {
inputServerConf.nextLine();
i++;
}
inputServerConf.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return i;
}
}