Procházet zdrojové kódy

server handle clients; clients run all commands

develop
Robin Thoni před 8 roky
rodič
revize
5c7c29dcf1

+ 104
- 1
client/src/main/java/com/uqac/rthoni/com/java_rmi/client/ClientApplication.java Zobrazit soubor

@@ -1,12 +1,115 @@
1 1
 package com.uqac.rthoni.com.java_rmi.client;
2 2
 
3
+import com.uqac.rthoni.java_rmi.common.Command;
4
+
5
+import java.io.*;
6
+import java.net.Socket;
7
+import java.util.Vector;
8
+
3 9
 /**
4 10
  * Created by robin on 9/15/16.
5 11
  */
6 12
 public class ClientApplication {
7 13
 
14
+    public static Vector<Command> readInputFile(String file)
15
+    {
16
+        FileInputStream fileInputStream = null;
17
+        try {
18
+            fileInputStream = new FileInputStream(file);
19
+        } catch (FileNotFoundException e) {
20
+            System.err.format("Failed to open input file %s: %s\n", file, e.getMessage());
21
+            System.exit(1);
22
+        }
23
+        Vector<Command> lines = new Vector<>();
24
+        try {
25
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
26
+            String line;
27
+             do {
28
+                line = bufferedReader.readLine();
29
+                if (line != null && !line.isEmpty()) {
30
+                    Command command = Command.fromString(line);
31
+                    if (command != null) {
32
+                        lines.add(command);
33
+                    }
34
+                }
35
+            } while (line != null);
36
+        }
37
+        catch (Exception e) {
38
+            System.err.format("Failed to read input file %s: %s\n", file, e.getMessage());
39
+            System.exit(2);
40
+        }
41
+        return lines;
42
+    }
43
+
8 44
     public static void run(String host, int port, String inputFile, String outputFile)
9 45
     {
10
-        System.out.println(String.format("Connecting to %s:%d...", host, port));
46
+        System.out.format("Reading input file %s...\n", inputFile);
47
+        Vector<Command> commands = readInputFile(inputFile);
48
+        FileOutputStream outputStream = null;
49
+        try {
50
+            outputStream = new FileOutputStream(outputFile);
51
+        } catch (Exception e) {
52
+            System.err.format("Failed to open output file %s: %s\n", outputFile, e.getMessage());
53
+            System.exit(3);
54
+        }
55
+        for (Command command : commands) {
56
+            runCommand(command, host, port, outputStream);
57
+        }
58
+        try {
59
+            outputStream.close();
60
+        } catch (IOException e) {
61
+        }
62
+    }
63
+
64
+    private static void runCommand(Command command, String host, int port, OutputStream outputStream)
65
+    {
66
+        System.out.format("Running command %s...\n", command.getCommandName());
67
+        System.out.format("Connecting to %s:%d...\n", host, port);
68
+        Socket server = null;
69
+        try {
70
+            server = new Socket(host, port);
71
+        }
72
+        catch (Exception e) {
73
+            System.err.format("Failed to connect to %s:%d: %s\n", host, port, e.getMessage());
74
+            System.exit(4);
75
+        }
76
+        try {
77
+            String str = String.format("%s\n", command.toString());
78
+            server.getOutputStream().write(str.getBytes());
79
+            server.getOutputStream().flush();
80
+        } catch (IOException e) {
81
+            System.err.format("Failed to send command: %s\n", e.getMessage());
82
+            System.exit(5);
83
+        }
84
+        String output = "";
85
+        try {
86
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(server.getInputStream()));
87
+            StringBuilder outputBuffer = new StringBuilder();
88
+            String line;
89
+            do {
90
+                line = bufferedReader.readLine();
91
+                if (line != null) {
92
+                    outputBuffer.append(line);
93
+                    outputBuffer.append("\n");
94
+                }
95
+            } while (line != null);
96
+            output = outputBuffer.toString();
97
+        }
98
+        catch (Exception e) {
99
+            System.err.format("Failed to read command output: %s\n", e.getMessage());
100
+            System.exit(6);
101
+        }
102
+
103
+        try {
104
+            outputStream.write(output.getBytes());
105
+        } catch (IOException e) {
106
+            System.err.format("Failed to write command output: %s\n", e.getMessage());
107
+            System.exit(7);
108
+        }
109
+
110
+        try {
111
+            server.close();
112
+        } catch (IOException e) {
113
+        }
11 114
     }
12 115
 }

+ 20
- 13
server/src/main/java/com/uqac/rthoni/com/java_rmi/server/ServerApplication.java Zobrazit soubor

@@ -53,6 +53,7 @@ public class ServerApplication {
53 53
             runServer(serverSocket);
54 54
         } catch (IOException e) {
55 55
             System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
56
+            System.exit(2);
56 57
         }
57 58
     }
58 59
 
@@ -60,26 +61,27 @@ public class ServerApplication {
60 61
     {
61 62
         boolean stop = false;
62 63
         while (!stop) {
63
-            Socket client;
64
+            Socket client = null;
64 65
             try {
65 66
                 client = serverSocket.accept();
66 67
             }
67 68
             catch (Exception e) {
68 69
                 System.err.format("Failed to accept client: %s\n", e.getMessage());
69
-                break;
70 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) {
71
+            if (client != null) {
72
+                String ipString = getFullIp(client.getInetAddress());
73
+                System.out.format("New client: %s:%d\n", ipString, client.getPort());
74
+                try {
75
+                    handleClient(client);
76
+                } catch (Exception e) {
77
+                    System.out.format("Error when handling client: %s\n", e.getMessage());
78
+                }
79
+                try {
80
+                    client.close();
81
+                } catch (Exception e) {
82
+                }
83
+                System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
81 84
             }
82
-            System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
83 85
         }
84 86
     }
85 87
 
@@ -99,6 +101,11 @@ public class ServerApplication {
99 101
         }
100 102
         else {
101 103
             System.out.format("Received command: %s\n", command.getCommandName());
104
+            try {
105
+                client.getOutputStream().write("test\n".getBytes());
106
+            } catch (IOException e) {
107
+                e.printStackTrace();
108
+            }
102 109
         }
103 110
     }
104 111
 }

Načítá se…
Zrušit
Uložit