Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MessageReceiver.java (partial implementation) by scott
- /**
- * This class implements the receiver side of the data link layer.
- * <P>
- * Note that the source code supplied contains only a partial
- * implementation. Your completed version must be submitted for
- * assessment.
- * <P>
- * To complete this class, you only need to finish the implementation
- * of the receiveMessage method. No other part of this file needs to
- * be changed. Don't alter the constructor or the interface of any
- * public method. You may add new private methods, if you wish, but
- * do not create any new classes as only this file will be processed
- * when your work is marked.
- */
- public class MessageReceiver
- {
- // Attributes --------------------------------------------------------
- private FrameReceiver physicalLayer; // physical layer object
- private boolean quiet; // true=quiet mode (suppress prompts and status info)
- // You may add additional attributes
- // -------------------------------------------------------------------
- /**
- * MessageReceiver constructor (do not alter this)
- * @param physicalLayer physical layer object with frame receiver service
- * (this will already have been created and initialized by TestReceiver)
- * @param quiet true for quiet mode which suppresses prompts and status info
- */
- public MessageReceiver(FrameReceiver physicalLayer, boolean quiet)
- {
- // Initialize attributes and report status
- this.physicalLayer = physicalLayer;
- this.quiet = quiet;
- if (!quiet)
- System.out.println("Data link layer ready");
- }
- // -------------------------------------------------------------------
- /**
- * Receive a message (this is the only method you need to modify)
- * @return the message received, or null if the end of the input stream
- * has been reached
- * @throws ProtocolException immediately if an error is detected,
- * such as a corrupt frame being received, and without attempting
- * to read any further frames.
- */
- public String receiveMessage() throws ProtocolException
- {
- String payload = "";
- String previousType = "E";
- String frame = "";
- while (true)
- {
- frame = physicalLayer.receiveFrame();
- validateFrame(frame);
- // It's safe to do this past this point as the format of the
- // frame has been checked and found to be OK
- int payloadSize = Integer.parseInt(frame.substring(3,4));
- String payloadType = frame.substring(1,2);
- // This is a start or continuation frame
- if (payloadType == "D")
- payload = payload + frame.substring(6, 6+payloadSize);
- // This must be the final frame in the message
- else
- return payload + frame.substring(6, 6+payloadSize);
- }
- if (!quiet)
- System.out.println("Frame received => " + frame);
- return frame;
- }
- /**
- * Create an appropriately formatted frame when provided with a message
- * @param submsg The portion of the full message to send in this frame
- * @param finalFrame Whether this frame is the final frame in a sequence
- */
- private String formatFrame(String submsg, String frameType)
- {
- String frame = "";
- String msgLength = String.format("%02d", submsg.length());
- frame = frame + frameType + "-" + msgLength + "-" + submsg + "-";
- String checksum = calculateChecksum(frame);
- frame = "<" + frame + checksum + ">";
- return frame;
- }
- /**
- * Calculate the checksum of a frame payload
- * @param frame The frame string for which to calculate the checksum
- */
- private String calculateChecksum(String frame)
- {
- int sum = 0;
- for (int i=0; i<frame.length(); i++)
- {
- sum += (int)frame.charAt(i);
- }
- String csum = String.format("%02d", sum);
- return csum.substring(csum.length()-2, csum.length());
- }
- /**
- * Validate that a frame conforms to the protocol specified in the protocol
- * documentation.
- * @return void
- * @throws ProtocolException (with a text description) if any error
- * in the protocol is found
- */
- private void validateFrame(String frame)
- {
- // If frame size is greater than 109 (99 character payload + 10 character
- // overhead), throw an error
- if (frame.length() > 109 || frame.length() < 10)
- throw new ProtocolException("Frame is too large (> 109B) or small (<10B)");
- // If the payload size can't be converted into an integer, throw new
- int payloadSize;
- try
- {
- payloadSize = Integer.parseInt(frame.substring(3,4));
- }
- catch (Exception error)
- {
- throw new ProtocolException("Payload size segment is not an integer!");
- }
- // Check that the type of frame is appropriate
- String payloadType = frame.substring(1,2);
- if (! (payloadType == "D" || payloadType =="E"))
- throw new ProtocolException("Payload type not supported.");
- // Check that the entire packet format is correct
- String newFrame = formatFrame(frame.substring(6,6+payloadSize), payloadType);
- if (frame != newFrame)
- throw new ProtocolException("Error in frame format or checksum.");
- }
- }
Add Comment
Please, Sign In to add comment