package de.unimuenchen.informatik.mnm.masa.agentSystem.voyagerintegration; import de.unimuenchen.informatik.mnm.masa.agentSystem.nameservice.*; import de.unimuenchen.informatik.mnm.masa.agent.voyagermasagateway.*; import de.unimuenchen.informatik.mnm.masa.tools.*; import com.objectspace.voyager.directory.*; import com.objectspace.voyager.reference.*; import com.objectspace.voyager.corba.*; import com.objectspace.voyager.orb.*; import com.objectspace.voyager.*; import com.objectspace.lib.util.Console; import org.omg.CosNaming.*; import org.omg.CORBA.*; import java.util.Vector; /** * The NamingGateway scans the Namespaces of Voyager servers and notifies * created or terminated agents to the VoyagerMasaGatewayStationaryAgent. * The NamingGateway is startet by his main, e.g. in a special thread * of the VoyagerMasaGatewayStationaryAgent */ public class NamingGateway { /** * Timerintervall between updates */ private static final int POLLINGTIME = 5000; /** * Compares to list of Strings and return the difference * * @return a-b, if a=b or a<b null */ private static String[] getDifference(String[] a, String []b){ Vector difference = new Vector(); int num=0; boolean contains = false; for(int i=0;i<a.length;i++){ for(int j=0; j<b.length && !contains; j++) if(a[i].equals(b[j])) contains = true; if (!contains) { difference.addElement(a[i]); num++; } contains = false; } if (num == 0) return null; // b>=a String [] result = new String [num]; difference.copyInto(result); return result; } /** * URL of the Voyager-Server */ private String[] _vurls; /** * List of all voyager agents on all voyager server */ private String [][] agents; /** * Agnet to communicate with masa */ private VoyagerMasaGateway gatewayAgent; /** * Constructor */ public NamingGateway(String [] vurls){ this._vurls = vurls; agents = new String[vurls.length][]; gatewayAgent = getVoyagerMasaGatewayStationaryAgent(); } /** * Gets the Voyager's Directory of the namingservice */ private IDirectory getVoyagerDirectory(String vurl) throws Exception { // Get Voyager Directory Console.setLogLevel(Console.VERBOSE); // Proxy from Server IDirectory directory; RemoteAddress addr = new RemoteAddress(vurl,Class.forName("com.objectspace.voyager.directory.Directory").getName(),-9); // -9=Directory of Namespace, not documented by Voyager IProxy proxy = addr.bind(true).newProxy(); directory = (IDirectory) proxy; return directory; } /** * Lists all voyager remote objects bound to the voyager's namespace * */ private synchronized String[] listAllVoyagerAgents(String vurl){ String[] result=new String[0]; try{ try{ Voyager.startup(); }catch(com.objectspace.voyager.StartupException e){ System.err.println("WARNING: " + e); } IDirectory directory = getVoyagerDirectory(vurl); java.lang.Object[] keys = directory.getKeys(); result = new String[keys.length]; for(int i=0; i<result.length; i++){ result[i] = keys[i].toString(); } Voyager.shutdown(); } catch (Exception e){ // Exceptions if there is no Voyager server if ((e instanceof com.objectspace.voyager.RuntimeRemoteException) || (e instanceof java.net.ConnectException)){ } else{ e.printStackTrace(); result = null; } } return result; } /** * Starts scanning the voyager's namespace * and reports changes to the VoyagerMasaGatewayStationaryAgent */ private void start(){ // check whether agents alread started for(int i=0; i<_vurls.length; i++){ agents[i] = listAllVoyagerAgents(_vurls[i]); if(agents[i].length != 0) gatewayAgent.agents_created(agents[i],_vurls[i]); } // start pooling int count_scanning=0; while(true){ // check every voyager server for agents for(int i=0; i<_vurls.length; i++){ // get new actual list of all agents running String [] agents_running = listAllVoyagerAgents(_vurls[i]); String [] created_agents = getDifference(agents_running,agents[i]); if (created_agents != null){ System.out.println("NamingGateway.start() - agents created on " + _vurls[i]); gatewayAgent.agents_created(created_agents,_vurls[i]); } String[] terminated_agents = getDifference(agents[i],agents_running); if (terminated_agents != null){ System.out.println("NamingGateway.start() - agents terminated on " + _vurls[i]); gatewayAgent.agents_terminated(terminated_agents); } agents[i] = agents_running; // save agent-lis for next polling loop } try{ Thread.sleep(POLLINGTIME); } catch (Exception e){ e.printStackTrace(); } count_scanning = count_scanning==255 ? 0 : count_scanning+1; } } /** * Gets the VoyagerMasaGatewayStationaryAgent via namingcontext * to communicate with masa. * Gets the Agent to handle the events of new created or terminated * voyager agents */ private VoyagerMasaGateway getVoyagerMasaGatewayStationaryAgent(){ try{ // get namingcontext ORB orb = ORB.init(); String ior = NamingWebServer.getIORWWW(); org.omg.CORBA.Object objRef = orb.string_to_object(ior); NamingContext namingcontext = NamingContextHelper.narrow(objRef); NameComponent path_agent[] = {new NameComponent("Agent",""), new NameComponent("global",""), new NameComponent("VoyagerMasaGateway","")}; return VoyagerMasaGatewayHelper.narrow(namingcontext.resolve(path_agent)); }catch(Exception e){ e.printStackTrace(); return null; } } /** * Start creates a object of the NamingGateway and start it */ public static void main(String[] args) throws Exception { NamingGateway naminggateway = new NamingGateway(args); naminggateway.start(); } }