// TalkJava
// Burton Rosenberg
// Wed Jul 16 17:30:58 EDT 1997

import java.io.* ;
import java.net.* ;
import java.awt.* ;

public class TalkJava extends Frame {

   static final int PORT_NUMBER = 2234 ;
   static final int WIDTH = 500 ;
   static final int HEIGHT = 500 ;

   public static void main( String [] args ) {
      
       TalkJava tj = new TalkJava("Talk Java") ;
       tj.resize( WIDTH, HEIGHT ) ;
       tj.show() ;
       tj.start(args) ;

   }

   TextArea tau ; // Upper Text Area
   TextArea tal ; // Lower Text Area
   TextField tf ; // Text Field, for text input
   Button ptt ;    //  Push to Talk
   PrintStream ps = null ;

   TalkJava (String title) {
      super(title) ;

      setLayout(new BorderLayout()) ;
      Panel p ;

      // layout the output text fields
      tau = new TextArea() ;
      tau.setEditable(false) ;
      tal = new TextArea() ;
      tal.setEditable(false) ;

      p = new Panel() ;
      p.setLayout(new GridLayout(0,1)) ;
      p.add(tau) ;
      p.add(tal) ;
      add("Center", p ) ;

      // layout the button input field and button
      p = new Panel() ;
      p.setLayout(new BorderLayout()) ;
      p.add("Center", tf = new TextField()) ;
      p.add("East", ptt = new Button("Talk")) ;
      add("South",p) ;

      validate() ;
   }

   void start(String [] args) {

      Socket s = null ;
   
      try {

          if ( args.length>0 ) {
              // i'll be a client
              s = new Socket(args[0], PORT_NUMBER) ;
              (new  SocketListener(s,tal)).start() ;
           } else {
              // i'll be a server
              System.out.println(
                 "Server listening on port " + PORT_NUMBER) ;
              ServerSocket ss = new ServerSocket(PORT_NUMBER) ;
              s = ss.accept() ;
              (new  SocketListener(s,tal)).start() ;
          }
          ps = new PrintStream(s.getOutputStream()) ; 

      }
      catch ( UnknownHostException uhe ) {
	   System.out.println("Unknown host " + args[0]) ;
      }
      catch ( IOException ioe ) {
	  System.out.println("Exception: " + ioe.getMessage()) ;
      }
 
   }


   public boolean action(Event e, Object o) {
        if ( e.target==ptt ) {
           String s = tf.getText() ;
           tf.setText("") ;
           ps.println(s) ;
           tau.appendText(s+"\n") ;
           return true ;
        }
        return false ;
    }

    public boolean handleEvent(Event e) {
       if ( e.id == Event.WINDOW_DESTROY ) {
         // this handles Quit and Exit on Menu Bar
         System.exit(0) ;
       }
       return super.handleEvent(e) ;
    }
}

class SocketListener extends Thread {

    Socket socket ;
    InputStream is ;
    DataInputStream dis ;
    TextArea ta ;

    SocketListener ( Socket s, TextArea ta )
    throws IOException {
         this.ta = ta ;
         socket = s ;
         is = s.getInputStream() ;
         dis = new DataInputStream(is) ;
    }

    public void run() {

        try {
	    String lineOfText ;
	    while ((lineOfText = dis.readLine()) != null) {
                ta.appendText(lineOfText+"\n") ;		
	    }
        }
        catch ( IOException ioe ) {
            System.out.println("Exception: " + ioe.getMessage()) ;
        }
    }
}

  
