next next up down toc Memos schedule allInOne PDF PDF mail
all, section 18.41.

18.41.  Startup Multiple Server on different Machines

Client: Src/16_MS/Client.java.minusSTART_STOP


import java.rmi.*;
import java.math.*;

public class Client {

    public static void doIt(String catServer, String mouseServer, int port) {

        MyServer aCatServer;
        MyServer aMouseServer;
        Point    aPoint = new Point(4, 2 );

        System.out.println("In Client: cat   is on: " + catServer );
        System.out.println("In Client: mouse is on: " + mouseServer );
        System.out.println("In Client: port     is: " + port );
        try {
                aCatServer = (MyServer)Naming.lookup("rmi://" +
                        catServer + ":" + port + "/CatServer");

                aMouseServer = (MyServer)Naming.lookup("rmi://" +
                        mouseServer + ":" + port + "/MouseServer");


// -------------- Cat --------------------
        System.out.println("In Client: aCatServer.movePoint(aPoint): " + 
             (aPoint = aCatServer.movePoint(aPoint)).toString() );
        System.out.println("In Client: aCatServer.movePoint(aPoint): " + 
                        aCatServer.movePoint(aPoint).toString() );
        System.out.println("In Client: aCatServer.movePoint(aPoint): " + 
                        aCatServer.movePoint(aPoint).toString() );

// -------------- Mouse --------------------

        System.out.println("In Client: aMouseServer.movePoint(aPoint): " + 
             (aPoint = aMouseServer.movePoint(aPoint)).toString() );
        System.out.println("In Client: aMouseServer.movePoint(aPoint): " + 
                        aMouseServer.movePoint(aPoint).toString() );
        System.out.println("In Client: aMouseServer.movePoint(aPoint): " + 
                        aMouseServer.movePoint(aPoint).toString() );



        } catch (Exception e) {
                System.out.println("Something went wrong: " +
                e.getMessage());
                e.printStackTrace();
        }

   }

    public static void main(String args[] ) {
    int    port   = 1099;
    String catServer   = "yps";
    String mouseServer = "yps";

    if ( args.length  >= 1 )
        catServer = args[0];
    if ( args.length  >= 2 )
        mouseServer = args[1];
    if ( args.length  == 3 )
        try {
                port = Integer.parseInt(args[2]);
        }
        catch ( NumberFormatException e )       {
                System.out.println("Hm , port = " +
                        args[2] + " is not valid.");
                System.exit(1);
        }
    
    if ( args.length  > 3 )     {
        System.out.println("Usage: " + 
                        "java Client [CatServer [MouseServer [port]]]");
        System.exit(1);
    }

    doIt(catServer, mouseServer, port);
  }
}

Source Code: Src//16_MS/Client.java

Interface:

Src/16_MS/MyServer.java.minusSTART_STOP


public interface MyServer extends java.rmi.Remote {
        Point movePoint(Point aPoint) throws java.rmi.RemoteException;
        int getX()                    throws java.rmi.RemoteException;
        int getY()                    throws java.rmi.RemoteException;
}

Source Code: Src//16_MS/MyServer.java

Cat Server

Src/16_MS/CatServer.java.minusSTART_STOP


import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class CatServer extends UnicastRemoteObject implements MyServer
{
        final private int DELTA = 10;
        private int x;
        private int y;
        private Point aPoint;

        public CatServer() throws RemoteException {
                ;
        }


        public Point movePoint(Point aPoint) throws RemoteException {
                System.out.println("\tIN CatServer: movePoint(): "
                        + aPoint.toString() );
                return aPoint.move(DELTA, DELTA);
        }
        public int getX() throws RemoteException {
                System.out.println("\tCIN atServer: getX(): " + x );
                return  x;
        }

        public int getY() throws RemoteException {
                System.out.println("\tCIN atServer: getY(): " + y );
                return  x;
        }

public static void main(String args[])
  {
          int port = 1099;

          // System.setSecurityManager(new RMISecurityManager());

          if ( args.length  == 1 )
                  try {
                          port = Integer.parseInt(args[0]);
                   }
                   catch ( NumberFormatException e )    {
                          System.out.println("Hm , port = " +
                          args[0] + " is not valid.");
                          System.exit(1);
                    }
           
          try {
                  CatServer obj = new CatServer();
                  System.out.println("\tIN CatServer: " +
                                "rmi://:" + port + "/CatServer");
                  Naming.rebind("rmi://:" + port + "/CatServer", obj);
                  System.out.println("\tIN CatServer bound in registry");
          } catch (RemoteException  e) {
                  System.out.println("CatServer RemoteException ");
                  e.printStackTrace();
          } catch (Exception e) {
                  System.out.println("CatServer err: "
                  + e.getMessage());
                  e.printStackTrace();
          }
  }
}

Source Code: Src//16_MS/CatServer.java

Point Class:

Src/16_MS/Point.java.minusSTART_STOP


/**
 * This class implements a point in a two dimensional
 * area.
 * All methods print the method name, when they are called.
 * state information includes:
 *
 * @version   $Id$
 *
 * RIT's home page: <a href="http://www.cs.rit.edu/~hpb">RIT</a>
 *
 * Revisions:
 *      $Log$
 */
import java.io.*;


public class Point implements Serializable {

