001    /*
002     * Networked Wire, Server Side
003     * 
004     * $Id: ServerWire.java,v 1.2 2003/10/28 21:41:15 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.ServerDialog;
016    import java.io.*;
017    import java.net.*;
018    
019    /**
020     * Networked Wire, Server Side.  Provides readObject, writeObject.
021     * <p>
022     *
023     * If server's port is not provided at creation time, the user
024     * is prompted for this information using ServerDialog.
025     * <p>
026     * Copyright 1996 Massachusetts Institute of Technology
027     *
028     * @see      cs101.net.Wire
029     * @see      cs101.net.ClientWire
030     *
031     * @author   Todd C. Parnell, tparnell@ai.mit.edu
032     * @author   Maciej Stachowiak, maciej@ai.mit.edu 
033     * @author   Lynn Andrea Stein, las@ai.mit.edu
034     * @version  $Id: ServerWire.java,v 1.2 2003/10/28 21:41:15 gus Exp $
035     *
036     */
037    
038    public class ServerWire implements Wire {
039    
040      /** 
041       * The network connection.  Contains all of the relevant connect
042       * info, if we need it 
043       */
044      private Socket sock;
045      /** Where to read from */ 
046      private ObjectInputStream in;
047      /** Where to write to */
048      private ObjectOutputStream out;
049      /** Spew to System.out? */
050      private boolean verbose;
051      
052      /**
053       * How to make one, if we know the port.  Control verbosity.
054       *
055       * @param port    the port number to listen on
056       * @param verbose toggle verbosity
057       */
058      public ServerWire( int port, boolean verbose ) {
059        this.verbose = verbose;
060        this.connectTo( port );
061      }
062      
063      
064      /**
065       * How to make one, if we know the port.  Verbose mode.
066       *
067       * @param port      the port number to listen on
068       */
069      public ServerWire( int port ) {
070        this(port, true);
071      }
072    
073      /**
074       * How to make one, if we don't know who we want to talk to.
075       * Uses ServerDialog to ask user.  Verbose mode.
076       */
077      public ServerWire() {    
078        this.verbose = true;
079        ServerDialog qd = new ServerDialog();
080        qd.ask();
081        this.connectTo( qd.getPort() );
082      }
083      
084      /**
085       * Opens a connection to a server presumed to be listening on hostName,
086       * port.  Sets up listener thread.  Called by constructor; should not be
087       * called otherwise.
088       *
089       * @param port      the port number on which the server is listening
090       */
091      protected void connectTo( int port ) {
092        if (this.verbose) System.out.println("Server:  listening on port " + port); 
093        try {
094          ServerSocket srv = new ServerSocket( port );
095          this.sock = srv.accept();
096          this.in = new ObjectInputStream( this.sock.getInputStream() );
097          this.out = new ObjectOutputStream( this.sock.getOutputStream() );
098        } catch (IOException e) {
099          throw new RuntimeException("Server:  " +
100                                     "can't establish communication on port " + port );
101        }
102      }
103    
104      /**
105       * Use this to read an Object from the Wire.
106       *
107       * @return the Object read.
108       */
109      public Object readObject() {
110        try {
111          return this.in.readObject();
112        }
113        catch (Exception e) {
114          throw new RuntimeException("ServerWire:  failed to read."); 
115        }
116      }
117      
118      /**
119       * Use this method to write an Object to the Wire.
120       *
121       * @param o The object to be written.
122       */
123      public void writeObject( Object o ) {
124        try {
125          this.out.writeUnshared( o );
126        } catch (IOException e) {
127          throw new RuntimeException("ServerWire:  failed to write."); 
128        }
129      }
130      
131      /**
132       *  Closes the Socket.
133       */
134      
135      public void finalize() {
136        try {
137          this.in.close();
138          this.out.close();
139          this.sock.close();
140        } catch (IOException e) {}
141      }
142    }
143    
144    
145    /* Comments:
146     *
147     * History:
148     *     $Log: ServerWire.java,v $
149     *     Revision 1.2  2003/10/28 21:41:15  gus
150     *     use writeUnshared not writeObject to avoid confusing communication problems
151     *
152     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
153     *     CS101 comes to Olin finally.
154     *
155     *     Revision 1.8  1999/02/17 17:36:47  tparnell
156     *     added verbosity option
157     *
158     *     Revision 1.7  1999/01/20 22:28:05  tparnell
159     *     left in a println.  oops
160     *
161     *     Revision 1.6  1999/01/20 22:24:55  tparnell
162     *     Modified ordering on creation of input/outputStreams
163     *
164     *     Revision 1.5  1998/10/16 19:46:04  tparnell
165     *     Fixed javadoc returns to return
166     *
167     *     Revision 1.4  1998/07/24 17:13:46  tparnell
168     *     Placate new javadoc behavior
169     *
170     *     Revision 1.3  1998/07/23 14:58:15  tparnell
171     *     javadoc fix
172     *
173     *     Revision 1.2  1998/07/22 18:18:08  tparnell
174     *     move from util to net
175     *
176     *     Revision 1.1  1998/06/24 16:32:20  tparnell
177     *     changes after summer98.  added a multi-user Group server and some
178     *     other misc files
179     *
180     *
181     */
182    
183