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

📄 napletdirectoryimpl.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
/*
 * @<#> NapletDirectoryImpl.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.directory;

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.util.*;

import naplet.*;

/**
 * The <code>NapletDirectoryImpl</code> class implements a naplet
 * directory that provides location information of long-lived alive naplets.
 *
 * @version 0.0.1, 1/1/2000
 * @author C. Xu
 */

public class NapletDirectoryImpl
		extends java.rmi.server.UnicastRemoteObject
		implements NapletDirectory, Runnable
{
	private HashMap directory;
	private int port;
	private String dirName;

	/**
	 * Constructs a NapletDirectory
	 * @param dirName the directory service name that is registered to a RMI
	 *        registry. By default, it is NapletDirectory
	 * @port port the directory service port
	 *
	 */
	public NapletDirectoryImpl( int port, String name )
			 throws RemoteException
	{
		if ( port < 0 || port > 0xFFFF )
		{
			System.out.println(
					"Naplet directory port out of range: " +
					port );
			System.exit(1);
		}
		this.port = port;
		this.dirName = name;

		directory = new HashMap();
		( new Thread( this ) ).start();
	};

	public void run()
	{

		if ( System.getSecurityManager() == null )
		{
			System.setSecurityManager( new RMISecurityManager() );

		}
		try
		{
			Registry reg = LocateRegistry.createRegistry( port );
			reg.rebind( dirName, this );
			System.out.println( dirName + " is bounded in registry" );
		}
		catch ( Exception e )
		{
			System.out.println( "NapletDirectory err:" + e.getMessage() );
		}
	}

	/**
	 * Register a naplet event: creation, arrival, departure,
	 * or destroy of a naplet, its in the naplet directory.
	 *
	 * @param nid Naplet identifier
	 * @param server Server where naplet is in
	 * @param time When the event occurs
	 * @param event Event type
	 */
	public synchronized void register( NapletID nid, URN server, Date time,
					   int event ) throws RemoteException
	{

		try
		{
			FootPrint fp = new FootPrint( server, time, event );
			String key = nid.toString();

			if ( directory.containsKey( key ) )
			{
				ArrayList fpList = ( ArrayList ) directory.get( key );
				fpList.add( fp );
			}
			else
			{
				ArrayList fpList = new ArrayList();
				fpList.add( fp );
				directory.put( key, fpList );
			}
		}
		catch ( InvalidEventException iee )
		{
			throw new DirectoryAccessException( iee.getMessage() );
		}

		System.out.println( "Naplet " + nid.toString() + " registered: " +
							event );
	}

	//
	//  There's is a timing clocking problem with this implementation
	//  To ensure the correctness, need logical clock implemenation
	//
	public synchronized URN lookup( NapletID nid ) throws RemoteException
	{
		String key = nid.toString();
		URN nsa;
		FootPrint fp;
		if ( directory.containsKey( key ) )
		{
			ArrayList list = ( ArrayList ) directory.get( key );
			fp = ( FootPrint ) list.get( list.size() - 1 );
			int event = fp.getEvent();
			if ( event == NapletEvent.ARRIVE || event == NapletEvent.DISPATCH )
			{
				nsa = fp.getServer();
				return nsa;
			}
			else
			{
				throw new NapletInternalError(
						"Directory contains naplets with invalid events" );
			}
		}
		else
		{
			throw new RemoteException( "NapletID is not found in the directory" );
		}
	}

	// public synchronized Vector checkTrace( String name )
	//				throws RemoteException {
	//      return (Vector) directory.get(name);
	// }

	// public synchronized void save (OutputStream out, String fname) {};
	// public synchronized void load (InputStream in) throws IOException {};

}

⌨️ 快捷键说明

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