⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 napletmanagerimpl.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
/* * @<#>NapletManagerImpl.java version 0.0.1, 1/1/2000 * * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION. * * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS. * * Copyright (c) 2000 Wayne State University. All Rights Reserved. */package naplet.server;import java.rmi.*;import java.rmi.server.*;import java.util.*;import naplet.*;import naplet.directory.*;import naplet.message.*;import naplet.tracing.*;import naplet.itinerary.ItineraryPattern;/** * The <code>NapletManagerImpl</code> is an implementation of * the <code>NapletManager</code> interface. It maintains the * information about its outstanding naplets in a <code>NapletTable</code> * object. The <code>NapletManagerImpl</code> is defined as a * singleton class. Each naplet server is associated with at most one * instance of the class. * * @see NapletManager * * @version 0.0.1, 1/1/2000 * @author C. Xu */public class NapletManagerImpl   extends UnicastRemoteObject   implements NapletManager, Runnable{      private static NapletManagerImpl instance = null;      /**       * List of outstanding naplets.       * Each record contains detailed information about       * NapletID, Listener, destination, depart time, status, description       */      private NapletTable table;      private ServerProperty property;      /**       * Constructor, called by <code>NapletServer</code>+       */      protected NapletManagerImpl( ServerProperty property )	 throws RemoteException      {	 table = new NapletTable();	 this.property = property;      }      public static NapletManagerImpl getInstance( ServerProperty property )	 throws RemoteException      {	 if ( instance == null )	 {	    instance = new NapletManagerImpl( property );	 }	 return instance;      }      public void run()      {}      public URN getServerURN( )	 throws RemoteException      {	 return property.getServerURN();      }      /**       * To launch a naplet       *       * @param nap the naplet to be launched       * @see NapletTable       * @see NapletRecord       *       */      public void launch( Naplet nap )	 throws RemoteException      {	 if ( nap == null )	 {	    throw new RemoteException( "Launch a null naplet" );	 }	 if ( table.containsKey( nap.getNapletID() ) )	 {	    throw new RemoteException( "NaplidID is duplicated" );	 }	 // A naplet proxy is nothing but a NapletID and an associated mailbox	 NapletID proxyID = nap.getNapletID().getProxyID();	 // Create a permanent mailbox for naplet-proxy residing on its local	 // naplet manager	 MailBox mbox = property.getMessenger().openMailBox( proxyID );	 // If NapletDirectory is enabled, register naplet creation event	 // with the directory	 if ( property.getNapletDirectory() != null )	 {	    try	    {	       property.getNapletDirectory().register( proxyID,						       property.getServerURN(),							       new Date(),						       NapletEvent.DISPATCH );	    }	    catch ( DirectoryAccessException dae )	    {	       throw new RemoteException( 		  "Unable to register naplet in directory" );	    }	 }	 NapletContext context = new NapletContext(	    property.getServerURN(),	    property.getNavigator(),	    property.getMessenger(), null );	 nap.init0( context );	 // Record the naplet in <code> NapletTable</code> for 	 // future reference	 NapletRecord rec = new NapletRecord( nap.getNapletID() );	 rec.setTime( new Date() );	 rec.setListener( nap.getListener() );	 rec.setItinerary( nap.getItinerary() );	 table.add( rec );	 // nap.getItinerary().travel( nap );	 ItineraryPattern first = nap.getItinerary().getRoute();	 if ( first == null )	 {	    System.out.println( "Naplet has no itinerary" );	 }	 else	 {	    first.go( nap );	 }      }      public URN locate( NapletID nid )	 throws RemoteException      {	 URN server = null;	 try	 {	    server = property.getLocator().lookup( nid );	 }	 catch ( NapletLocateException nte )	 {	    throw new RemoteException( "Unable to locate the naplet" );	 }	 return server;      }      public URN locate( NapletID nid, long timeout )	 throws RemoteException      {	 URN server = null;	 try	 {	    server = property.getLocator().lookup( nid, timeout );	 }	 catch ( NapletLocateException nte )	 {	    throw new RemoteException( "Unable to locate the naplet" );	 }	 return server;      }      /**       * Terminate an outstanding naplet and wait for a confirmation       * from the naplet.       *       * @param nid The naplet id to be terminated       */      public void terminate( NapletID nid )	 throws RemoteException      {	 URN ser = locate( nid );	 terminate( ser, nid );      }      /**       * Terminate an outstanding naplet in a specified server and       * wait for a confirmation from the naplet.       *       * @param ser Naplet server to which message addresses       * @param nid The naplet to be terminated       */      public void terminate( URN ser, NapletID nid )	 throws RemoteException      {	 NapletID proxyID = nid.getProxyID();	 Message msg = new Message( Message.TERMINATE, 				    proxyID, nid, "terminate" );	 try	 {	    property.getMessenger().send( proxyID, ser, msg );	 }	 catch ( NapletCommunicationException nce )	 {	    System.out.println( "Unable to post a message to " 				+ ser.toString() );	 }	 // wait for an acknowledgement	 MailBox mbox = property.getMessenger().getMailBox( proxyID );	 try	 {	    Message reply = mbox.getMessage();	    System.out.println( "Remote naplet is terminated: Ack" 				+ reply.getMessage() );	 }	 catch ( InterruptedException ie )	 {	    System.out.println(	       "Interrupted while waiting for acknowledge from naplet" );	    System.exit( 1 );	 }      }      /**       * Simply display the running status       */      public void status( NapletID nid, URN server, String msg )	 throws RemoteException      {	 System.out.println( " ++ Naplet <<"			     + nid.toString() + ">>" );	 System.out.println( " ++ reporting from " + 			     server.toString() + "---" );	 System.out.println( msg );      }      public void callback( NapletID nid )	 throws RemoteException      {};      /**       * Each <code>NapletTable</code> object contains detailed information       * about local outstanding naplets.       *       * @see NapletRecord       */      private class NapletTable      {	    private HashMap table;	    public NapletTable()	    {	       table = new HashMap();	    }	    public void add( NapletRecord rec )	    {	       if ( rec == null )	       {		  throw new NullPointerException( "Null NapletRecord" );	       }	       String key = rec.getNapletID().toString();	       if ( table.containsKey( key ) )	       {		  throw new NapletInternalError( "The NapletID " 						 + key + " is duplicated" );	       }	       else	       {		  table.put( key, rec );	       }	    }	    public void remove( NapletID nid )	       throws NoSuchElementException	    {	       if ( nid == null )	       {		  throw new NoSuchElementException( "Null NapletID" );	       }	       String key = nid.toString();	       if ( table.containsKey( key ) )	       {		  table.remove( key );	       }	       else	       {		  throw new NoSuchElementException( "No such Naplet exists" );	       }	    }	    public NapletRecord get( NapletID nid )	    {	       if ( nid == null )	       {		  throw new NullPointerException( "Null napletID" );	       }	       String key = nid.toString();	       return ( NapletRecord ) table.get( key );	    }	    public boolean containsKey( NapletID nid )	    {	       if ( nid == null )	       {		  throw new NullPointerException( "Null napletID" );	       }	       String key = nid.toString();	       if ( table.containsKey( key ) )	       {		  return true;	       }	       else	       {		  return false;	       }	    }      }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -