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 4.2KB

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