-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMain.java
More file actions
78 lines (60 loc) · 2.26 KB
/
Main.java
File metadata and controls
78 lines (60 loc) · 2.26 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
75
76
77
78
import crypto.Crypto;
import server.LocalServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.RemoteServer;
import util.*;
public class Main {
private final static Logger logger = LoggerFactory.getLogger(Main.class);
public static void checkCrypto(Crypto crypto) {
if(crypto == null) {
logger.error("unknown crypto");
System.exit(1);
}
}
public static void main(String[] args) {
logger.info("starting...");
String configPath = null;
Mode mode = Mode.Unknown;
// load config path
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-c") && i + 1 < args.length) {
configPath = args[i + 1];
i++;
} else if (args[i].equals("-h")) {
Util.printHelpInfo();
System.exit(0);
}
}
// decide running mode
mode = ConfigLoader.loadConfigMode(configPath);
if (mode == Mode.Local) {
LocalConfig config = ConfigLoader.loadAsLocalConfig(configPath);
if (config == null) {
logger.info("failed to load local config.");
System.exit(1);
} else {
logger.info(config.toString());
}
checkCrypto(config.getCrypto());
LocalServer localServer = new LocalServer(config.getHost(), config.getHostPort(), config.getLocalPort());
LocalServer.crypto = config.getCrypto();
localServer.listen();
} else if (mode == Mode.Server) {
ServerConfig config = ConfigLoader.loadAsServerConfig(configPath);
if (config == null) {
logger.info("failed to load server config.");
System.exit(1);
} else {
logger.info(config.toString());
}
checkCrypto(config.getCrypto());
RemoteServer remoteServer = new RemoteServer(config.getPort());
RemoteServer.crypto = config.getCrypto();
remoteServer.listen();
} else {
logger.error("unknown running mode");
System.exit(1);
}
}
}