001    /*
002     * Networked Wire, Client Side
003     * 
004     * $Id: ClientWire.java,v 1.3 2003/11/03 19:00:06 gus Exp $
005     *
006     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
007     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
008     * CS101 homepage</a> or email <las@ai.mit.edu>.
009     *
010     * Copyright (C) 1996 Massachusetts Institute of Technology.
011     * Please do not redistribute without obtaining permission.
012     */
013    package cs101.net;
014    
015    import cs101.awt.ClientDialog;
016    import java.io.*;
017    import java.net.*;
018    
019    /**
020     * Networked Wire, Client Side.  Provides readObject, writeObject. 
021     *
022     * <P>If server's hostName and port are not provided at creation time,
023     * the user is prompted for this information using ClientDialog.
024     * 
025     * <P>Copyright 1996 Massachusetts Institute of Technology
026     *
027     * @see      cs101.net.Wire
028     * @see      cs101.net.ServerWire
029     *
030     * @author   Todd C. Parnell, tparnell@ai.mit.edu
031     * @author   Maciej Stachowiak, maciej@ai.mit.edu 
032     * @author   Lynn Andrea Stein, las@ai.mit.edu
033     * @version  $Id: ClientWire.java,v 1.3 2003/11/03 19:00:06 gus Exp $
034     */
035    
036    public class ClientWire implements Wire {
037    
038      /** 
039       * The network connection.  Contains all of the relevant connect
040       * info, if we need it 
041       */
042      private Socket sock;
043      /** Where to read from */ 
044      private ObjectInputStream in;
045      /** Where to write to */
046      private ObjectOutputStream out;
047      /** Who we're connected to */
048      private String hostname;
049      /** Which port we're connected to */
050      private int port;
051      /** Operate verbosely */
052      private boolean verbose;
053    
054      /**
055       * How to make one, if we know who we want to talk to.
056       * Specify verboseness.
057       *
058       * @param hostName  the name of the machine that the server is on
059       * @param port      the port number on which the server is listening
060       * @param verbose   specify verboseness level
061       */
062      public ClientWire( String hostName, int port, boolean verbose ) {
063        this.verbose = verbose;
064        this.connectTo( hostName, port );
065      }
066    
067      /**
068       * How to make one, if we know who we want to talk to. 
069       * Verbosness on.
070       *
071       * @param hostName  the name of the machine that the server is on
072       * @param port      the port number on which the server is listening
073       */
074      public ClientWire( String hostName, int port ) {
075        this(hostName, port, true);
076      }
077    
078      /**
079       * How to make one, if we don't know who we want to talk to.
080       * Uses ClientDialog to ask user.  Verboseness on.
081       */
082      public ClientWire() {    
083        this.verbose = true;
084        ClientDialog qd = new ClientDialog();
085        qd.ask();
086        this.connectTo( qd.getHostName(), qd.getPort() );
087      }
088    
089      /**
090       * Opens a connection to a server presumed to be listening on hostName,
091       * port.  Sets up listener thread.  Called by constructor; should not be
092       * called otherwise.
093       *
094       * @param hostName  the name of the machine that the server is on
095       * @param port      the port number on which the server is listening
096       */
097      protected void connectTo( String hostName, int port ) {
098        if (this.verbose)
099          System.out.println("Client:  trying to connect to " + hostName
100                             + " on port " + port );
101        try {
102          this.hostname = hostName;
103          this.port = port;
104          this.sock = new Socket( hostName, port );
105          this.out = new ObjectOutputStream( this.sock.getOutputStream() );
106          this.in = new ObjectInputStream( this.sock.getInputStream() );
107        } catch (IOException e) {
108          throw new RuntimeException("Client:  " +
109                                     "can't establish communication with " + 
110                                     hostName + " on port " + port );
111        }
112      }
113    
114      /**
115       * Use this to read an Object from the Wire.
116       *
117       * @return the Object read.
118       */
119      public Object readObject() {
120        try {
121          return this.in.readObject();
122        }
123        catch (Exception e) {
124          throw new RuntimeException("ClientWire:  failed to read from " + 
125                                     hostname + " on " + port); 
126        }
127      }
128    
129      /**
130       * Use this method to write an Object to the Wire.
131       *
132       * @param o The object to be written.
133       */
134      public void writeObject( Object o ) {
135        try {
136          this.out.writeUnshared( o );
137        } catch (IOException e) {
138          throw new RuntimeException("ClientWire:  failed to write to " + 
139                                     hostname + " on " + port); 
140        }
141      }
142    
143      /**
144       *  Closes the Socket and Streams.
145       */
146      public void finalize() {
147        try {
148          this.in.close();
149          this.out.close();
150          this.sock.close();
151        } catch (IOException e) {}
152      }
153        
154    }
155    
156    
157    /* Comments:
158     *
159     * History:
160     *     $Log: ClientWire.java,v $
161     *     Revision 1.3  2003/11/03 19:00:06  gus
162     *     fix typo
163     *
164     *     Revision 1.2  2003/10/28 21:41:15  gus
165     *     use writeUnshared not writeObject to avoid confusing communication problems
166     *
167     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
168     *     CS101 comes to Olin finally.
169     *
170     *     Revision 1.7  1999/02/17 20:24:22  tparnell
171     *     added verbose option
172     *
173     *     Revision 1.6  1999/01/20 22:24:55  tparnell
174     *     Modified ordering on creation of input/outputStreams
175     *
176     *     Revision 1.5  1998/10/16 19:46:02  tparnell
177     *     Fixed javadoc returns to return
178     *
179     *     Revision 1.4  1998/07/24 17:13:39  tparnell
180     *     Placate new javadoc behavior
181     *
182     *     Revision 1.3  1998/07/22 18:17:53  tparnell
183     *     move from util to net
184     *
185     *     Revision 1.2  1998/06/24 21:28:53  tparnell
186     *     code cleanup
187     *
188     *     Revision 1.1  1998/06/24 16:32:16  tparnell
189     *     changes after summer98.  added a multi-user Group server and some
190     *     other misc files
191     *
192     *
193     */
194    
195