Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. // MessageReceiver.java (partial implementation) by scott
  2.  
  3. /**
  4. * This class implements the receiver side of the data link layer.
  5. * <P>
  6. * Note that the source code supplied contains only a partial
  7. * implementation. Your completed version must be submitted for
  8. * assessment.
  9. * <P>
  10. * To complete this class, you only need to finish the implementation
  11. * of the receiveMessage method. No other part of this file needs to
  12. * be changed. Don't alter the constructor or the interface of any
  13. * public method. You may add new private methods, if you wish, but
  14. * do not create any new classes as only this file will be processed
  15. * when your work is marked.
  16. */
  17.  
  18. public class MessageReceiver
  19. {
  20. // Attributes --------------------------------------------------------
  21.  
  22. private FrameReceiver physicalLayer; // physical layer object
  23. private boolean quiet; // true=quiet mode (suppress prompts and status info)
  24.  
  25. // You may add additional attributes
  26.  
  27. // -------------------------------------------------------------------
  28.  
  29. /**
  30. * MessageReceiver constructor (do not alter this)
  31. * @param physicalLayer physical layer object with frame receiver service
  32. * (this will already have been created and initialized by TestReceiver)
  33. * @param quiet true for quiet mode which suppresses prompts and status info
  34. */
  35.  
  36. public MessageReceiver(FrameReceiver physicalLayer, boolean quiet)
  37. {
  38. // Initialize attributes and report status
  39.  
  40. this.physicalLayer = physicalLayer;
  41. this.quiet = quiet;
  42. if (!quiet)
  43. System.out.println("Data link layer ready");
  44. }
  45.  
  46. // -------------------------------------------------------------------
  47.  
  48.  
  49. /**
  50. * Receive a message (this is the only method you need to modify)
  51. * @return the message received, or null if the end of the input stream
  52. * has been reached
  53. * @throws ProtocolException immediately if an error is detected,
  54. * such as a corrupt frame being received, and without attempting
  55. * to read any further frames.
  56. */
  57.  
  58. public String receiveMessage() throws ProtocolException
  59. {
  60.  
  61. String payload = "";
  62. String previousType = "E";
  63. String frame = "";
  64. while (true)
  65. {
  66. frame = physicalLayer.receiveFrame();
  67. validateFrame(frame);
  68.  
  69. // It's safe to do this past this point as the format of the
  70. // frame has been checked and found to be OK
  71. int payloadSize = Integer.parseInt(frame.substring(3,4));
  72. String payloadType = frame.substring(1,2);
  73.  
  74. // This is a start or continuation frame
  75. if (payloadType == "D")
  76. payload = payload + frame.substring(6, 6+payloadSize);
  77.  
  78. // This must be the final frame in the message
  79. else
  80. return payload + frame.substring(6, 6+payloadSize);
  81. }
  82.  
  83. if (!quiet)
  84. System.out.println("Frame received => " + frame);
  85.  
  86. return frame;
  87.  
  88. }
  89.  
  90. /**
  91. * Create an appropriately formatted frame when provided with a message
  92. * @param submsg The portion of the full message to send in this frame
  93. * @param finalFrame Whether this frame is the final frame in a sequence
  94. */
  95. private String formatFrame(String submsg, String frameType)
  96. {
  97. String frame = "";
  98. String msgLength = String.format("%02d", submsg.length());
  99. frame = frame + frameType + "-" + msgLength + "-" + submsg + "-";
  100. String checksum = calculateChecksum(frame);
  101. frame = "<" + frame + checksum + ">";
  102.  
  103. return frame;
  104. }
  105.  
  106. /**
  107. * Calculate the checksum of a frame payload
  108. * @param frame The frame string for which to calculate the checksum
  109. */
  110. private String calculateChecksum(String frame)
  111. {
  112. int sum = 0;
  113.  
  114. for (int i=0; i<frame.length(); i++)
  115. {
  116. sum += (int)frame.charAt(i);
  117. }
  118.  
  119. String csum = String.format("%02d", sum);
  120. return csum.substring(csum.length()-2, csum.length());
  121.  
  122. }
  123.  
  124. /**
  125. * Validate that a frame conforms to the protocol specified in the protocol
  126. * documentation.
  127. * @return void
  128. * @throws ProtocolException (with a text description) if any error
  129. * in the protocol is found
  130. */
  131. private void validateFrame(String frame)
  132. {
  133.  
  134. // If frame size is greater than 109 (99 character payload + 10 character
  135. // overhead), throw an error
  136. if (frame.length() > 109 || frame.length() < 10)
  137. throw new ProtocolException("Frame is too large (> 109B) or small (<10B)");
  138.  
  139. // If the payload size can't be converted into an integer, throw new
  140. int payloadSize;
  141. try
  142. {
  143. payloadSize = Integer.parseInt(frame.substring(3,4));
  144. }
  145. catch (Exception error)
  146. {
  147. throw new ProtocolException("Payload size segment is not an integer!");
  148. }
  149.  
  150. // Check that the type of frame is appropriate
  151. String payloadType = frame.substring(1,2);
  152. if (! (payloadType == "D" || payloadType =="E"))
  153. throw new ProtocolException("Payload type not supported.");
  154.  
  155. // Check that the entire packet format is correct
  156. String newFrame = formatFrame(frame.substring(6,6+payloadSize), payloadType);
  157.  
  158. if (frame != newFrame)
  159. throw new ProtocolException("Error in frame format or checksum.");
  160. }
  161.  
  162. }
Add Comment
Please, Sign In to add comment