001/* 002The 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. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "ServerSocketStreamSource.java". Description: 010"A StreamSource that gets streams from ServerSockets." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132004. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the �GPL�), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025*/ 026 027package ca.uhn.hl7v2.protocol.impl; 028 029import java.io.IOException; 030import java.net.ServerSocket; 031import java.net.Socket; 032import java.net.SocketTimeoutException; 033 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037import ca.uhn.hl7v2.protocol.TransportException; 038 039/** 040 * A <code>StreamSource</code> that gets streams from ServerSockets. This 041 * allows you to communicate over sockets that are established by the remote 042 * party (ie as a TCP/IP server). 043 * 044 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 045 * @version $Revision: 1.4 $ updated on $Date: 2009-12-19 20:01:20 $ by $Author: jamesagnew $ 046 */ 047public class ServerSocketStreamSource extends SocketStreamSource { 048 049 /** The default SO_TIMEOUT value for sockets returned by this class */ 050 public static final int TIMEOUT = 500; 051 052 private ServerSocket myServerSocket; 053 private String myExpectedAddress; 054 private Socket mySocket; 055 056 /** 057 * @param theServerSocket a ServerSocket at which to listen for incoming connections 058 * @param theExpectedAddress the IP address from which to accept connections (null means 059 * accept from any address) 060 * @throws TransportException 061 */ 062 public ServerSocketStreamSource(ServerSocket theServerSocket, String theExpectedAddress) throws TransportException { 063 myServerSocket = theServerSocket; 064 myExpectedAddress = theExpectedAddress; 065 } 066 067 /** 068 * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket() 069 */ 070 public Socket getSocket() { 071 return mySocket; 072 } 073 074 /** 075 * Accepts new connections on underlying ServerSocket, replacing 076 * any existing socket with the new one, blocking until a connection 077 * is available. See {@link DualTransportConnector} for a method of 078 * connecting two <code>TransportLayer</code>s in a way that avoids deadlock. 079 * 080 * @see ca.uhn.hl7v2.protocol.StreamSource#connect() 081 */ 082 public void connect() throws TransportException { 083 Acceptor a = new Acceptor(myServerSocket, myExpectedAddress); 084 mySocket = a.waitForSocket(); 085 } 086 087 /** 088 * A thing with which waiting for inbound socket connections can 089 * be done in a separate thread. This is needed because we may have to 090 * start waiting at two ports before pending on either. Otherwise if 091 * we accept() in a different order than the remote system connects, 092 * we will deadlock. 093 * 094 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a> 095 * @version $Revision: 1.4 $ updated on $Date: 2009-12-19 20:01:20 $ by $Author: jamesagnew $ 096 */ 097 private static class Acceptor { 098 099 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 public Acceptor(final ServerSocket theServer, final String theAddress) { 111 final Acceptor a = this; 112 if (theAddress != null) { 113 log.info("Server socket is about to try to accept a connection from {}", theAddress); 114 } else { 115 log.info("Server socket is about to try to accept a connection from any addess"); 116 } 117 118 Runnable r = new Runnable() { 119 public void run() { 120 while (true) { 121 122 Socket s; 123 try { 124 125 if (!theServer.isClosed()) { 126 s = theServer.accept(); 127 s.setSoTimeout(TIMEOUT); 128 String address = s.getInetAddress().getHostAddress(); 129 if (theAddress == null || address.equals(theAddress)) { 130 a.setSocket(s); 131 synchronized (a) { 132 a.notifyAll(); 133 } 134 } else { 135 log.info("Ignoring connection from {}: expecting ", address, theAddress); 136 } 137 } 138 139 } catch (SocketTimeoutException e) { 140 log.debug("Socket timed out without receiving a connection"); 141 } catch (IOException e) { 142 log.error("Error accepting remote connection", e); 143 } // try-catch 144 145 if (a.getSocket() != null) { 146 log.info("Accepted connection from address: {}", a.getSocket().getInetAddress()); 147 return; 148 } 149 150 if (theServer.isClosed()) { 151 log.warn("Server socket closed, aborting"); 152 return; 153 } 154 155 //if there's a problem, don't fill up the log at lightning speed 156 try { 157 Thread.sleep(1000); 158 } catch (InterruptedException e2) {} 159 160 } 161 } 162 }; 163 164 Thread thd = new Thread(r); 165 thd.start(); 166 } 167 168 public void setSocket(Socket theSocket) { 169 mySocket = theSocket; 170 } 171 172 public Socket getSocket() { 173 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 while (getSocket() == null) { 182 try { 183 synchronized (this) { 184 this.wait(100); 185 } 186 } catch (InterruptedException e) {} 187 } 188 return getSocket(); 189 } 190 191 } 192 193 194}