You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClientApplication.java 3.8KB

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