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 SocketAddress myAddress;
21      private Socket mySocket;
22  
23      /**
24       * @param theAddress the address at which to connect sockets
25       * @throws TransportException
26       */
27      public ClientSocketStreamSource(SocketAddress theAddress) throws TransportException {
28          myAddress = theAddress;
29      }
30  
31      /** 
32       * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket()
33       */
34      public Socket getSocket() {
35          return mySocket;
36      }
37  
38      /** 
39       * Gets fresh instances of sockets.  
40       */
41      public void connect() throws TransportException {
42          mySocket = getSocket(myAddress);
43      }
44  
45      private Socket getSocket(SocketAddress theAddress) throws TransportException {
46          Socket s = new Socket();        
47          try {
48              s.connect(theAddress);
49          } catch (IOException e) {
50              throw new TransportException(e);
51          }
52          return s;        
53      }
54  
55  }