Skip to content

Commit

Permalink
Support for recieving all messages if SimpleServer is asked to delive…
Browse files Browse the repository at this point in the history
…r them all
  • Loading branch information
Tom Chiverton committed Aug 3, 2017
1 parent c5412b3 commit 452f115
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions hapi-base/src/main/java/ca/uhn/hl7v2/DefaultHapiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ public void setSocketFactory(SocketFactory socketFactory) {
public SimpleServer newServer(int port, boolean tls) {
return new SimpleServer(this, port, tls);
}

public SimpleServer newServer(int port, boolean tls, boolean acceptAll) {
return new SimpleServer(this, port, tls, acceptAll);
}

public TwoPortService newServer(int port1, int port2, boolean tls) {
return new TwoPortService(this, port1, port2, tls);
Expand Down
17 changes: 17 additions & 0 deletions hapi-base/src/main/java/ca/uhn/hl7v2/HapiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ public interface HapiContext extends Closeable {
*/
HL7Service newServer(int port, boolean tls);

/**
* Construct a new HL7 Server which will listen for incoming connections
* and will pass all messages to the responders, even if the message control id is not
* known to the server
*
* @param port The port on which to listen for new connections
* @param tls Whether or not to use SSL/TLS
* @param acceptAll Whether or not to accept all messages
* @return HL7 service running on the configured port using the default parser and executor
* service instances provided by this interface. Note that the returned service <b>will not
* be started</b>, and must manually be started using {@link HL7Service#start()} or
* {@link HL7Service#startAndWait()}
* @see <a href="http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/SendAndReceiveAMessage.html">here<> for an example of how to use this method
* @see #setSocketFactory(SocketFactory)
*/
HL7Service newServer(int port, boolean tls, boolean acceptAll);

/**
* Construct a new HL7 Server which will listen for a pair of connections (one for
* incoming messages, one for outgoing)
Expand Down
4 changes: 4 additions & 0 deletions hapi-base/src/main/java/ca/uhn/hl7v2/HapiContextSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ public HL7Service newServer(int port, boolean tls) {
return context.newServer(port, tls);
}

public HL7Service newServer(int port, boolean tls, boolean acceptAll){
return context.newServer(port, tls, acceptAll);
}

public HL7Service newServer(int inboundPort, int outboundPort, boolean tls) {
return context.newServer(inboundPort, outboundPort, tls);
}
Expand Down
13 changes: 13 additions & 0 deletions hapi-base/src/main/java/ca/uhn/hl7v2/app/ActiveConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ActiveConnection implements Connection {
private List<Receiver> receivers;
private boolean open = true;
private ExecutorService executorService;
private boolean acceptAll = false;

/**
* Creates a new instance of Connection, with inbound and outbound
Expand All @@ -92,6 +93,14 @@ public ActiveConnection(Parser parser, LowerLayerProtocol llp,
this.initiator = new ActiveInitiator(this);
}

public ActiveConnection(Parser parser, LowerLayerProtocol llp,
Socket bidirectional, ExecutorService executorService,
boolean acceptAllMsg)
throws LLPException, IOException {
this(parser, llp, bidirectional, executorService);
acceptAll = acceptAllMsg;
}

/**
* Creates a new instance of Connection, with inbound communication on one
* port and outbound on another.
Expand Down Expand Up @@ -263,4 +272,8 @@ public boolean isOpen() {
return open;
}

public boolean acceptAllMessages(){
return acceptAll;
}

}
4 changes: 3 additions & 1 deletion hapi-base/src/main/java/ca/uhn/hl7v2/app/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ protected void processMessage(String message) {
log.debug("Unsolicited Message Received: {}", message);
getExecutorService().submit(new Grunt(conn, message));
} else {
if (!conn.isRecipientWaiting(ackID, message)) {
if ( conn.acceptAllMessages() ){
getExecutorService().submit(new Grunt(conn, message));
}else if (!conn.isRecipientWaiting(ackID, message)) {
log.info("Unexpected Message Received. This message appears to be an acknowledgement (MSA-2 has a value) so it will be ignored: {}", message);
} else {
log.debug("Response Message Received: {}", message);
Expand Down
20 changes: 18 additions & 2 deletions hapi-base/src/main/java/ca/uhn/hl7v2/app/SimpleServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class SimpleServer extends HL7Service {
private final BlockingQueue<AcceptedSocket> queue;
private AcceptorThread acceptor;
private HapiContext hapiContext;
private boolean acceptAllMsg = false;

/**
* Creates a new instance of SimpleServer that listens on the given port,
Expand All @@ -100,7 +101,7 @@ public SimpleServer(int port) {
*/
public SimpleServer(int port, boolean tls) {
this(port, new MinLowerLayerProtocol(), new PipeParser(), tls);
}
}

/**
* Creates a new instance of SimpleServer that listens on the given port.
Expand Down Expand Up @@ -146,6 +147,21 @@ public SimpleServer(HapiContext hapiContext, int port, boolean tls) {
this.queue = new LinkedBlockingQueue<AcceptedSocket>(100);
}

/**
* Creates a new instance of SimpleServer that listens on a given server socket
* and will pass all messages to the responders, even if the message control id
* is not known to the server.
* SimpleServer will bind the socket when it is started, so the server socket
* must not already be bound.
*
* @since 2.4
* @throws IllegalStateException If serverSocket is already bound
*/
public SimpleServer(HapiContext hapiContext, int port, boolean tls, boolean acceptAll) {
this(hapiContext, port, tls);
acceptAllMsg = acceptAll;
}

/**
* Prepare server by initializing the server socket
*
Expand Down Expand Up @@ -182,7 +198,7 @@ protected void handle() {
log.info("Accepted connection from {}:{} on local port {}",
new Object[] { newSocket.socket.getInetAddress().getHostAddress(), newSocket.socket.getPort(), port });
ActiveConnection c = new ActiveConnection(getParser(), getLlp(), newSocket.socket,
getExecutorService());
getExecutorService(), acceptAllMsg);
newConnection(c);
}
} catch (InterruptedException ie) {
Expand Down

0 comments on commit 452f115

Please sign in to comment.