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 final SocketAddress myAddress;
021    private Socket mySocket;
022
023    /**
024     * @param theAddress the address at which to connect sockets
025     */
026    public ClientSocketStreamSource(SocketAddress theAddress) {
027        myAddress = theAddress;
028    }
029
030    /** 
031     * @see ca.uhn.hl7v2.protocol.impl.SocketStreamSource#getSocket()
032     */
033    public Socket getSocket() {
034        return mySocket;
035    }
036
037    /** 
038     * Gets fresh instances of sockets.  
039     */
040    public void connect() throws TransportException {
041        mySocket = getSocket(myAddress);
042    }
043
044    private Socket getSocket(SocketAddress theAddress) throws TransportException {
045        Socket s = new Socket();        
046        try {
047            s.connect(theAddress);
048        } catch (IOException e) {
049            throw new TransportException(e);
050        }
051        return s;        
052    }
053
054}