[Aglets-commits] aglets/src build.xml, 1.13, 1.14 TahitiDaemonClient.java, 1.3, 1.4 ServerApp.java,
Status: Beta
Brought to you by:
cat4hire
|
From: Luca F. <cat...@us...> - 2009-07-28 07:05:05
|
Update of /cvsroot/aglets/aglets/src In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv18746/src Added Files: build.xml TahitiDaemonClient.java ServerApp.java WatcherClient.java Log Message: Merge of experimental branch. --- NEW FILE: WatcherClient.java --- /* * @(#)WatcherClient.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.message.Message; import com.ibm.aglet.system.Aglets; import java.net.URL; import java.util.Properties; /** * The WatcherClient example illustrates how to write an application program * with Aglets. With this API, you can create and dispatch an aglet, * send the aglet a message and receive a result from it. * To run this example, you need the server and WatcherSlave Aglet. * * @version 1.00 $Date: 2009/07/28 07:04:53 $ * @author Mitsuru Oshima * @see aglet.system.Aglets * @see examples.watcher.ProxyWatcher * @see examples.watcher.WatcherSlave */ public class WatcherClient { public static void main(String a[]) throws java.lang.Exception { if (a.length < 1) { System.out .println("WatcherClient firstAddress secondAddress ..."); return; } System.out.println("========= Creating in " + a[0]); AgletProxy proxy = Aglets.createAglet(a[0], null, "keio.ics.nak.watcher.WatcherSlave", null); System.out.println((String)proxy.sendMessage(new Message("getInfo"))); int i = 1; while (a.length > i) { try { System.out.println("========== Dispatching to " + a[i]); proxy = proxy.dispatch(new URL(a[i++])); String str = (String)proxy.sendMessage(new Message("getInfo")); System.out.println(str); } catch (Exception ex) { ex.printStackTrace(); break; } } proxy.dispose(); } } --- NEW FILE: build.xml --- <?xml version="1.0"?> <!-- Build Script for Aglets for Java 2 compatible Aglet Software Development Kit (ASDK) $Revision: 1.14 $ $Date: 2009/07/28 07:04:53 $ $Author: cat4hire $ --> <project name="Aglets" default="all" basedir="."> <!-- Build Properties --> <property name="Name" value="aglets"/> <property name="version" value="2.0.2"/> <property name="src.dir" location="."/> <!-- Dependent Libraries and compiled classes --> <property name="lib.dir" location="../lib"/> <property name="lib.classes.dir" location="../lib/classes"/> <!-- Configuration source directory --> <property name="cnf.dir" location="../cnf"/> <!-- directory for docs --> <property name="doc.dir" location="../doc"/> <!-- Directory holding user aglets. --> <property name="public.dir" location="../public"/> <!-- Use Java 2 compiler --> <property name="build.compiler" value="modern" /> <!-- Project classpath. Imports sytem classpath and then adds all jar files found in the lib directory. --> <path id="project.classpath"> <pathelement path="${classpath}"/> <fileset dir="${lib.dir}"> <include name="*.jar"/> </fileset> </path> <!-- Same as project.classpath with project classes inlcuded. --> <path id="app.classpath"> <pathelement path="${lib.classes.dir}"/> <path refid="project.classpath"/> </path> <!-- Top level target. Redirect to all-release --> <target name="all" depends="all-release"/> <!-- Top level target. Builds java files with deprecation off and optimization on. --> <target name="all-release" depends="release-flags,prepare,lib,stubs,jar,public,examples,doc"/> <!-- Top level target. Builds java files with deprecation on and optimization off. --> <target name="all-dev" depends="dev-flags,prepare,lib,stubs,jar,public,doc"/> <!-- Top level target. Builds java files with development settings. No jar or examples. --> <target name="dev" depends="dev-flags,prepare,lib,stubs,public"/> <!-- Set flags for compiler (Dev) --> <target name="dev-flags"> <property name="dep.flag" value="on"/> <property name="debug.flag" value="on"/> <property name="opt.flag" value="off"/> </target> <!-- Set flags for compiler (Release) --> <target name="release-flags"> <property name="dep.flag" value="off"/> <property name="debug.flag" value="off"/> <property name="opt.flag" value="on"/> </target> <!-- Initialize build. Checks for availabiltiy of dependent libraries. --> <target name="init" > <echo> Aglets SDK Build $Revision: 1.14 $ $Date: 2009/07/28 07:04:53 $ </echo> <tstamp/> <!-- Check for log4j classes --> <available classname="org.apache.log4j.Category" property="log4j.present" classpathref="project.classpath" /> </target> <!-- Creates lib.directory tree. This is not the installation tree. --> <target name="prepare" depends="init"> <mkdir dir="${lib.classes.dir}" /> <mkdir dir="${public.dir}" /> </target> <!-- If log4j is in project classpath, build the log4j logger. --> <target name="logger" if="log4j.present"> <echo message="Log4j found. Building logger."/> <javac srcdir="${src.dir}" destdir="${lib.classes.dir}" debug="${debug.flag}" deprecation="${dep.flag}" optimize="${opt.flag}" > <classpath refid="project.classpath"/> <include name="org/aglets/log/log4j/*.java"/> </javac> </target> <!-- Compile the ASDK java files --> <target name="lib" depends="prepare,logger"> <javac srcdir="${src.dir}" destdir="${lib.classes.dir}" debug="${debug.flag}" deprecation="${dep.flag}" optimize="${opt.flag}" > <classpath refid="project.classpath"/> <include name="com/ibm/**/*.java"/> <include name="TahitiDaemonClient.java"/> <include name="org/aglets/log/**/*.java"/> <exclude name="com/ibm/agletx/**/*.java"/> <exclude name="org/aglets/log/log4j/*.java"/> </javac> </target> <!-- Build RMI stubs --> <target name="stubs" depends="lib"> <rmic base="${lib.classes.dir}" includes="com/ibm/maf/rmi/*_RMIImpl.class"/> </target> <!-- Build the helper classes not part of the core. --> <target name="public" depends="prepare"> <javac srcdir="${src.dir}" destdir="${public.dir}" debug="${debug.flag}" deprecation="${dep.flag}" optimize="${opt.flag}" > <classpath> <path refid="app.classpath"/> </classpath> <include name="com/ibm/agletx/**/*.java"/> </javac> </target> <!-- Build the example aglets --> <target name="examples" depends="prepare"> <javac srcdir="${src.dir}" destdir="${public.dir}" debug="${debug.flag}" deprecation="${dep.flag}" optimize="${opt.flag}" > <classpath> <path refid="app.classpath"/> </classpath> <include name="examples/**/*.java"/> <exclude name="examples/test/security/*.java"/> <exclude name="examples/protection/**/*.java"/> </javac> </target> <!-- Somebody's security test --> <target name="test.security" depends="prepare"> <javac srcdir="${src.dir}" destdir="${lib.classes.dir}" debug="on" deprecation="on" optimize="off" > <classpath> <path refid="app.classpath"/> </classpath> <include name="examples/test/security/*.java"/> </javac> </target> <!-- Build the ASDK jar file --> <target name="jar" depends="lib"> <jar jarfile="${lib.dir}/${Name}-${version}.jar"> <fileset dir="${lib.classes.dir}"> <include name="com/ibm/**/*.class"/> <include name="org/aglets/log/**/*.class"/> <exclude name="com/ibm/agletx/**/*.class"/> </fileset> </jar> </target> <!-- Build API docs from source code. --> <target name="doc" depends="prepare"> <javadoc packagenames="org.aglets.*,com.ibm.*" classpathref="app.classpath" sourcepath="${src.dir}" destdir="${doc.dir}" author="true" version="true" use="true" private="yes" windowtitle="Aglets Development Kit" doctitle="<h1>Aglets Development Kit</h1>"> </javadoc> </target> <!-- Cleans the tree for release. --> <target name="tidy" depends="init"> <delete dir="${lib.classes.dir}" /> <delete dir="${src.dir}/lib" /> <delete dir="${src.dir}/cnf" /> <delete dir="${src.dir}/scripts" /> <delete> <fileset dir=".." defaultexcludes="no" > <include name="**/*~"/> <include name="**/CVS/"/> </fileset> </delete> </target> <!-- Cleans the tree of all generated files. --> <target name="clean-all" depends="tidy"> <delete> <fileset dir="../bin"> <exclude name="*.in"/> <exclude name="install*"/> <exclude name="build.xml"/> <exclude name="ant*"/> <exclude name="lcp.bat"/> </fileset> </delete> <delete dir="${public.dir}" /> <delete includeEmptyDirs="true"> <fileset dir="${doc.dir}"> <include name="**/*"/> <exclude name="spec11.html"/> </fileset> </delete> </target> <target name="noCvs"> <delete includeEmptyDirs="true"> <fileset dir=".." defaultexcludes="no"> <include name="**/CVS/" /> </fileset> </delete> </target> </project> --- NEW FILE: TahitiDaemonClient.java --- /* * $Id: TahitiDaemonClient.java,v 1.4 2009/07/28 07:04:53 cat4hire Exp $ * * @(#)TahitiDaemonClient.java * * @author Lary Spector * @created July 22, 2001 * * TahitiDaemonClient is a java application used to communicate with * and control a running TahitiDaemon. * * TahitiDaemon implements a Tahiti service which listens on * a configurable control port for commands. */ import java.io.*; import java.net.*; public class TahitiDaemonClient { private static boolean _verbose = false; private static boolean _dont_connect = false; private static int _control_port_num = 5545; private static String hostname; private static boolean _banner_version_OK = false; private static int _socket_timeout = 10000; // 10 seconds /* * Banner and version Strings */ private static String _banner_string = "TahitiDaemon"; private static String _version_string = "1.0"; private static String helpMsg = "help Display this message. \n" + "quit Disconnect from the server and quit. \n" + "shutdown Shutdown the server and quit. \n" + "reboot Reboot the server and quit. \n" + "list List all aglets in the server. \n" + "msg on|off Turns message printing on/off, default is off. \n" + "debug on|off Debug output on/off, default is off. \n" + "create [codeBase] name Create new aglet. \n" + "<aglet> dispatch URL Dispatch the aglet to the URL. \n" + "<aglet> clone Clone the aglet. \n" + "<aglet> dispose Dispose the aglet. \n" + "<aglet> dialog Request a dialog to interact with.\n" + "<aglet> property Display properties of the aglet.\n" + "Note: <aglet> is a left most string listed in the result of list command. "; /* * Main method * Args: -help -verbose -controlport -host */ public static void main(String[] args) throws IOException { String prompt = ">"; Socket clientSocket = null; PrintWriter out = null; BufferedReader in = null; parseArgs(args); // bail out if the user only asked for help. if (_dont_connect) System.exit(1); try { clientSocket = new Socket(hostname, _control_port_num); out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); clientSocket.setSoTimeout(_socket_timeout); } catch (UnknownHostException e) { System.err.println("\nUnknown host: " + hostname); System.exit(1); } catch (IOException e) { System.err.println("\nCouldn't get I/O for the connection to: " + hostname); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String fromServer = "start"; String fromUser; Thread.currentThread().setPriority(1); // Try reading from the Daemon try { fromServer = in.readLine(); } catch (IOException ex) { System.err.println("\nFailure reading from: " + hostname + ":" + _control_port_num); if (_verbose) { ex.printStackTrace(); } System.exit(1); } /* * Handshake with the Daemon, check banner and version */ try { if (fromServer != null) { if (_verbose) { System.out.println("Server banner: " + fromServer); } if (fromServer.equals(_banner_string)) { if (_verbose) { System.out.println("Banner check OK."); } if ((fromServer = in.readLine()) != null) { if (_verbose) { System.out.println("Server version: " + fromServer); } if (fromServer.equals(_version_string)) { if (_verbose) { System.out.println("Version check OK."); } _banner_version_OK = true; } else { System.out.println("Version mismatch."); System.out.println("Client version is: " + _version_string); System.out.println("Server version is: " + fromServer); } } } else { System.out.println("Banner check failed."); System.out.println("Client banner is: " + _banner_string); System.out.println("Server banner is: " + fromServer); } } } catch (IOException ex) { System.err.println("\nFailure reading from: " + hostname + ":" + _control_port_num); if (_verbose) { ex.printStackTrace(); } System.exit(1); } /* * If the banner and version strings are OK, * start handling commands. */ if (_banner_version_OK) { try { // read the connection info if ((fromServer = in.readLine()) != null) { // display it if verbose if (_verbose) { System.out.println("Server: " + fromServer); } } } catch (IOException ex) { System.err.println("\nFailure reading from: " + hostname + ":" + _control_port_num); if (_verbose) { ex.printStackTrace(); } System.exit(1); } while (true) { try { // Display the Prompt System.out.print(prompt + " "); System.out.flush(); // Handle client side input fromUser = stdIn.readLine(); if (fromUser != null) { if (_verbose) System.out.println("Client: " + fromUser); // Special cases for help and quit if ("help".equalsIgnoreCase(fromUser)) { System.out.println (helpMsg); System.out.flush(); } if ("quit".equalsIgnoreCase(fromUser)) { System.out.println ("Closing client connection"); System.out.flush(); break; } // Send command to server out.println(fromUser); } // Read reply from the server fromServer = in.readLine(); // Special cases for shutdown and reboot if (_verbose) { System.out.println("Server: " + fromServer); } if (fromServer.equals("shutting down")) { System.out.println("Server shutting down, closing client connection."); break; } else if (fromServer.equals("rebooting")) { System.out.println("Server rebooting, closing client connection."); break; } else { //handle everything else. System.out.println(fromServer); while (!((fromServer = in.readLine()).equals("done."))) { System.out.println(fromServer); } } } catch (IOException ex) { System.err.println("\nFailure reading from: " + hostname + ":" + _control_port_num); if (_verbose) { ex.printStackTrace(); } break; } catch (Throwable ex) { if (_verbose) { ex.printStackTrace(); } break; } } } out.close(); in.close(); stdIn.close(); clientSocket.close(); System.exit(1); } private static void usage() { System.err.println("\nTahitiDaemonClient [-verbose] [-host <hostname>] [-controlport <port>] [-help]\n"); System.err.println("Connects to a Tahiti Daemon and provides an interface to control it.\n"); System.err.println("options:"); System.err.println(" -verbose verbose output"); System.err.println(" -host <hostname>, default is localhost"); System.err.println(" -controlport <port number>, default is port " + _control_port_num); System.err.println(" -timeout <timeout>, in milliseconds, 0 (zero) is infinite, default is " + _socket_timeout); System.err.println(" -help prints this message"); // signal that the program // should not continue the connection process. _dont_connect=true; } private static void parseArgs(String[] args) { if (_verbose) System.out.println ("Parsing Args...\n"); if (args.length <= 0) return; for (int i = 0; i < args.length; i++) { if (args[i].equalsIgnoreCase("-help")) { usage(); break; } else if (args[i].equalsIgnoreCase("-verbose")) { _verbose = true; } else if (args[i].equalsIgnoreCase("-controlport")) { if (i + 1 >= args.length) { usage(); break; } i++; try { _control_port_num = Integer.parseInt(args[i]); } catch (NumberFormatException ex) { System.err.println("\nError! controlport <" + args[i] + "> is invalid, it must be an integer "); _dont_connect=true; break; } } else if (args[i].equalsIgnoreCase("-timeout")) { if (i + 1 >= args.length) { usage(); break; } i++; try { _socket_timeout = Integer.parseInt(args[i]); } catch (NumberFormatException ex) { System.err.println("\nError! timeout <" + args[i] + "> is invalid, it must be an integer "); _dont_connect=true; break; } } else if (args[i].equalsIgnoreCase("-host")) { if (i + 1 >= args.length) { System.err.println("\nError! hostname was not specified after -host argument"); usage(); break; } i++; hostname = args[i]; } else { System.err.println("\nUnknown argument: " + args[i] + "\n"); usage(); break; } } } } --- NEW FILE: ServerApp.java --- /* * @(#)ServerApp.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import java.awt.Frame; import java.net.URL; import java.net.UnknownHostException; import java.io.File; import java.util.Properties; import com.ibm.maf.*; import com.ibm.aglet.*; import com.ibm.aglet.message.Message; import com.ibm.aglet.system.*; import com.ibm.aglets.MAFAgentSystem_AgletsImpl; import com.ibm.aglets.tahiti.Tahiti; import java.security.AccessController; import java.security.PrivilegedAction; import com.ibm.awb.misc.*; /** * The ServerApp example illustrates how to embed the AgletsServer facility * into an application program. * * @version 1.00 $Date: 2009/07/28 07:04:53 $ * @author Mitsuru Oshima * @see com.ibm.aglet.system.AgletRuntime * @see com.ibm.aglets.Tahiti.Main */ public class ServerApp extends ContextAdapter { // additional options final static Opt options[] = { Opt.Entry("-protocol", "maf.protocol", null), Opt.Entry("-username", "username", null), Opt.Entry("-password", "password", null), }; public void agletActivated(ContextEvent ev) { System.out.println("Aglet Activated : " + ev.getAgletProxy()); } public void agletArrived(ContextEvent ev) { System.out.println("Aglet Arrived : " + ev.getAgletProxy()); } public void agletCloned(ContextEvent ev) { System.out.println("Aglet Cloned : " + ev.getAgletProxy()); } public void agletCreated(ContextEvent ev) { System.out.println("Aglet Created : " + ev.getAgletProxy()); } public void agletDeactivated(ContextEvent ev) { System.out.println("Aglet Deactivated : " + ev.getAgletProxy()); } public void agletDispatched(ContextEvent ev) { System.out.println("Aglet Dispatched : " + ev.getAgletProxy()); } public void agletDisposed(ContextEvent ev) { System.out.println("Aglet Disposed : " + ev.getAgletProxy()); } public void agletReverted(ContextEvent ev) { System.out.println("Aglet Reverted : " + ev.getAgletProxy()); } public static void main(String args[]) throws java.lang.Exception { Opt.setopt(options); AgletRuntime runtime = AgletRuntime.init(args); String[] r = (String[])AccessController.doPrivileged(new PrivilegedAction() { public Object run() { String[] results = new String[2]; String userName = System.getProperty("user.name"); results[0] = System.getProperty("username", userName); results[1] = System.getProperty("password", ""); return results; } }); String username = r[1]; String password = r[2]; Object obj = runtime.authenticateOwner(username, password); if (obj == null) { System.err.println("Cannot authenticate the user \"" + username + "\""); throw new Exception("User authentication failed."); } /* * User authentication (optional) * If this program requests user identification from dialog box, * we need the following call. */ /* * if (Main.login(runtime) == null) return; */ MAFAgentSystem maf_system = new MAFAgentSystem_AgletsImpl(runtime); String protocol = "atp"; protocol = (String)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return System.getProperty("maf.protocol", "atp"); } }); MAFAgentSystem.initMAFAgentSystem(maf_system, protocol); Tahiti.initializeGUI(); Tahiti.installFactories(); /* * Create named contexts. To dispatch to this context, * sender has to secify the destination like, * "atp://aglets.trl.ibm.com:434/test" * Defining multiple contexts is also possible. * * Support of multiple context is experimental function. * This feature may drop in the future. */ // first context AgletContext cxt = runtime.createAgletContext("test"); cxt.addContextListener(new ServerApp()); // second context AgletContext cxt2 = runtime.createAgletContext("test2"); cxt2.addContextListener(new ServerApp()); Tahiti.installSecurity(); MAFAgentSystem.startMAFAgentSystem(maf_system, protocol); // start contexts cxt.start(); cxt2.start(); /* * From this point, you can use contexts. * (creating, dispatching an aglet, etc.) * 1. Create HelloAglet in cxt, * 2. And, dispatches it to cxt2. * See the source code for the detail of HelloAglet. */ AgletProxy p = cxt.createAglet(null, "examples.hello.HelloAglet", null); Message msg = new Message("startTrip", cxt2.getHostingURL().toString()); p.sendMessage(msg); } public void showMessage(ContextEvent ev) { System.out.println("message : " + ev.getMessage()); } } |