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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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 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. OutputStream outputStream = null;
  44. if (outputFile.equals("-")) {
  45. outputStream = System.out;
  46. }
  47. else {
  48. try {
  49. outputStream = new FileOutputStream(outputFile);
  50. } catch (Exception e) {
  51. System.err.format("Failed to open output file %s: %s\n", outputFile, e.getMessage());
  52. System.exit(3);
  53. }
  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. private void runCommand(Command command, String host, int port, OutputStream outputStream)
  64. {
  65. System.out.format("Running command %s...\n", command.getCommandName());
  66. System.out.format("Connecting to %s:%d...\n", host, port);
  67. Socket server = null;
  68. try {
  69. server = new Socket(host, port);
  70. }
  71. catch (Exception e) {
  72. System.err.format("Failed to connect to %s:%d: %s\n", host, port, e.getMessage());
  73. System.exit(4);
  74. }
  75. try {
  76. String str = String.format("%s\n", command.toString());
  77. server.getOutputStream().write(str.getBytes());
  78. server.getOutputStream().flush();
  79. } catch (IOException e) {
  80. System.err.format("Failed to send command: %s\n", e.getMessage());
  81. System.exit(5);
  82. }
  83. String output = "";
  84. try {
  85. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(server.getInputStream()));
  86. StringBuilder outputBuffer = new StringBuilder();
  87. String line;
  88. do {
  89. line = bufferedReader.readLine();
  90. if (line != null) {
  91. outputBuffer.append(line);
  92. outputBuffer.append("\n");
  93. }
  94. } while (line != null);
  95. output = outputBuffer.toString();
  96. }
  97. catch (Exception e) {
  98. System.err.format("Failed to read command output: %s\n", e.getMessage());
  99. System.exit(6);
  100. }
  101. try {
  102. outputStream.write(output.getBytes());
  103. } catch (IOException e) {
  104. System.err.format("Failed to write command output: %s\n", e.getMessage());
  105. System.exit(7);
  106. }
  107. try {
  108. server.close();
  109. } catch (IOException e) {
  110. }
  111. }
  112. }