📄 hellonaplet.java
字号:
/*
* @<#> HelloNaplet.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 hello;
/**
* The <code>HelloNaplet</code> class implements an example of naplets
* that travels around the network, carrying greetings from visited servers.
*
* @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.message.*;
import service.*;
public class HelloNaplet
extends Naplet
{
public HelloNaplet( String[] servers )
throws InvalidNapletException, InvalidItineraryException
{
this( null, servers, null );
}
public HelloNaplet( 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 HelloNaplet( 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 HelloNaplet( String name, String[] servers, NapletListener listener )
throws InvalidNapletException, InvalidItineraryException
{
super( name, listener );
try
{
// Create a protected state space to keep messages from
// servers along the path
setNapletState( new ProtectedNapletState() );
// The messages will be kept in a hashtable and the
// hashtable is stored in the naplet state space
// in the name of "message"
Hashtable messages = new Hashtable( servers.length );
getNapletState().set( "message", messages );
}
catch ( NapletStateAccessException nsae )
{
throw new InvalidNapletException( "Naplet state initilization failure" );
}
setItinerary( new ICItinerary( servers ) );
}
/**
* Entry point of a naplet at each server
*/
public void onStart()
throws InterruptedException
{
String serverName;
String msg = null;
// Display the naplet trace on arrival at each server
URN server = getNapletContext().getServerURN();
System.out.println( "Naplet " + this.getNapletID().toString()
+ " arrives at " + server.toString() );
// Get non-privileged service handler,
// according to a service-level agreement defined in service
InfCollection handler = null;
try
{
handler = ( InfCollection ) getNapletContext().getServiceHandler(
"serviceImpl.InfCollection" );
msg = handler.getMsg();
}
catch ( ServiceAccessException sae )
{
msg = "Failure in service access";
}
catch ( RemoteException re )
{
msg = "Failure in getting message";
}
try
{
Hashtable messages = ( Hashtable ) getNapletState().get( "message" );
messages.put( server.toString(), msg );
}
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
{
throw new NapletRuntimeException(
"Result report failure: No callback port" );
}
}
catch ( java.rmi.RemoteException re )
{
}
catch ( NapletStateAccessException nsae )
{
throw new NapletRuntimeException( nsae );
}
}
}
/**
* Example itineraries to be associated with the hello naplet
*/
private class ICItinerary
extends Itinerary
{
public ICItinerary( String[] servers )
throws InvalidItineraryException
{
Operable act = new ResultReport();
// seq(s0,s1,s2)
setRoute( new SeqPattern( servers, act ) );
/* par(s0, s1,s2)
setRoute( new ParPattern( servers, act ) );
*/
/* post-action
setRoute( new SingletonPattern(servers[0], act) );
*/
/* broadcast
ItineraryPattern[] ip = new ItineraryPattern[servers.length];
for (int i=0; i<servers.length; i++)
ip[i] = new SingletonPattern(servers[i], act);
setRoute( new ParPattern(ip) );
*/
/* par(seq(s2, s0),seq(s1, s0))
String[] path0 = { servers[1], servers[0] };
String[] path1 = { servers[2], servers[0] };
ItineraryPattern[] ip = new ItineraryPattern[2];
ip[0] = new SeqPattern( path0 );
ip[1] = new SeqPattern( path1 );
setRoute( new ParPattern( ip, act ) );
*/
/* par(seq(s2, s1, s0))
String[] path = { servers[2], servers[1], servers[0] };
ItineraryPattern[] ip = new ItineraryPattern[1];
ip[0] = new SeqPattern( path );
setRoute( new ParPattern( ip, act ) );
*/
/* seq(s1; par(s2, s3))
ItineraryPattern[] ip = new ItineraryPattern[2];
ip[0] = new SingletonPattern( servers[2] );
String[] s = { servers[1], servers[0] };
ip[1] = new ParPattern( s, act );
setRoute( new SeqPattern( ip, act ) );
*/
/* par(s1; seq(s2, s3))
ItineraryPattern[] ip = new ItineraryPattern[2];
ip[0] = new SingletonPattern( servers[0] );
String[] s = { servers[1], servers[2] };
ip[1] = new SeqPattern( s, act );
setRoute( new ParPattern( ip, act ) );
*/
System.out.println( "ICItinerary = " + getRoute().toString() );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -