Skip to content

Commit f978818

Browse files
committed
improve logging, decouple components, allow multiple algorithms
1 parent e531a36 commit f978818

27 files changed

+505
-225
lines changed

astm-http-lib/pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.itech</groupId>
66
<artifactId>astm-http-lib</artifactId>
7-
<version>2.0.1</version>
7+
<version>2.1.0</version>
88

99
<properties>
1010
<java.version>21</java.version>
@@ -13,14 +13,12 @@
1313
</properties>
1414

1515
<dependencies>
16-
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
1716
<dependency>
1817
<groupId>jakarta.servlet</groupId>
1918
<artifactId>jakarta.servlet-api</artifactId>
2019
<version>6.0.0</version>
2120
<scope>provided</scope>
2221
</dependency>
23-
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
2422
<dependency>
2523
<groupId>org.projectlombok</groupId>
2624
<artifactId>lombok</artifactId>
@@ -37,6 +35,7 @@
3735
<artifactId>commons-lang3</artifactId>
3836
<version>3.14.0</version>
3937
</dependency>
38+
4039
</dependencies>
4140

4241
</project>

astm-http-lib/src/main/java/org/itech/ahb/lib/astm/servlet/ASTMHandlerMarshaller.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@
1111
@Slf4j
1212
public class ASTMHandlerMarshaller {
1313

14+
public enum MarshallerMode {
15+
ALL,
16+
FIRST
17+
}
18+
1419
private List<ASTMHandler> handlers;
20+
private MarshallerMode mode;
1521

16-
public ASTMHandlerMarshaller(List<ASTMHandler> handlers) {
22+
public ASTMHandlerMarshaller(List<ASTMHandler> handlers, MarshallerMode mode) {
1723
this.handlers = handlers;
24+
this.mode = mode;
1825
}
1926

2027
public HandleStatus handle(ASTMMessage message) {
@@ -24,7 +31,9 @@ public HandleStatus handle(ASTMMessage message) {
2431
if (handler.matches(message)) {
2532
log.debug("handler found for astm message: " + message.hashCode());
2633
messageHandlers.put(message, handler);
27-
break;
34+
if (mode == MarshallerMode.FIRST) {
35+
break;
36+
}
2837
}
2938
}
3039
if (!messageHandlers.containsKey(message)) {

astm-http-lib/src/main/java/org/itech/ahb/lib/astm/servlet/ASTMReceiveThread.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import java.io.IOException;
44
import java.net.Socket;
5-
import java.util.List;
65
import lombok.extern.slf4j.Slf4j;
7-
import org.itech.ahb.lib.common.ASTMInterpreterFactory;
86
import org.itech.ahb.lib.common.ASTMMessage;
97
import org.itech.ahb.lib.common.HandleStatus;
108
import org.itech.ahb.lib.common.exception.ASTMCommunicationException;
@@ -13,57 +11,39 @@
1311
@Slf4j
1412
public class ASTMReceiveThread extends Thread {
1513

16-
private Socket socket;
17-
private ASTMInterpreterFactory astmInterpreterFactory;
18-
private LIS01A2Communicator communicator;
14+
private final Socket socket;
15+
private final Communicator communicator;
1916
private ASTMHandlerMarshaller astmHandlerMarshaller;
2017

21-
public ASTMReceiveThread(
22-
Socket socket,
23-
ASTMInterpreterFactory astmInterpreterFactory,
24-
ASTMHandlerMarshaller astmHandlerMarshaller
25-
) {
18+
public ASTMReceiveThread(Communicator communicator, Socket socket, ASTMHandlerMarshaller astmHandlerMarshaller) {
19+
this.communicator = communicator;
2620
this.socket = socket;
27-
this.astmInterpreterFactory = astmInterpreterFactory;
2821
this.astmHandlerMarshaller = astmHandlerMarshaller;
2922
}
3023

31-
public ASTMReceiveThread(LIS01A2Communicator communicator, ASTMHandlerMarshaller astmHandlerMarshaller) {
24+
public ASTMReceiveThread(Communicator communicator, ASTMHandlerMarshaller astmHandlerMarshaller) {
3225
this.communicator = communicator;
26+
this.socket = null;
3327
this.astmHandlerMarshaller = astmHandlerMarshaller;
3428
}
3529

3630
@Override
3731
public void run() {
3832
log.trace("thread started to receive ASTM message");
3933
try {
40-
if (communicator == null) {
41-
communicator = new LIS01A2Communicator(astmInterpreterFactory, socket);
42-
log.debug("successfully created LIS01A2 communicator " + communicator.getID());
43-
}
4434
try {
45-
List<ASTMMessage> messages = communicator.receiveProtocol();
46-
for (ASTMMessage message : messages) {
47-
HandleStatus status = astmHandlerMarshaller.handle(message);
48-
log.debug("astm HandleStatus is: " + status);
49-
if (status != HandleStatus.SUCCESS) {
50-
log.error("message was not handled successfully");
51-
}
35+
ASTMMessage message = communicator.receiveProtocol();
36+
HandleStatus status = astmHandlerMarshaller.handle(message);
37+
log.debug("astm HandleStatus is: " + status);
38+
if (status != HandleStatus.SUCCESS) {
39+
log.error("message was not handled successfully");
5240
}
5341
} catch (IllegalStateException | FrameParsingException | ASTMCommunicationException e) {
5442
log.error("an error occurred understanding or handling what was received from the astm sender", e);
5543
}
5644
} catch (IOException e) {
5745
log.error("error occurred communicating with astm sender", e);
5846
} finally {
59-
if (communicator != null) {
60-
try {
61-
communicator.close();
62-
log.debug("successfully closed communicator " + communicator.getID() + " with astm sender");
63-
} catch (IOException e) {
64-
log.error("error occurred closing communicator " + communicator.getID() + " with astm sender", e);
65-
}
66-
}
6747
if (socket != null && !socket.isClosed()) {
6848
try {
6949
socket.close();

astm-http-lib/src/main/java/org/itech/ahb/lib/astm/servlet/ASTMServlet.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,44 @@
88
@Slf4j
99
public class ASTMServlet {
1010

11+
public enum ASTMVersion {
12+
LIS01_A,
13+
E1381_95,
14+
NON_COMPLIANT
15+
}
16+
1117
private final ASTMHandlerMarshaller astmMessageMarshaller;
1218
private final ASTMInterpreterFactory astmInterpreterFactory;
1319
private final int listenPort;
20+
private final ASTMVersion astmVersion;
1421

1522
public ASTMServlet(
1623
ASTMHandlerMarshaller astmMessageMarshaller,
1724
ASTMInterpreterFactory astmInterpreterFactory,
18-
int listenPort
25+
int listenPort,
26+
ASTMVersion astmVersion
1927
) {
2028
this.astmMessageMarshaller = astmMessageMarshaller;
2129
this.astmInterpreterFactory = astmInterpreterFactory;
2230
this.listenPort = listenPort;
31+
this.astmVersion = astmVersion;
2332
}
2433

2534
public void listen() {
2635
try (ServerSocket serverSocket = new ServerSocket(listenPort)) {
27-
log.info("Server is listening on port " + listenPort + " for ASTM protocol messages");
36+
log.info("Server is listening on port " + listenPort + " for ASTM protocol: " + astmVersion + " messages");
2837
// Communication Endpoint for the client and server.
2938
while (true) {
3039
// Waiting for socket connection
3140
Socket s = serverSocket.accept();
32-
new ASTMReceiveThread(s, astmInterpreterFactory, astmMessageMarshaller).start();
41+
new ASTMReceiveThread(
42+
new GeneralASTMCommunicator(astmInterpreterFactory, s.getInputStream(), s.getOutputStream(), astmVersion),
43+
s,
44+
astmMessageMarshaller
45+
).start();
3346
}
34-
} catch (Exception e) {}
47+
} catch (Exception e) {
48+
log.error("an exception caused the astm server to shut down", e);
49+
}
3550
}
3651
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.itech.ahb.lib.astm.servlet;
2+
3+
import java.io.IOException;
4+
import org.itech.ahb.lib.common.ASTMMessage;
5+
import org.itech.ahb.lib.common.exception.ASTMCommunicationException;
6+
import org.itech.ahb.lib.common.exception.FrameParsingException;
7+
8+
public interface Communicator {
9+
String getID();
10+
boolean sendProtocol(ASTMMessage message) throws ASTMCommunicationException, IOException;
11+
ASTMMessage receiveProtocol() throws FrameParsingException, ASTMCommunicationException, IOException;
12+
}

0 commit comments

Comments
 (0)