3434import org.itech.ahb.lib.util.LogUtil;
3535import org.itech.ahb.lib.util.ThreadUtil;
3636
37- //If this class gets too complicated, separate out the LISA-01 and E1382-95 protocols
37+ //If this class gets too complicated, separate out the LISA-01 and E1382-95 protocols into separate classes
38+ /**
39+ * This class is a general communicator that can send and receive ASTM messages
40+ * over protocols such as LIS01-A and E1382-95, as well as non-compliant transmissions
41+ * where the sender sends ASTM messages by sending the message character by character
42+ * without using control characters, frame numbers, or checksums.
43+ */
3844@Slf4j
3945public class GeneralASTMCommunicator implements Communicator {
4046
47+ /**
48+ * Errors that can occur when receiving a frame.
49+ */
4150 public enum FrameError {
51+ /**
52+ * The frame number is expected to be between 0-7 and increase by 1 each frame mod 8
53+ */
4254 WRONG_FRAME_NUMBER,
55+ /**
56+ * Frame exceeds the maximum frame size
57+ */
4358 MAX_SIZE_EXCEEDED,
59+ /**
60+ * Received a character in the the Data Content of Message that is not allowed
61+ */
4462 ILLEGAL_CHAR,
63+ /**
64+ * Checksum does not match the calculated checksum
65+ */
4566 BAD_CHECKSUM,
67+ /**
68+ * Start caracter is not the expected start character
69+ */
4670 ILLEGAL_START,
71+ /**
72+ * End character is not the expected end character
73+ */
4774 ILLEGAL_END
4875 }
4976
@@ -102,7 +129,8 @@ public enum FrameError {
102129 private static final int MAX_COMMUNICATOR_ID_COUNTER = 1024;
103130
104131 /**
105- * @return int
132+ * Gets a new ID for this communicator instance after incrementing the counter
133+ * @return the new ID
106134 */
107135 private final int incrementAndGetId() {
108136 return COMMUNICATOR_ID_COUNTER.accumulateAndGet(
@@ -120,10 +148,21 @@ private final int incrementAndGetId() {
120148 private ASTMVersion astmVersion;
121149 private Boolean receiveEstablished = false;
122150
151+ /**
152+ * Constructor for a GeneralASTMCommunicator, will assume the ASTM version is LIS01-A
153+ * @param astmInterpreterFactory a factory that will create an interpreter for a received message
154+ * @param socket the socket to communicate on
155+ */
123156 public GeneralASTMCommunicator(ASTMInterpreterFactory astmInterpreterFactory, Socket socket) throws IOException {
124157 this(astmInterpreterFactory, socket, ASTMVersion.LIS01_A);
125158 }
126159
160+ /**
161+ * Constructor for a GeneralASTMCommunicator with a specific ASTM version
162+ * @param astmInterpreterFactory a factory that will create an interpreter for a received message
163+ * @param socket the socket to communicate on
164+ * @param astmVersion the ASTM version to communicate over
165+ */
127166 public GeneralASTMCommunicator(ASTMInterpreterFactory astmInterpreterFactory, Socket socket, ASTMVersion astmVersion)
128167 throws IOException {
129168 communicatorId = Integer.toString(incrementAndGetId());
@@ -137,9 +176,6 @@ public GeneralASTMCommunicator(ASTMInterpreterFactory astmInterpreterFactory, So
137176 this.astmVersion = astmVersion;
138177 }
139178
140- /**
141- * @return String
142- */
143179 @Override
144180 public String getID() {
145181 return communicatorId;
@@ -226,6 +262,15 @@ public Boolean establishmentReceive() throws IOException, InterruptedException {
226262 }
227263 }
228264
265+ /**
266+ * Receives an ASTM message that is being sent non-compliantly (not using the ASTM transmission protocol).
267+ * Instead the message is read character by character until the termination record is reached.
268+ *
269+ * @return the received ASTM message.
270+ * @throws FrameParsingException if there is an error parsing the frame.
271+ * @throws ASTMCommunicationException if there is a communication error in the ASTM transmission protocol.
272+ * @throws IOException if an I/O error occurs.
273+ */
229274 private ASTMMessage receiveInNonCompliantMode()
230275 throws IOException, ASTMCommunicationException, FrameParsingException {
231276 final FutureTask<ASTMMessage> recievedMessageFuture = new FutureTask<>(receiveIncompliantMessage());
@@ -241,6 +286,15 @@ private ASTMMessage receiveInNonCompliantMode()
241286 throw new ASTMCommunicationException("non compliant mode could not return a valid ASTM message");
242287 }
243288
289+ /**
290+ * Callable that recieves an ASTM message that is being sent non-compliantly (not using the ASTM transmission protocol).
291+ * Instead the message is read character by character until the termination record is reached.
292+ *
293+ * @return the received ASTM message.
294+ * @throws FrameParsingException if there is an error parsing the frame.
295+ * @throws ASTMCommunicationException if there is a communication error in the ASTM transmission protocol.
296+ * @throws IOException if an I/O error occurs.
297+ */
244298 private Callable<ASTMMessage> receiveIncompliantMessage() {
245299 return new Callable<ASTMMessage>() {
246300 @Override
@@ -270,6 +324,15 @@ public ASTMMessage call() throws IOException, ASTMCommunicationException {
270324 };
271325 }
272326
327+ /**
328+ * Receives an ASTM message that is being sent compliantly over the ASTM transmission protocol.
329+ * This version supports LISA-01 and E1382-95 protocols
330+ *
331+ * @return the received ASTM message.
332+ * @throws FrameParsingException if there is an error parsing the frame.
333+ * @throws ASTMCommunicationException if there is a communication error in the ASTM transmission protocol.
334+ * @throws IOException if an I/O error occurs.
335+ */
273336 private ASTMMessage receiveInCompliantMode() throws IOException, ASTMCommunicationException, FrameParsingException {
274337 List<ASTMFrame> frames = new ArrayList<>();
275338 int i = 0;
@@ -332,6 +395,13 @@ private ASTMMessage receiveInCompliantMode() throws IOException, ASTMCommunicati
332395 return astmInterpreterFactory.createInterpreterForFrames(frames).interpretFramesToASTMMessage(frames);
333396 }
334397
398+ /**
399+ * Creates a callable task that reads a single frame and adds it to the list of frames.
400+ *
401+ * @param frames the list of frames that this task will add the next frame to.
402+ * @return a callable task that returns the information about the frame that was read
403+ * @throws IOException if an I/O error occurs.
404+ */
335405 private Callable<ReadFrameInfo> receiveNextFrameTask(List<ASTMFrame> frames) throws IOException {
336406 return new Callable<ReadFrameInfo>() {
337407 @Override
@@ -358,6 +428,15 @@ public ReadFrameInfo call() throws IOException, InterruptedException {
358428 };
359429 }
360430
431+ /**
432+ * Read the next frame from the reader and add it to the list of frames.
433+ *
434+ * @param frames the list of frames that this task will add the next frame to.
435+ * @param expectedFrameNumber the expected number that the next frame whouls start with.
436+ * @return a Set of issues with the frame that was received. This will be empty if no issue was detected.
437+ * @throws IOException if an I/O error occurs.
438+ * @throws InterruptedException if the operation is interrupted.
439+ */
361440 private Set<FrameError> readNextCompliantFrame(List<ASTMFrame> frames, int expectedFrameNumber)
362441 throws IOException, InterruptedException {
363442 log.debug("reading frame...");
@@ -443,6 +522,14 @@ private Set<FrameError> readNextCompliantFrame(List<ASTMFrame> frames, int expec
443522 return frameErrors;
444523 }
445524
525+ /**
526+ * Read the next ASTM record and add it to the list of ASTM records
527+ *
528+ * @param records the list of records that this method will add to
529+ * @return a Set of issues with the "frame" (record in this case) that was received. for This will be empty if no issue was detected.
530+ * @throws IOException if an I/O error occurs.
531+ * @throws InterruptedException if the operation is interrupted.
532+ */
446533 private Set<FrameError> readNextIncompliantRecord(List<ASTMRecord> records) throws IOException, InterruptedException {
447534 log.debug("reading incompliant record...");
448535 Set<FrameError> recordErrors = new HashSet<>();
@@ -565,6 +652,11 @@ public SendResult sendProtocol(ASTMMessage message)
565652 return new SendResult(false, false);
566653 }
567654
655+ /**
656+ * Sends the signal to establish communication with the receiver, beginning the "establishment phase" of the ASTM transmission protocol.
657+ *
658+ * @return a callable task that returns the response character from the receiver.
659+ */
568660 private Callable<Character> establishmentTaskSend() {
569661 return new Callable<Character>() {
570662 @Override
@@ -604,6 +696,12 @@ public Character call() throws IOException, InterruptedException {
604696 };
605697 }
606698
699+ /**
700+ * Creates a callable task that sends the next ASTM frame to the receiver.
701+ *
702+ * @param frame the ASTM frame to send to the reciever.
703+ * @return a callable task that sends the next ASTM frame and returns true if the frame was sent successfully.
704+ */
607705 private Callable<Boolean> sendNextFrameTask(ASTMFrame frame) {
608706 return new Callable<Boolean>() {
609707 @Override
@@ -629,19 +727,39 @@ public Boolean call() {
629727 };
630728 }
631729
730+ /**
731+ * Sends the termination signal to enter the termination phase of the ASTM transmission protocol.
732+ */
632733 private void terminationSignal() {
633734 log.debug("sending '" + LogUtil.convertForDisplay(EOT) + "' as termination for exchange");
634735 writer.append(EOT);
635736 writer.flush();
636737 }
637738
739+ /**
740+ * Sends the termination signal to enter the termination phase of the ASTM transmission protocol.
741+ *
742+ * @param checksum the checksum that the calculated checksum should match.
743+ * @param frameNumber the frame number character to be used in the checksum calculation.
744+ * @param frame the frame to be used in the checksum calculation.
745+ * @param frameTerminator the frame terminator character to be used in the checksum calculation.
746+ * @return true if the checksum matches the calculated checksum.
747+ */
638748 private boolean checksumFits(String checksum, char frameNumber, String frame, char frameTerminator) {
639749 log.trace(
640750 "received: '" + LogUtil.convertForDisplay(checksum) + "'. Expecting 2 base 16 checksum characters [00-FF]"
641751 );
642752 return checksum.equals(checksumCalc(frameNumber, frame, frameTerminator));
643753 }
644754
755+ /**
756+ * Calculates the checksum from the parameters.
757+ *
758+ * @param frameNumber the frame number character to be used in the checksum calculation.
759+ * @param frame the frame to be used in the checksum calculation.
760+ * @param frameTerminator the frame terminator character to be used in the checksum calculation.
761+ * @return the calculated checksum as a String.
762+ */
645763 private String checksumCalc(char frameNumber, String frame, char frameTerminator) {
646764 int computedChecksum = 0;
647765 computedChecksum += (byte) frameNumber;
@@ -655,6 +773,9 @@ private String checksumCalc(char frameNumber, String frame, char frameTerminator
655773 return checksum;
656774 }
657775
776+ /**
777+ * Object for holding information about the reading a frame.
778+ */
658779 @Data
659780 @AllArgsConstructor
660781 private class ReadFrameInfo {
0 commit comments