Coverage Report - ca.uhn.hl7v2.protocol.impl.ServerSocketStreamSource
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerSocketStreamSource
100%
8/8
N/A
2.75
ServerSocketStreamSource$Acceptor
95%
19/20
75%
3/4
2.75
ServerSocketStreamSource$Acceptor$1
79%
19/24
70%
7/10
2.75
 
 1  
 /*
 2  
 The contents of this file are subject to the Mozilla Public License Version 1.1 
 3  
 (the "License"); you may not use this file except in compliance with the License. 
 4  
 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
 5  
 Software distributed under the License is distributed on an "AS IS" basis, 
 6  
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
 7  
 specific language governing rights and limitations under the License. 
 8  
 
 9  
 The Original Code is "ServerSocketStreamSource.java".  Description: 
 10  
 "A StreamSource that gets streams from ServerSockets." 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2004.  All Rights Reserved. 
 14  
 
 15  
 Contributor(s): ______________________________________. 
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the 
 18  
 GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
 19  
 applicable instead of those above.  If you wish to allow use of your version of this 
 20  
 file only under the terms of the GPL and not to allow others to use your version 
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 22  
 and replace  them with the notice and other provisions required by the GPL License.  
 23  
 If you do not delete the provisions above, a recipient may use your version of 
 24  
 this file under either the MPL or the GPL. 
 25  
 */
 26  
 
 27  
 package ca.uhn.hl7v2.protocol.impl;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.net.ServerSocket;
 31  
 import java.net.Socket;
 32  
 import java.net.SocketTimeoutException;
 33  
 
 34  
 import org.slf4j.Logger;
 35  
 import org.slf4j.LoggerFactory;
 36  
 
 37  
 import ca.uhn.hl7v2.protocol.TransportException;
 38  
 
 39  
 /**
 40  
  * A <code>StreamSource</code> that gets streams from ServerSockets.  This 
 41  
  * allows you to communicate over sockets that are established by the remote 
 42  
  * party (ie as a TCP/IP server).
 43  
  * 
 44  
  * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
 45  
  * @version $Revision: 1.4 $ updated on $Date: 2009-12-19 20:01:20 $ by $Author: jamesagnew $
 46  
  */
 47  
 public class ServerSocketStreamSource extends SocketStreamSource {
 48  
 
 49  
         /** The default SO_TIMEOUT value for sockets returned by this class */
 50  
         public static final int TIMEOUT = 500;
 51  
         
 52  
     private ServerSocket myServerSocket;
 53  
     private String myExpectedAddress;
 54  
     private Socket mySocket;
 55  
     
 56  
     /**
 57  
      * @param theServerSocket a ServerSocket at which to listen for incoming connections  
 58  
      * @param theExpectedAddress the IP address from which to accept connections (null means 
 59  
      *      accept from any address) 
 60  
      * @throws TransportException
 61  
      */
 62  50
     public ServerSocketStreamSource(ServerSocket theServerSocket, String theExpectedAddress) throws TransportException {
 63  50
         myServerSocket = theServerSocket;
 64  50
         myExpectedAddress = theExpectedAddress;
 65  50
     }
 66  
 
 67  
     /** 
 68  
      * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket()
 69  
      */
 70  
     public Socket getSocket() {
 71  420
         return mySocket;
 72  
     }
 73  
 
 74  
     /** 
 75  
      * Accepts new connections on underlying ServerSocket, replacing 
 76  
      * any existing socket with the new one, blocking until a connection 
 77  
      * is available.  See {@link DualTransportConnector} for a method of 
 78  
      * connecting two <code>TransportLayer</code>s in a way that avoids deadlock.    
 79  
      * 
 80  
      * @see ca.uhn.hl7v2.protocol.StreamSource#connect()
 81  
      */
 82  
     public void connect() throws TransportException {
 83  60
         Acceptor a = new Acceptor(myServerSocket, myExpectedAddress);                
 84  60
         mySocket = a.waitForSocket();
 85  40
     }
 86  
     
 87  
     /**
 88  
      * A thing with which waiting for inbound socket connections can 
 89  
      * be done in a separate thread.  This is needed because we may have to 
 90  
      * start waiting at two ports before pending on either.  Otherwise if 
 91  
      * we accept() in a different order than the remote system connects, 
 92  
      * we will deadlock.  
 93  
      * 
 94  
      * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
 95  
      * @version $Revision: 1.4 $ updated on $Date: 2009-12-19 20:01:20 $ by $Author: jamesagnew $
 96  
      */
 97  55
     private static class Acceptor {
 98  
         
 99  5
         private static final Logger log = LoggerFactory.getLogger(Acceptor.class);
 100  
         
 101  
         private Socket mySocket;
 102  
         
 103  
         /**
 104  
          * Starts waiting in a separate thread for connections to the given 
 105  
          * ServerSocket from the given IP address.  
 106  
          * @param theServer 
 107  
          * @param theAddress IP address from which to accept connections (null
 108  
          *      means any) 
 109  
          */
 110  60
         public Acceptor(final ServerSocket theServer, final String theAddress) {
 111  60
             final Acceptor a = this;
 112  60
             if (theAddress != null) {
 113  60
                 log.info("Server socket is about to try to accept a connection from {}", theAddress);
 114  
             } else {
 115  0
                 log.info("Server socket is about to try to accept a connection from any addess");
 116  
             }
 117  
 
 118  60
             Runnable r = new Runnable() {
 119  
                 public void run() {
 120  
                     while (true) {
 121  
 
 122  
                         Socket s;
 123  
                         try {
 124  
 
 125  60
                                 if (!theServer.isClosed()) {
 126  55
                                     s = theServer.accept();
 127  40
                                     s.setSoTimeout(TIMEOUT);
 128  40
                                     String address = s.getInetAddress().getHostAddress();
 129  40
                                     if (theAddress == null || address.equals(theAddress)) {
 130  40
                                         a.setSocket(s);
 131  40
                                         synchronized (a) {
 132  40
                                             a.notifyAll();
 133  40
                                         }
 134  
                                     } else {
 135  0
                                         log.info("Ignoring connection from {}: expecting ", address, theAddress);
 136  
                                     }
 137  
                                 }
 138  
                                 
 139  0
                         } catch (SocketTimeoutException e) {
 140  0
                             log.debug("Socket timed out without receiving a connection");
 141  5
                         } catch (IOException e) {
 142  5
                             log.error("Error accepting remote connection", e); 
 143  45
                         } // try-catch
 144  
 
 145  50
                         if (a.getSocket() != null) {
 146  40
                             log.info("Accepted connection from address: {}", a.getSocket().getInetAddress());
 147  40
                             return;
 148  
                         }
 149  
 
 150  10
                         if (theServer.isClosed()) {
 151  10
                             log.warn("Server socket closed, aborting");
 152  10
                             return;
 153  
                         }
 154  
 
 155  
                         //if there's a problem, don't fill up the log at lightning speed
 156  
                         try {
 157  0
                             Thread.sleep(1000);
 158  0
                         } catch (InterruptedException e2) {}
 159  
 
 160  
                     }
 161  
                 }
 162  
             };
 163  
             
 164  60
             Thread thd = new Thread(r);
 165  60
             thd.start();
 166  60
         }
 167  
         
 168  
         public void setSocket(Socket theSocket) {
 169  40
             mySocket = theSocket;
 170  40
         }
 171  
         
 172  
         public Socket getSocket() {
 173  4000
             return mySocket;
 174  
         }
 175  
         
 176  
         /**
 177  
          * @return as getSocket(), but doesn't return until getSocket() returns 
 178  
          *  non-null.  
 179  
          */
 180  
         public Socket waitForSocket() {
 181  3870
             while (getSocket() == null) {
 182  
                 try {
 183  3830
                     synchronized (this) {
 184  3830
                         this.wait(100);
 185  3810
                     }
 186  3810
                 } catch (InterruptedException e) {}
 187  
             }
 188  40
             return getSocket();
 189  
         }
 190  
         
 191  
     }
 192  
 
 193  
 
 194  
 }