GoThenBack.java 100644 253 243 4373 6754030736 12352 0 ustar taib crmper /* Test aglet
*/
import com.ibm.aglet.*;
import java.net.URL;
/** This Aglet will go to a specified destination,
* then will go back home. This is achieved by 2 successive
* dispatch calls. This is the very simplicity, and may be used
* to test a remote platform. I used it to see if I could send
* an agent on Todd's platform, and it did work...
*
* @author Ronnie Taib (Ronnie.Taib@crm.mot.com)
* @version 10/08/99
*/
public class GoThenBack extends Aglet {
/** home and target addresses
*/
URL
home = null,
remote = null;
/** Boolean flags to (simply) know where we are,
* and what to do next.
* The aglet will first be local (i.e. local and firstTime
* are true). Then it goes on the target (i.e. local
* and firstTime are false). When it goes back, it is
* local again, but firstTime is false.
* This should be replaced by a more sophisticated agent,
* with an Itinerary for example.
*/
boolean
local = true,
firstTime = true;
/* Initializes the aglet.
* Only called the very first time this aglet is created.
* @param init This agent should be passed the target
* platform URL as a String in the init variable.
*/
public void onCreation( Object init ){
try{
remote = new URL( (String)init );
}
catch( Exception e ){
System.err.println( "Init object is not a valid URL! " );
dispose();
}
// Initialize the variables.
home = getAgletContext().getHostingURL();
System.out.println( "I am alive. I will go from " +home
+" to " +remote );
}
public void run(){
try{
if ( local ){
if ( firstTime ){
local = false;
firstTime = false;
System.out.println( "Dispatching myself..." );
dispatch( remote );
}
else{
System.out.println( "I am back." );
dispose();
}
}
else{
local = true;
/* Uncomment the two following lines if you want your agent to output
* something on the target platform.
*/
// System.out.println( "Arriving on the remote platform.\n"
// +"Going back now..." );
dispatch( home );
}
}
catch( Exception e ){
System.err.println( "Could not dispatch, because: ");
e.printStackTrace();
}
}
} // End GoThenBack
MiniServer.java 100644 253 243 14114 6754035551 12502 0 ustar taib crmper /* Aglets minimal server
*/
import com.ibm.atp.daemon.*;
import com.ibm.aglet.system.*;
import com.ibm.aglet.*;
import java.util.Vector;
import java.io.IOException;
/** This class is a generic standalone Aglets server.
* It is highly inspired from the Aglets' toolkit example!
* This server keeps an agent listener, displaying all the actions
* made by the agents (creation, moves, disposal).
*
* @author Ronnie Taib (Ronnie.Taib@crm.mot.com)
* @version 10/08/99
*/
public class MiniServer {
private static Integer
portNb = null;
private AgletContext
context;
/** The MiniServer constructor. It initializes and starts
* the server.
* @param args The arguments for the server initialization.
* They are the same as those given to Tahiti.
*/
MiniServer( String [] args ){
try{
if ( Main.importOptions(args) == false )
throw new IOException();
}
catch( IOException ioe ){
System.err.println( "Unvalid options for the server. Aborting!" );
System.exit( 1 );
}
Daemon daemon = Daemon.init( args );
AgletRuntime runtime = AgletRuntime.init( args );
context = runtime.createAgletContext( "" );
daemon.start( "aglets" );
context.start();
// Begin to listen to new aglets
context.addContextListener( new CL() );
}
/** The context listener prints a message for each action
* performed by an Aglet.
*/
class CL extends ContextAdapter {
/** This is method is called by the context when an Aglet
* is created.
* @param ev A context event containing information on
* the Aglet action.
*/
public void agletCreated( ContextEvent ev ){
AgletProxy proxy = ev.getAgletProxy();
try{
System.out.println( "Aglet is being created:\n"
+proxy.getAgletInfo() );
}
catch( InvalidAgletException iae ){
System.err.println( iae );
}
}
/** This is method is called by the context when an Aglet
* arrives after a dispatch.
* @param ev A context event containing information on
* the Aglet action.
*/
public void agletArrived( ContextEvent ev ){
AgletProxy proxy = ev.getAgletProxy();
try{
System.out.println( "Aglet is arriving:\n"
+proxy.getAgletInfo() );
}
catch( InvalidAgletException iae ){
System.err.println( iae );
}
}
/** This is method is called by the context when an Aglet
* is dispatched.
* @param ev A context event containing information on
* the Aglet action.
*/
public void agletDispatched( ContextEvent ev ){
AgletProxy proxy = ev.getAgletProxy();
try{
System.out.println( "Aglet is leaving:\n"
+proxy.getAgletInfo() );
}
catch( InvalidAgletException iae ){
System.err.println( iae );
}
}
/** This is method is called by the context when an Aglet
* is disposed.
* @param ev A context event containing information on
* the Aglet action.
*/
public void agletDisposed( ContextEvent ev ){
AgletProxy proxy = ev.getAgletProxy();
try{
System.out.println( "Aglet is being disposed of:\n"
+proxy.getAgletInfo() );
}
catch( InvalidAgletException iae ){
System.err.println( iae );
}
}
}
/** Prints the usage for the standalone server
*/
public static void usage(){
System.out.println( "Usage: java MiniServer "
+"-port "
+"[-start anAgletPackage.AgletClassName "
+"[\"optional_parameter_string\"]]...");
System.exit( 0 );
}
/** The main method is used to launch the server as a standalone
* application.
* @param myArgs The command-line arguments, including a port
* number, optionally information on some Aglets to be started at
* boot, and finally classical information (the same as you would
* supply to Tahiti).
*/
public static void main( String [] myArgs ){
/* Determines if there are boot aglets to start
* on the command-line.
*/
Vector
startClasses = new Vector(), // Class names of the boot Aglets
initArgs = new Vector(), // Arguments to pass to the boot Aglets
classicArgs = new Vector(); // Classical command-line arguments
for ( int argNb = 0; argNb < myArgs.length; argNb++ ){
if ( myArgs[argNb].equals( "-help" ) )
usage();
if ( myArgs[argNb].equals( "-start" ) ) {
argNb++;
if ( argNb >= myArgs.length ){
System.err.println( "Boot class not specified!" );
break;
}
startClasses.addElement( myArgs[ argNb ] );
// Are there arguments specified for this boot aglet?
if ( ( argNb+1 < myArgs.length) &&
( !myArgs[ argNb+1 ].startsWith("-") ) ){
argNb++;
initArgs.addElement( myArgs[ argNb ] );
}
else
initArgs.addElement( null );
}
else
classicArgs.addElement( myArgs[argNb] );
}
/* Gets the port number:
* 1. Verifies that the port number has been filled
* 2. Gets its value
*/
int
portIndex = classicArgs.indexOf( "-port" );
if ( ( portIndex == -1 ) ||
( portIndex+1 >= classicArgs.size() ) ){
System.err.println( "No port number specified!" );
System.exit( 1 );
}
try {
portNb =
new Integer( (String)classicArgs.elementAt( portIndex+1 ) );
if ( portNb == null ) throw new Exception();
} catch (Exception e){
System.err.println( "Unvalid port number specified!" );
System.exit( 1 );
}
/* Generates a new args array without the boot aglet information,
* to pass it to the server.
*/
String
args[] = new String[ classicArgs.size() ];
for ( int i = 0; i < classicArgs.size(); i++ )
args[i] = (String)classicArgs.elementAt( i );
/** Starts the server
*/
MiniServer
myServ = new MiniServer( args );
/* Launches the boot aglets with their arguments
*/
for ( int i=0; i