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

📄 napletmonitorimpl.java~1~

📁 移动Agent编程工具Naplet
💻 JAVA~1~
字号:
/*
 * @<#> NapletMonitorImpl.java version 0.1, 7/1.2001
 *
 * 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.util.*;
import naplet.*;

/**
 * The <code>NapletMonitorImpl</code> provides an implementation
 * of the <code>NapletMonitor</code> interface. It relies on a
 * <code>NapletThreadTable</code> to maintain the run-time information
 * of the residing alien naplets.
 *
 * @see NapletMonitor
 * @version 0.1, 7/1/2001
 * @author C. Xu, czxu@wayne.edu
 */
public class NapletMonitorImpl
    implements NapletMonitor, Runnable
{
    private static int MAX_NAPLETTHREAD_PRI = 8;
    private static NapletMonitorImpl instance = null;
    private ServerProperty property;
    private NapletThreadTable threadTable;
    /** table for server socket */
    private Hashtable nssTable = new Hashtable();
    protected NapletMonitorImpl( ServerProperty property )
    {
        this.property = property;
        threadTable = new NapletThreadTable();
    }

    public static NapletMonitorImpl getInstance( ServerProperty property )
    {
        if ( instance == null )
        {
            instance = new NapletMonitorImpl( property );
        }
        return instance;
    }

    public void join( Naplet nap )
    {
        ThreadGroup group = new ThreadGroup( nap.getNapletID().toString() );
        group.setMaxPriority( MAX_NAPLETTHREAD_PRI );

        NapletContext context = new NapletContext(
             property.getServerURN(),
             property.getNavigator(),
             property.getMessenger(),
             property.getResourceManager().getServiceProxy( nap.getNapletID() )
             );


        Thread thr = new Thread( group, new NapletThread( nap, context ) );

        threadTable.put( nap.getNapletID(), thr );

        /** record server here !!zhongxl:*/
        String svr = nap.getServer();
        String sid = nap.getServerID();
        if ( ( svr != null ) && ( sid != null ) )
        {
            nssTable.put( sid, svr );
        }
//    System.out.println("thread table:"+threadTable+",nss:"+nssTable);
        thr.start();

    }

    /**
     * locate a server according to its naplet id.
     * @param nid
     * @return
     */
    public String locate( NapletID nid )
    {
        return ( String ) nssTable.get( nid.toString() );
    }

    public void leave( NapletID nid )
    {}

    public void interrupt( NapletID nid )
    {
        Thread thr = threadTable.get( nid );
        if ( thr != null )
        {
            thr.interrupt();
        }
    }

    public void run()
    {}

    /**
     * Create a naplet thread
     */
    private class NapletThread
        implements Runnable
    {
        Naplet naplet;
        NapletContext context;
        NapletThread( Naplet naplet, NapletContext context )
        {
            this.naplet = naplet;
            this.context = context;
        }

        public void run()
        {
            naplet.init0( context );
            naplet.init();
            try
            {
                naplet.onStart();
            }
            catch ( InterruptedException ie )
            {
                naplet.onInterrupt();
            }
        }
    }

    /**
     * The <code>NapletThreadTable</code> object contains
     * a list of threads (thread group) associated with each
     * alien naplet. NapletServer manages the threads based on
     * this information.
     *
     * @version 0.0.1, 1/1/2001
     * @author C. Xu
     *
     * @see NapletServer
     */
    private class NapletThreadTable
    {
        private HashMap threadTable;
        protected NapletThreadTable()
        {
            threadTable = new HashMap();
        }

        protected void put( NapletID nid, Thread thr )
        {
            String key = nid.toString();
            threadTable.put( key, thr );
        }

        protected void remove( NapletID nid )
        {
            String key = nid.toString();
            threadTable.remove( key );
        }

        protected Thread get( NapletID nid )
        {
            String key = nid.toString();
            return ( Thread ) threadTable.get( key );
        }

        public String toString()
        {
            return threadTable.toString();
        }
    }
}

⌨️ 快捷键说明

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