//
//###################################################################
//                   Class Front
//###################################################################
//  The "front" component of a sample distributed application.
//
//  In this example just simple data types are exchanged.
//
//  Creator               : Chris Anderson
//  (C) UCLA 1997 
//
//  Updated 07/15/98 CRA  
//  
//###################################################################
//
import java.io.*;
import cam.netapp.NetworkConnection;

public class Front implements Runnable
{ 
//
//################################################################
//  Data Members
//################################################################
//
//  Streams for communication
//
    InputStream  FrontInputStream;
    OutputStream FrontOutputStream;
//
//  NetworkConnection : a class to facilitate stream 
//  connection and control of a remote class.
//
    NetworkConnection netConnect;
//  
//  remote class name, remote address and remote port 
//
    public String  remoteClassName;
    public String      inetAddress;
    public int          portNumber; 
//
//################################################################
//  METHODS
//################################################################
//
public Front()
{
    remoteClassName = "Back";
    inetAddress     = "127.0.0.1"; // default = loopback
    portNumber      = 6789;        // default.      
}
public void setRemoteClassName(String rName)
{
    remoteClassName = new String(rName);
}

public void setRemoteAddress(String iAddress)
{
    inetAddress = new String(iAddress);
}

public void setRemotePort(int rPort)
{
    portNumber = rPort;
}

public void run()
{
    System.out.println("Front : Running");
//
//  Create a connection with a "remote" class
//
    netConnect = new NetworkConnection();
    netConnect.setVerboseFlag(true);       // set to true for
                                           // diagnostics 
//
//  Set up a network connection to remote application using the 
//  values of class data members inetAddress, portNumber 
//  and remoteClassName
//
    try
    {netConnect.connectRemoteApplication(inetAddress,portNumber,remoteClassName);}
    catch(Exception e){System.out.println(e);}
//
//  Capture streams 
//
    FrontInputStream  = netConnect.getInputStream();
    FrontOutputStream = netConnect.getOutputStream();
//
//  Start remote application
//
    netConnect.startRemoteApplication();
//
//  Exchange data with the remote class
//  Here I "wrap" the output and intput streams with 
//  a PrintStream and DataInputStream respectively so that I can have
//  higher level stream functions (like println etc).
//
    PrintStream P     = new PrintStream(FrontOutputStream); 
    DataInputStream D = new DataInputStream(FrontInputStream);
    
    System.out.println("Front : Sending Data");
    P.println("Some Text Data");

    
    double d1 = 0.0;
    double d2 = 0.0;
    try
    {
      d1 = D.readDouble();
      d2 = D.readDouble();
    }catch(IOException e){};
    
    System.out.println("Front : Values Received ");
    System.out.println("Front : d1 = " + d1);
    System.out.println("Front : d2 = " + d2);
    

    netConnect.stopRemoteApplication();
}
//
//################################################################
//  MAIN
//################################################################
//
public static void main(String[] args)
{
   
   Front F      = new Front();
   
   F.setRemoteClassName("Back");   // name of remote class 
   F.setRemoteAddress("127.0.0.1"); // local loopback
   F.setRemotePort(6789);           // port that server manager is listening on
//
// Create a thread to run the Front application and start it
//
   Thread Frun = new Thread(F);
   Frun.start();
//
// Pause for console input at program termination
//
   BufferedReader D = new BufferedReader(new InputStreamReader(System.in));
   try{D.readLine();}catch(Exception e){}
}// main()

}