|
@@ -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
|
}
|