|
@@ -1,13 +1,104 @@
|
1
|
1
|
package com.uqac.rthoni.com.java_rmi.server;
|
2
|
2
|
|
|
3
|
+import com.uqac.rthoni.java_rmi.common.Command;
|
|
4
|
+
|
|
5
|
+import java.io.BufferedReader;
|
|
6
|
+import java.io.IOException;
|
|
7
|
+import java.io.InputStreamReader;
|
|
8
|
+import java.net.InetAddress;
|
|
9
|
+import java.net.ServerSocket;
|
|
10
|
+import java.net.Socket;
|
|
11
|
+
|
3
|
12
|
/**
|
4
|
13
|
* Created by robin on 9/15/16.
|
5
|
14
|
*/
|
6
|
15
|
public class ServerApplication {
|
7
|
16
|
|
|
17
|
+ public static String ipToString(InetAddress ip) {
|
|
18
|
+ String ipString = ip.toString();
|
|
19
|
+ if (!ipString.isEmpty() && ipString.startsWith("/")) {
|
|
20
|
+ ipString = ipString.substring(1);
|
|
21
|
+ }
|
|
22
|
+ return ipString;
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public static String getFullIp(InetAddress ip) {
|
|
26
|
+ try {
|
|
27
|
+ InetAddress address = InetAddress.getByName(ip.toString().split("/")[1]);
|
|
28
|
+ return String.format("%s/%s", address.getCanonicalHostName(), ipToString(ip));
|
|
29
|
+ }
|
|
30
|
+ catch (Exception e) {
|
|
31
|
+ return ipToString(ip);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+
|
8
|
35
|
public static void run(int port, String sourceDir, String classDir, String logFile)
|
9
|
36
|
{
|
10
|
|
- String ip = "0.0.0.0";
|
11
|
|
- System.out.println(String.format("Listening for clients on %1s:%2d...", ip, port));
|
|
37
|
+ String host = "0.0.0.0";
|
|
38
|
+ InetAddress ip = null;
|
|
39
|
+ try {
|
|
40
|
+ ip = InetAddress.getByName(host);
|
|
41
|
+ }
|
|
42
|
+ catch (Exception e) {
|
|
43
|
+ }
|
|
44
|
+ if (ip == null) {
|
|
45
|
+ System.err.format("Failed to resolve '%s'\n", host);
|
|
46
|
+ System.exit(1);
|
|
47
|
+ }
|
|
48
|
+ String ipString = ipToString(ip);
|
|
49
|
+
|
|
50
|
+ try {
|
|
51
|
+ ServerSocket serverSocket = new ServerSocket(port, 1, ip);
|
|
52
|
+ System.out.format("Listening for clients on %s:%d...\n", ipString, port);
|
|
53
|
+ runServer(serverSocket);
|
|
54
|
+ } catch (IOException e) {
|
|
55
|
+ System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public static void runServer(ServerSocket serverSocket)
|
|
60
|
+ {
|
|
61
|
+ boolean stop = false;
|
|
62
|
+ while (!stop) {
|
|
63
|
+ Socket client;
|
|
64
|
+ try {
|
|
65
|
+ client = serverSocket.accept();
|
|
66
|
+ }
|
|
67
|
+ catch (Exception e) {
|
|
68
|
+ System.err.format("Failed to accept client: %s\n", e.getMessage());
|
|
69
|
+ break;
|
|
70
|
+ }
|
|
71
|
+ String ipString = getFullIp(client.getInetAddress());
|
|
72
|
+ System.out.format("New client: %s:%d\n", ipString, client.getPort());
|
|
73
|
+ try {
|
|
74
|
+ handleClient(client);
|
|
75
|
+ } catch (Exception e) {
|
|
76
|
+ System.out.format("Error when handling client: %s\n", e.getMessage());
|
|
77
|
+ }
|
|
78
|
+ try {
|
|
79
|
+ client.close();
|
|
80
|
+ } catch (Exception e) {
|
|
81
|
+ }
|
|
82
|
+ System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ private static void handleClient(Socket client)
|
|
87
|
+ {
|
|
88
|
+ String str;
|
|
89
|
+ try {
|
|
90
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
91
|
+ str = in.readLine();
|
|
92
|
+ } catch (IOException e) {
|
|
93
|
+ System.err.format("Failed to read from client: %s", e.getMessage());
|
|
94
|
+ return;
|
|
95
|
+ }
|
|
96
|
+ Command command = Command.fromString(str);
|
|
97
|
+ if (command == null) {
|
|
98
|
+ System.err.format("Failed to deserialize command: %s\n", str);
|
|
99
|
+ }
|
|
100
|
+ else {
|
|
101
|
+ System.out.format("Received command: %s\n", command.getCommandName());
|
|
102
|
+ }
|
12
|
103
|
}
|
13
|
104
|
}
|