 |
-- JohnCatherino - 15 Jun 2005
Using cajo Multicasting
This page is the result of questions regarding the use of the gnu.cajo.utils.Multicast class.
Here we will set out a simple two-class example, which will hopefully make its use clear. Both classes can be run on the same machine, or separate ones.
The first class is called Testee.java; it listens for a multicast announcement:
import gnu.cajo.utils.Multicast;
public class Testee {
static Object object = new Testee();
public Object multicast(Multicast m) { // multicast listener callback
try { System.out.println(m.item.invoke("doit", "hello!")); }
catch (Exception x) { x.printStackTrace(); }
synchronized(this) { notify(); } // end program
return this; // stop listening, else return null
}
public static void main(String args[]) throws Exception { // unit test
Multicast m = new Multicast();
m.listen(object);
synchronized(object) { object.wait(); }
}
}
It should be running before starting the second example class; Tester.java:
import gnu.cajo.utils.Multicast;
public class Tester {
static Object object = new Tester();
public String doit(String message) { // remotely callable method
System.out.println("Doing it " + message);
synchronized(this) { notify(); } // end program
return "Didit!";
}
public static void main(String args[]) throws Exception { // unit test
Multicast m = new Multicast();
m.announce(object, 3); // broadcast reference short distance
synchronized(object) { object.wait(); }
System.exit(0);
}
}
To compile and run the Testee class, use the following command lines:
javac -classpath cajo.jar Testee.java
java -classpath cajo.jar;. Testee
Then to compile and run the Tester class, use the following command lines:
javac -classpath cajo.jar Tester.java
java -classpath cajo.jar;. Tester
Tester will make a multicast announcement of a reference to its object. Testee will receive this reference, and invoke its doit method. This will result in the Teser object object printing the following line to its console:
Doing it hello!
This will effectively end the execution of Tester. However, not before its remotely callable method returns a String; this will cause Testee to print the following line to its console:
Didit!
Then it will terminate its execution as well.
|