|
@@ -1,5 +1,6 @@
|
1
|
|
-package com.uqac.rthoni.com.java_rmi.server;
|
|
1
|
+package com.uqac.rthoni.java_rmi.server;
|
2
|
2
|
|
|
3
|
+import com.uqac.rthoni.java_rmi.server.executors.AbstractCommandExecutor;
|
3
|
4
|
import com.uqac.rthoni.java_rmi.common.Command;
|
4
|
5
|
|
5
|
6
|
import java.io.BufferedReader;
|
|
@@ -8,12 +9,15 @@ import java.io.InputStreamReader;
|
8
|
9
|
import java.net.InetAddress;
|
9
|
10
|
import java.net.ServerSocket;
|
10
|
11
|
import java.net.Socket;
|
|
12
|
+import java.util.Vector;
|
11
|
13
|
|
12
|
14
|
/**
|
13
|
15
|
* Created by robin on 9/15/16.
|
14
|
16
|
*/
|
15
|
17
|
public class ServerApplication {
|
16
|
18
|
|
|
19
|
+ private Vector<AbstractCommandExecutor> _executors = null;
|
|
20
|
+
|
17
|
21
|
public static String ipToString(InetAddress ip) {
|
18
|
22
|
String ipString = ip.toString();
|
19
|
23
|
if (!ipString.isEmpty() && ipString.startsWith("/")) {
|
|
@@ -32,8 +36,15 @@ public class ServerApplication {
|
32
|
36
|
}
|
33
|
37
|
}
|
34
|
38
|
|
35
|
|
- public static void run(int port, String sourceDir, String classDir, String logFile)
|
|
39
|
+ public void run(int port, String sourceDir, String classDir, String logFile)
|
36
|
40
|
{
|
|
41
|
+ try {
|
|
42
|
+ loadExecutors();
|
|
43
|
+ } catch (ClassNotFoundException e) {
|
|
44
|
+ System.err.format("Failed to load executors: %s\n", e.getMessage());
|
|
45
|
+ System.exit(1);
|
|
46
|
+ }
|
|
47
|
+
|
37
|
48
|
String host = "0.0.0.0";
|
38
|
49
|
InetAddress ip = null;
|
39
|
50
|
try {
|
|
@@ -43,7 +54,7 @@ public class ServerApplication {
|
43
|
54
|
}
|
44
|
55
|
if (ip == null) {
|
45
|
56
|
System.err.format("Failed to resolve '%s'\n", host);
|
46
|
|
- System.exit(1);
|
|
57
|
+ System.exit(2);
|
47
|
58
|
}
|
48
|
59
|
String ipString = ipToString(ip);
|
49
|
60
|
|
|
@@ -53,14 +64,27 @@ public class ServerApplication {
|
53
|
64
|
runServer(serverSocket);
|
54
|
65
|
} catch (IOException e) {
|
55
|
66
|
System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
|
56
|
|
- System.exit(2);
|
|
67
|
+ System.exit(3);
|
57
|
68
|
}
|
58
|
69
|
}
|
59
|
70
|
|
60
|
|
- public static void runServer(ServerSocket serverSocket)
|
|
71
|
+ private void loadExecutors() throws ClassNotFoundException
|
61
|
72
|
{
|
62
|
|
- boolean stop = false;
|
63
|
|
- while (!stop) {
|
|
73
|
+ _executors = new Vector<>();
|
|
74
|
+ Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
|
|
75
|
+ for (Class c : classes) {
|
|
76
|
+ try {
|
|
77
|
+ AbstractCommandExecutor executor = (AbstractCommandExecutor)c.newInstance();
|
|
78
|
+ _executors.add(executor);
|
|
79
|
+ } catch (Exception e) {
|
|
80
|
+ System.err.format("Failed to load executor '%s': %s\n", c.getName(), e.getMessage());
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ private void runServer(ServerSocket serverSocket)
|
|
86
|
+ {
|
|
87
|
+ while (true) {
|
64
|
88
|
Socket client = null;
|
65
|
89
|
try {
|
66
|
90
|
client = serverSocket.accept();
|
|
@@ -85,7 +109,7 @@ public class ServerApplication {
|
85
|
109
|
}
|
86
|
110
|
}
|
87
|
111
|
|
88
|
|
- private static void handleClient(Socket client)
|
|
112
|
+ private void handleClient(Socket client)
|
89
|
113
|
{
|
90
|
114
|
String str;
|
91
|
115
|
try {
|
|
@@ -101,11 +125,42 @@ public class ServerApplication {
|
101
|
125
|
}
|
102
|
126
|
else {
|
103
|
127
|
System.out.format("Received command: %s\n", command.getCommandName());
|
|
128
|
+
|
|
129
|
+ handleCommand(command, client);
|
|
130
|
+ }
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ private AbstractCommandExecutor getExecutor(Command command)
|
|
134
|
+ {
|
|
135
|
+ for (AbstractCommandExecutor executor : _executors) {
|
|
136
|
+ if (executor.getCommandName().equals(command.getCommandName())) {
|
|
137
|
+ return executor;
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+ return null;
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ private void handleCommand(Command command, Socket client)
|
|
144
|
+ {
|
|
145
|
+ String data;
|
|
146
|
+ AbstractCommandExecutor executor = getExecutor(command);
|
|
147
|
+ if (executor == null) {
|
|
148
|
+ data = String.format("Unknown command '%s'", command.getCommandName());
|
|
149
|
+ System.err.println(data);
|
|
150
|
+ }
|
|
151
|
+ else {
|
104
|
152
|
try {
|
105
|
|
- client.getOutputStream().write("test\n".getBytes());
|
106
|
|
- } catch (IOException e) {
|
107
|
|
- e.printStackTrace();
|
|
153
|
+ data = executor.run(command);
|
|
154
|
+ } catch (Exception e) {
|
|
155
|
+ data = String.format("Error when handling command: %s", e.getMessage());
|
|
156
|
+ System.err.println(data);
|
108
|
157
|
}
|
109
|
158
|
}
|
|
159
|
+
|
|
160
|
+ try {
|
|
161
|
+ client.getOutputStream().write(String.format("%s\n", data).getBytes());
|
|
162
|
+ } catch (IOException e) {
|
|
163
|
+ e.printStackTrace();
|
|
164
|
+ }
|
110
|
165
|
}
|
111
|
166
|
}
|