001/**
002 * The contents of this file are subject to the Mozilla Public License Version 1.1
003 * (the "License"); you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 * Software distributed under the License is distributed on an "AS IS" basis,
006 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 * specific language governing rights and limitations under the License.
008 *
009 * The Original Code is "Receiver.java".  Description:
010 * "Listens for incoming messages on a certain input stream, and
011 * sends them to the appropriate location."
012 *
013 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
014 * 2002.  All Rights Reserved.
015 *
016 * Contributor(s): _____________.
017 *
018 * Alternatively, the contents of this file may be used under the terms of the
019 * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
020 * applicable instead of those above.  If you wish to allow use of your version of this
021 * file only under the terms of the GPL and not to allow others to use your version
022 * of this file under the MPL, indicate your decision by deleting  the provisions above
023 * and replace  them with the notice and other provisions required by the GPL License.
024 * If you do not delete the provisions above, a recipient may use your version of
025 * this file under either the MPL or the GPL.
026 */
027
028package ca.uhn.hl7v2.app;
029
030import java.io.IOException;
031import java.net.SocketException;
032
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import ca.uhn.hl7v2.concurrent.Service;
037import ca.uhn.hl7v2.llp.HL7Reader;
038
039/**
040 * Listens for incoming messages on a certain input stream, and sends them to
041 * the appropriate location.
042 * 
043 * @author Bryan Tripp
044 */
045public class Receiver extends Service {
046
047        private static final Logger log = LoggerFactory.getLogger(Receiver.class);
048
049        private ActiveConnection conn;
050        private HL7Reader in;
051
052        /** Creates a new instance of Receiver, associated with the given Connection */
053        public Receiver(ActiveConnection c, HL7Reader in) {
054                super("Receiver", c.getExecutorService());
055                this.conn = c;
056                this.in = in;
057        }
058
059
060        @Override
061        protected void handle() {
062                try {
063                        String message = in.getMessage();
064                        if (message == null) {
065                                log.debug("Failed to read a message");
066                        } else {
067                                processMessage(message);
068                        }
069                } catch (SocketException e)  {
070                        // This probably means that the client closed the server connection normally
071                        conn.close();
072                        log.info("SocketException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver: " + e.getMessage());
073                } catch (IOException e) {
074                        conn.close();
075                        log.warn("IOException: closing Connection from " + describeRemoteConnection() + ", will no longer read messages with this Receiver. ", e);
076                } catch (Exception e) {
077                        conn.close();
078                        log.error("Unexpected error, closing connection from " + describeRemoteConnection() + " - ", e);
079                }
080
081        }
082
083
084        private String describeRemoteConnection() {
085                return conn.getRemoteAddress().getHostAddress() + ":" + conn.getRemotePort();
086        }
087
088
089        /**
090         * Processes a single incoming message by sending it to the appropriate
091         * internal location. If an incoming message contains an MSA-2 field, it is
092         * assumed that this message is meant as a reply to a message that has been
093         * sent earlier. In this case an attempt is give the message to the object
094         * that sent the corresponding outbound message. If the message contains an
095         * MSA-2 but there are no objects that appear to be waiting for it, it is
096         * discarded and an exception is logged. If the message does not contain an
097         * MSA-2 field, it is concluded that the message has arrived unsolicited. In
098         * this case it is sent to the Responder (in a new Thread).
099         */
100        protected void processMessage(String message) {
101                String ackID = conn.getParser().getAckID(message);
102                if (ackID == null) {
103                        log.debug("Unsolicited Message Received: {}", message);
104                        getExecutorService().submit(new Grunt(conn, message));
105                } else {
106                        if (!conn.isRecipientWaiting(ackID, message)) {
107                                log.info("Unexpected Message Received. This message appears to be an acknowledgement (MSA-2 has a value) so it will be ignored: {}", message);
108                        } else {
109                                log.debug("Response Message Received: {}", message);
110                        }
111                }
112        }
113
114        /** Independent thread for processing a single message */
115        private static class Grunt implements Runnable {
116
117                private ActiveConnection conn;
118                private String m;
119
120                public Grunt(ActiveConnection conn, String message) {
121                        this.conn = conn;
122                        this.m = message;
123                }
124
125                public void run() {
126                        try {
127                                String response = conn.getResponder().processMessage(m);
128                                if (response != null) {
129                                        conn.getAckWriter().writeMessage(response);
130                                } else {
131                                        log.debug("Not responding to incoming message");
132                                }
133                        } catch (Exception e) {
134                                log.error("Error while processing message: ", e);
135                        }
136                }
137        }
138
139}