Network programming assignment for University
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

102 rindas
3.5 KiB

  1. /**
  2. * @file Image.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.vmodem;
  8. /** @name imports */
  9. /** @{ */
  10. import java.io.*;
  11. /** @} */
  12. /**
  13. * @class Image
  14. *
  15. * Class to used for the error free and non error free image requests
  16. */
  17. class Image {
  18. static final int IMAGE_BUFFER_SIZE = 256*1024; /**< image buffer size */
  19. static final byte[] IMAGE_BEGIN = {(byte)0xFF, (byte)0xD8}; /**< jpeg image begin pattern */
  20. static final byte[] IMAGE_END = {(byte)0xFF, (byte)0xD9}; /**< jpeg image end pattern */
  21. private Com com_; /**< Reference to communication module */
  22. private Log log_; /**< Reference to logging module */
  23. private Transaction transaction_; /**< A transaction object to used as a buffer for all requests */
  24. private String filename_; /**< The filename to store */
  25. private int items_; /**< how many images to fetch */
  26. private byte[] code_; /**< The image request code for the virtual lab */
  27. /**
  28. * Basic constructor
  29. * @param com The com module to use
  30. * @param log The log module to use
  31. * @param code The request code
  32. * @param filename The filename
  33. * @param items How many items to fetch
  34. */
  35. Image (Com com, Log log, byte[] code, String filename, int items) {
  36. com_ = com;
  37. log_ = log;
  38. // Allocate memory for the response
  39. transaction_= new Transaction(null, new byte[IMAGE_BUFFER_SIZE]);
  40. filename_ = filename;
  41. items_ = items;
  42. code_ = code;
  43. }
  44. /** Get function for the code */
  45. void code (byte[] code) { code_ = code; }
  46. /**
  47. * Functionality to drain the response buffer for the welcome message
  48. * from the server
  49. * @param code The code that we will use (for printing, not sending)
  50. */
  51. void caption (byte[] code) {
  52. String line;
  53. line = "Running video decoder with: " + new String(code);
  54. log_.write(line, true);
  55. transaction_ = com_.request (transaction_, null, null, null, false);
  56. line = new String(transaction_.getResponse());
  57. log_.out(line);
  58. }
  59. /**
  60. * Main transaction loop. It send requests to server, get the response
  61. * and log the procedure while doing it.
  62. */
  63. void run () {
  64. String file, line;
  65. BufferedOutputStream ostream;
  66. for (int i =1 ; i<= items_ ; ++i) {
  67. // Make the filename string
  68. if (items_>1)
  69. file = filename_ + "_" + i + ".jpg";
  70. else
  71. file = filename_ + ".jpg";
  72. line = new String(code_) + ": ";
  73. log_.write(line);
  74. transaction_ = com_.request(transaction_, code_, IMAGE_BEGIN, IMAGE_END, true);
  75. line = "File= " + file
  76. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]"
  77. + " Tt= " + (System.currentTimeMillis() - transaction_.departure) + " [msec]";
  78. log_.write(line);
  79. try {
  80. ostream = new BufferedOutputStream(new FileOutputStream(file));
  81. ostream.write(transaction_.response);
  82. ostream.flush();
  83. ostream.close();
  84. }
  85. catch (Exception exp) {
  86. System.err.println ("Error creating " + file + exp.getMessage());
  87. return;
  88. }
  89. }
  90. }
  91. }