/** * Called by subclasses when a new Connection is made. Registers the * ApplicationRouter with the given Connection and stores it. * * @param c existing connection */ public synchronized void newConnection(ActiveConnection c) { c.getResponder().setApplicationRouter(applicationRouter); c.activate(); connections.add(c); // keep track of connections notifyListeners(c); }
public void run() { try { String response = conn.getResponder().processMessage(m); if (response != null) { conn.getAckWriter().writeMessage(response); } else { log.debug("Not responding to incoming message"); } } catch (Exception e) { log.error("Error while processing message: ", e); } } }
/** Creates a new instance of Receiver, associated with the given Connection */ public Receiver(ActiveConnection c, HL7Reader in) { super("Receiver", c.getExecutorService()); this.conn = c; this.in = in; }
private String describeRemoteConnection() { return conn.getRemoteAddress().getHostAddress() + ":" + conn.getRemotePort(); }
String outbound = conn.getParser().encode(out); rawOutbound.debug(outbound); Future<String> inbound = null; try { String message; inbound = conn.waitForResponse(messID, timeoutMillis); conn.getSendWriter().writeMessage(outbound); if (inbound != null && (message = inbound.get()) != null) { Message response = conn.getParser().parse(message); log.debug("response parsed"); return response; if (inbound != null) inbound.cancel(true); conn.close(); throw e; } catch (InterruptedException e) {
/** * Helper method that checks whether the newSocket completes a two-port * connection or not. If yes, the {@link ActiveConnection} object is created and * returned. */ private ActiveConnection acceptConnection(AcceptedSocket newSocket) throws LLPException, IOException { ActiveConnection conn = null; if (newSocket != null) { String address = newSocket.socket.getInetAddress().getHostAddress(); AcceptedSocket otherSocket = waitingForSecondSocket.remove(address); if (otherSocket != null && otherSocket.origin != newSocket.origin) { log.debug("Socket {} completes a two-port connection", newSocket.socket); Socket in = getInboundSocket(newSocket, otherSocket); Socket out = getOutboundSocket(newSocket, otherSocket); conn = new ActiveConnection(getParser(), getLlp(), in, out, getExecutorService()); } else { log.debug( "Registered {} Still waiting for second socket for two-port connection", newSocket.socket); waitingForSecondSocket.put(address, newSocket); } } return conn; }
/** * Processes a single incoming message by sending it to the appropriate * internal location. If an incoming message contains an MSA-2 field, it is * assumed that this message is meant as a reply to a message that has been * sent earlier. In this case an attempt is give the message to the object * that sent the corresponding outbound message. If the message contains an * MSA-2 but there are no objects that appear to be waiting for it, it is * discarded and an exception is logged. If the message does not contain an * MSA-2 field, it is concluded that the message has arrived unsolicited. In * this case it is sent to the Responder (in a new Thread). */ protected void processMessage(String message) { String ackID = conn.getParser().getAckID(message); if (ackID == null) { log.debug("Unsolicited Message Received: {}", message); 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); } } }
public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getRemoteAddress().getHostName()); buf.append(":"); for (Iterator<Socket> iter = sockets.iterator(); iter.hasNext();) { Socket socket = iter.next(); buf.append(socket.getPort()); if (iter.hasNext()) buf.append(","); } return buf.toString(); }
@Override protected void handle() { try { String message = in.getMessage(); if (message == null) { log.debug("Failed to read a message"); } else { processMessage(message); } } catch (SocketException e) { // This probably means that the client closed the server connection normally conn.close(); log.info("SocketException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver: " + e.getMessage()); } catch (IOException e) { conn.close(); log.warn("IOException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver. ", e); } catch (Exception e) { conn.close(); log.error("Unexpected error, closing connection from " + describeRemoteConnection() + " - ", e); } }
String outbound = conn.getParser().encode(out); rawOutbound.debug(outbound); Future<String> inbound = null; try { String message; inbound = conn.waitForResponse(messID, timeoutMillis); conn.getSendWriter().writeMessage(outbound); if (inbound != null && (message = inbound.get()) != null) { Message response = conn.getParser().parse(message); log.debug("response parsed"); return response; if (inbound != null) inbound.cancel(true); conn.close(); throw e; } catch (InterruptedException e) {
private String describeRemoteConnection() { return conn.getRemoteAddress().getHostAddress() + ":" + conn.getRemotePort(); }
/** * Helper method that checks whether the newSocket completes a two-port * connection or not. If yes, the {@link ActiveConnection} object is created and * returned. */ private ActiveConnection acceptConnection(AcceptedSocket newSocket) throws LLPException, IOException { ActiveConnection conn = null; if (newSocket != null) { String address = newSocket.socket.getInetAddress().getHostAddress(); AcceptedSocket otherSocket = waitingForSecondSocket.remove(address); if (otherSocket != null && otherSocket.origin != newSocket.origin) { log.debug("Socket {} completes a two-port connection", newSocket.socket); Socket in = getInboundSocket(newSocket, otherSocket); Socket out = getOutboundSocket(newSocket, otherSocket); conn = new ActiveConnection(getParser(), getLlp(), in, out, getExecutorService()); } else { log.debug( "Registered {} Still waiting for second socket for two-port connection", newSocket.socket); waitingForSecondSocket.put(address, newSocket); } } return conn; }
/** * Processes a single incoming message by sending it to the appropriate * internal location. If an incoming message contains an MSA-2 field, it is * assumed that this message is meant as a reply to a message that has been * sent earlier. In this case an attempt is give the message to the object * that sent the corresponding outbound message. If the message contains an * MSA-2 but there are no objects that appear to be waiting for it, it is * discarded and an exception is logged. If the message does not contain an * MSA-2 field, it is concluded that the message has arrived unsolicited. In * this case it is sent to the Responder (in a new Thread). */ protected void processMessage(String message) { String ackID = conn.getParser().getAckID(message); if (ackID == null) { log.debug("Unsolicited Message Received: {}", message); 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); } } }
public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getRemoteAddress().getHostName()); buf.append(":"); for (Iterator<Socket> iter = sockets.iterator(); iter.hasNext();) { Socket socket = iter.next(); buf.append(socket.getPort()); if (iter.hasNext()) buf.append(","); } return buf.toString(); }
@Override protected void handle() { try { String message = in.getMessage(); if (message == null) { log.debug("Failed to read a message"); } else { processMessage(message); } } catch (SocketException e) { // This probably means that the client closed the server connection normally conn.close(); log.info("SocketException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver: " + e.getMessage()); } catch (IOException e) { conn.close(); log.warn("IOException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver. ", e); } catch (Exception e) { conn.close(); log.error("Unexpected error, closing connection from " + describeRemoteConnection() + " - ", e); } }
/** * Called by subclasses when a new Connection is made. Registers the * ApplicationRouter with the given Connection and stores it. * * @param c existing connection */ public synchronized void newConnection(ActiveConnection c) { c.getResponder().setApplicationRouter(applicationRouter); c.activate(); connections.add(c); // keep track of connections notifyListeners(c); }
public void run() { try { String response = conn.getResponder().processMessage(m); if (response != null) { conn.getAckWriter().writeMessage(response); } else { log.debug("Not responding to incoming message"); } } catch (Exception e) { log.error("Error while processing message: ", e); } } }
/** * Loop that waits for a connection and starts a ConnectionManager when it * gets one. */ @Override protected void handle() { if (acceptor.getServiceExitedWithException() != null) { setServiceExitedWithException(acceptor.getServiceExitedWithException()); } try { // Wait some period of time for connections AcceptedSocket newSocket = queue.poll(500, TimeUnit.MILLISECONDS); if (newSocket != null) { 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()); newConnection(c); } } catch (InterruptedException ie) { // just timed out } catch (Exception e) { log.error("Error while accepting connections: ", e); } }
/** * Polls for accepted sockets */ protected void handle() { if (inboundAcceptor.getServiceExitedWithException() != null) { setServiceExitedWithException(inboundAcceptor.getServiceExitedWithException()); } if (outboundAcceptor.getServiceExitedWithException() != null) { setServiceExitedWithException(outboundAcceptor.getServiceExitedWithException()); } try { ActiveConnection conn = acceptConnection(queue.poll(2, TimeUnit.SECONDS)); if (conn != null) { log.info("Accepted connection from " + conn.getRemoteAddress().getHostAddress()); newConnection(conn); } } catch (Exception e) { log.error("Error while accepting connections: ", e); } }
/** Creates a new instance of Receiver, associated with the given Connection */ public Receiver(ActiveConnection c, HL7Reader in) { super("Receiver", c.getExecutorService()); this.conn = c; this.in = in; }