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

📄 rshnaplet.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:
/* * @<#> RshNaplet.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 rsh;/** * The <code>RshNaplet</code> class implements an example of naplets * that require priviledged services on visited servers. * In this example, the naplet ask for information of "uname -a" command * from each server. Corresponding services should be implemented in the * directory of "ServiceImpl" at naplet server side. * * @version 0.0.1, 1/1/2000 * @author C. Xu * */import java.io.*;import java.rmi.*;import java.util.*;import java.lang.reflect.*;import naplet.*;import naplet.itinerary.*;import naplet.serviceChannel.*;public class RshNaplet    extends Naplet{  static String CMD = "uname -a";  static int BufSize = 128;  public RshNaplet( String[] servers )      throws InvalidNapletException, InvalidItineraryException  {    this( null, servers, null );  }  public RshNaplet( String name, String[] servers )      throws InvalidNapletException, InvalidItineraryException  {    this( name, servers, null );  }  /**   * Constructs a naplet   * @param servers a list of servers to be visited   * @param listener listener of the naplet waiting for results   */  public RshNaplet( String[] servers, NapletListener listener )      throws InvalidNapletException, InvalidItineraryException  {    this( null, servers, listener );  }  /**   * Constructs a naplet, setting itinerary   *   * @param name Nick name of the naplet   * @param servers A list of servers to be visitted   * @param listener Listener of the naplet waiting for results   */  public RshNaplet( String name, String[] servers, NapletListener listener )      throws InvalidNapletException, InvalidItineraryException  {    super( name, listener );    try    {      Hashtable messages = new Hashtable( servers.length );      setNapletState( new ProtectedNapletState() );      getNapletState().set( "message", messages );    }    catch ( NapletStateAccessException nsae )    {      throw new InvalidNapletException(          "RshNaplet state initialization failiure" );    }    setItinerary( new ICItinerary( servers ) );  }  /**   * Entry point of a naplet at each server   */  public void onStart()      throws InterruptedException  {    String serverName;    String msg = null;    char[] buffer = new char[BufSize];    ServiceChannel channel = null;    serverName = getNapletContext().getServerURN().getHostName();    System.out.println( "Naplet " + this.getNapletID().toString()                        + " is running at " + serverName );    try    {      channel = ( ServiceChannel )          getNapletContext().getServiceChannel( "serviceImpl.ServiceShell" );    }    catch ( ServiceAccessException sae )    {      throw new NapletRuntimeException( sae );    }    try    {      NapletWriter out = channel.getNapletWriter();      char[] cmdChar = CMD.toCharArray();      try      {        for ( int i = 0; i < cmdChar.length; i++ )        {          out.write( cmdChar[i] );        }      }      finally      {        out.close();      }      NapletReader in = channel.getNapletReader();      int ch;      int i = 0;      while ( ( ch = in.read() ) != -1 )      {        if ( i < BufSize )        {          buffer[i++] = ( char ) ch;        }      }      System.out.println( new String( buffer, 0, i ) );    }    catch ( IOException ioe )    {      ioe.printStackTrace();    }    try    {      Hashtable messages = ( Hashtable ) getNapletState().get( "message" );      messages.put( serverName, new String( buffer, 0, BufSize - 1 ) );    }    catch ( NapletStateAccessException nsae )    {      throw new NapletRuntimeException( nsae );    }    try    {      getItinerary().travel( this );    }    catch ( NapletMigrateException nme )    {      throw new NapletRuntimeException( nme );    }  }  static void classFinalize()  {    System.out.println( "MyNaplet was finalized" );  }  /**   * The <code>ResultReport</code> class defines an operation to be performed   * at the end of an itinerary.   */  private class ResultReport      implements Operable  {    public void operate( Naplet nap )    {      try      {        if ( nap.getListener() != null )        {          Hashtable messages = ( Hashtable ) nap.getNapletState().get(              "message" );          nap.getListener().report( messages );        }        else        {}      }      catch ( java.rmi.RemoteException re )      {}      catch ( NapletStateAccessException nsae )      {        throw new NapletRuntimeException( nsae );      }    }  }  private class ICItinerary      extends Itinerary  {    public ICItinerary( String[] servers )        throws InvalidItineraryException    {      Operable act = new ResultReport();      // seq(s0,s1,s2)      setRoute( new SeqPattern( servers, act ) );      System.out.println( "ICItinerary = " + getRoute().toString() );    }  }}

⌨️ 快捷键说明

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