  private int x;                // x coordinate of the point
  private int y;                // y cooridnate of the point

/**
 * Constructor.
 * initialize x and y values of a point
 *
 * @param       x       x coordinate
 * @param       y       y coordinate
 *
 * @return      a Point object
 */
  public Point(int _x, int _y){
        this.x = _x;
        this.y = _y;
  }

  private void writeObject(ObjectOutputStream s) throws IOException {
           s.defaultWriteObject();
  }

  private void readObject(ObjectInputStream s) throws IOException  {
        try {
                s.defaultReadObject();
        }
        catch ( ClassNotFoundException e)       {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
  }
/**
 * initialzes x and y of a point.
 *
 * @param       x       int x coordinate
 * @param       y       int y coordinate
 *
 * @return      a Point object
 */
  public Point initPoint(int _x, int _y){

        this.x = _x;
        this.y = _y;

        return this;
  }

/**
 * moves a point
 *
 * @param       _x      int delta x value
 * @param       _y      int delta y value
 *
 * @return      a Point object
 */
  public Point move(int _x, int _y){

        this.x += _x;
        this.y += _y;
        return this;
  }

/**
 * Returns the x coordinate of a point
 *
 * @return x value 
 */
  public int getX(){
        return this.x;
  }

/**
 * Returns the y coordinate of a point
 *
 * @return y value 
 */
  public int getY(){
        return this.y;
  }

/**
 * Returns a String reperesentation of the point
 *
 * @return String reprasentation of the point
 */
  public String toString(){
        return "Point at (" + x + "/" + y + ")";
  }
}

Source Code: Src/16_MS/Point.java

Makefile:

 1      
 2      
 3      all: Point.class                                        \
 4           CatServer_Skel.class CatServer_Stub.class  \
 5           MouseServer_Skel.class MouseServer_Stub.class      \
 6           MyServer.class Client.class                        
 7      
 8              fireItUp
 9      
10      
11      CatServer_Skel.class CatServer_Stub.class: CatServer.java
12              rmic CatServer
13      MouseServer_Skel.class MouseServer_Stub.class: MouseServer.java
14              rmic MouseServer
15      
16      MyServer.class: MyServer.java
17              javac MyServer.java
18      
19      Client.class:   Client.java
20              javac Client.java
21      
22      CatServer.class:        CatServer.java
23              javac CatServer.java
24      
25      MouseServer.class:      MouseServer.java
26              javac MouseServer.java
27              
28      Point.class:    Point.java
29              javac Point.java
30              
31      clean:
32              rm -f *class

Source Code: Src/16_MS/makefile

Result:

        IN CatServer: //yps:2001/CatServer
        IN MouseServer: /yps:2001/MouseServer
        IN CatServer bound in registry
In Client: cat   is on: yps
In Client: mouse is on: yps
In Client: port     is: 2001
        IN MouseServer bound in registry
        IN CatServer: movePoint(): Point at (4/2)
In Client: aCatServer.movePoint(aPoint): Point at (14/12)
        IN CatServer: movePoint(): Point at (14/12)
In Client: aCatServer.movePoint(aPoint): Point at (24/22)
        IN CatServer: movePoint(): Point at (14/12)
In Client: aCatServer.movePoint(aPoint): Point at (24/22)

Start of a fireItUp Script:

 1      #!/bin/sh
 2      
 3      KILL_IT="killIt; killIt java"
 4      ME="`who am i | sed 's/ .*//'`"
 5      HOSTNAME="`hostname`"
 6      USEDHOSTS="yps yps yps" # <-- modify here ...
 7      WD=`pwd`
 8      
 9      
10      remote_cmd()            # bg host cmd
11      {
12              echo "$HOSTNAME $ME" > $HOME/.rhosts
13              if [ $1 = "bg" ]
14              then
15                      rsh $2 "rm -f $HOME/.rhosts; cd $WD && $3" &
16              else
17                      rsh $2 "rm -f $HOME/.rhosts; cd $WD && $3" 
18              fi
19      }
20      
21      kill_all()
22      {
23              for i in $USEDHOSTS
24              do
25                      remote_cmd fg $i "$KILL_IT" 2>&1 > /dev/null
26              done
27      }
28      
29      
30      
31      
32      
33      
34      
35      
36      
37      
38      
39      
40      
41      
42      kill_all
43      sleep 2
44      
45      echo 1
46      rmiregistry & 
47      echo "Waiting for rmiregistry .... chrr ... "; sleep 1
48      java CatServer &
49      
50      echo 2
51      remote_cmd bg yps "rmiregistry &" 
52      
53      echo 3
54      echo "Waiting for rmiregistry .... chrr ... "; sleep 2
55      remote_cmd bg yps "java MouseServer &"
56      
57      echo 4
58      echo "Waiting for the servers  .... chrr ... "; sleep 2
59      remote_cmd fg yps "java Client $HOSTNAME stones"
60      
61      kill_all
62      
63      exit 0
64      

Source Code: Src/16_MS/fireItUp


back next up down toc Memos schedule allInOne pdf PDF mail

Created by unroff, java2html & & hp-tools. © by csfac. All Rights Reserved (2010).
It is not allowed to print these pages on a CAST printer.
Last modified: 01/April/10 (17:16)