123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package com.uqac.rthoni.com.java_rmi.server;
-
- import com.uqac.rthoni.java_rmi.common.Command;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
-
- /**
- * Created by robin on 9/15/16.
- */
- public class ServerApplication {
-
- public static String ipToString(InetAddress ip) {
- String ipString = ip.toString();
- if (!ipString.isEmpty() && ipString.startsWith("/")) {
- ipString = ipString.substring(1);
- }
- return ipString;
- }
-
- public static String getFullIp(InetAddress ip) {
- try {
- InetAddress address = InetAddress.getByName(ip.toString().split("/")[1]);
- return String.format("%s/%s", address.getCanonicalHostName(), ipToString(ip));
- }
- catch (Exception e) {
- return ipToString(ip);
- }
- }
-
- public static void run(int port, String sourceDir, String classDir, String logFile)
- {
- String host = "0.0.0.0";
- InetAddress ip = null;
- try {
- ip = InetAddress.getByName(host);
- }
- catch (Exception e) {
- }
- if (ip == null) {
- System.err.format("Failed to resolve '%s'\n", host);
- System.exit(1);
- }
- String ipString = ipToString(ip);
-
- try {
- ServerSocket serverSocket = new ServerSocket(port, 1, ip);
- System.out.format("Listening for clients on %s:%d...\n", ipString, port);
- runServer(serverSocket);
- } catch (IOException e) {
- System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
- System.exit(2);
- }
- }
-
- public static void runServer(ServerSocket serverSocket)
- {
- boolean stop = false;
- while (!stop) {
- Socket client = null;
- try {
- client = serverSocket.accept();
- }
- catch (Exception e) {
- System.err.format("Failed to accept client: %s\n", e.getMessage());
- }
- if (client != null) {
- String ipString = getFullIp(client.getInetAddress());
- System.out.format("New client: %s:%d\n", ipString, client.getPort());
- try {
- handleClient(client);
- } catch (Exception e) {
- System.out.format("Error when handling client: %s\n", e.getMessage());
- }
- try {
- client.close();
- } catch (Exception e) {
- }
- System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
- }
- }
- }
-
- private static void handleClient(Socket client)
- {
- String str;
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
- str = in.readLine();
- } catch (IOException e) {
- System.err.format("Failed to read from client: %s", e.getMessage());
- return;
- }
- Command command = Command.fromString(str);
- if (command == null) {
- System.err.format("Failed to deserialize command: %s\n", str);
- }
- else {
- System.out.format("Received command: %s\n", command.getCommandName());
- try {
- client.getOutputStream().write("test\n".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
|