001/*
002 * Created on 19-Apr-2004
003 */
004package ca.uhn.hl7v2.protocol.impl;
005
006import java.io.IOException;
007import java.net.Socket;
008import java.net.SocketAddress;
009
010import ca.uhn.hl7v2.protocol.TransportException;
011
012/**
013 * A client-side <code>SocketStreamSource</code>.  
014 * 
015 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
016 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
017 */
018public class ClientSocketStreamSource extends SocketStreamSource {
019
020    private SocketAddress myAddress;
021    private Socket mySocket;
022
023    /**
024     * @param theAddress the address at which to connect sockets
025     * @throws TransportException
026     */
027    public ClientSocketStreamSource(SocketAddress theAddress) throws TransportException {
028        myAddress = theAddress;
029    }
030
031    /** 
032     * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket()
033     */
034    public Socket getSocket() {
035        return mySocket;
036    }
037
038    /** 
039     * Gets fresh instances of sockets.  
040     */
041    public void connect() throws TransportException {
042        mySocket = getSocket(myAddress);
043    }
044
045    private Socket getSocket(SocketAddress theAddress) throws TransportException {
046        Socket s = new Socket();        
047        try {
048            s.connect(theAddress);
049        } catch (IOException e) {
050            throw new TransportException(e);
051        }
052        return s;        
053    }
054
055}