View Javadoc
1   /*
2    * Created on 19-Apr-2004
3    */
4   package ca.uhn.hl7v2.protocol.impl;
5   
6   import java.io.IOException;
7   import java.net.Socket;
8   import java.net.SocketAddress;
9   
10  import ca.uhn.hl7v2.protocol.TransportException;
11  
12  /**
13   * A client-side <code>SocketStreamSource</code>.  
14   * 
15   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
16   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
17   */
18  public class ClientSocketStreamSource extends SocketStreamSource {
19  
20      private final SocketAddress myAddress;
21      private Socket mySocket;
22  
23      /**
24       * @param theAddress the address at which to connect sockets
25       */
26      public ClientSocketStreamSource(SocketAddress theAddress) {
27          myAddress = theAddress;
28      }
29  
30      /** 
31       * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket()
32       */
33      public Socket getSocket() {
34          return mySocket;
35      }
36  
37      /** 
38       * Gets fresh instances of sockets.  
39       */
40      public void connect() throws TransportException {
41          mySocket = getSocket(myAddress);
42      }
43  
44      private Socket getSocket(SocketAddress theAddress) throws TransportException {
45          Socket s = new Socket();        
46          try {
47              s.connect(theAddress);
48          } catch (IOException e) {
49              throw new TransportException(e);
50          }
51          return s;        
52      }
53  
54  }