📄 loopnaplet.java
字号:
/* * @<#> LoopNaplet.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 loop;/** * The <code>LoopNaplet</code> class implements an example of naplets * that travels around the network repetitively until cerntain condition * is satisfied. * * @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 LoopNaplet extends Naplet { /** * Constructs a naplet * @param servers a list of servers to be visited */ public LoopNaplet(String[] servers ) throws InvalidNapletException, InvalidItineraryException { this(null, servers); } /** * Constructs a repetitive naplet, setting itinerary * * @param name Nick name of the naplet * @param servers A list of servers to be visitted */ public LoopNaplet(String name, String[] servers) throws InvalidNapletException, InvalidItineraryException { super(name, null); setNapletState( new ProtectedNapletState() ); // Setting a repetition count, assuming the naplet loops // for certain number; // Naplet state cann't contain variables of primitive types. // Instead, their wrapper classes should be used. try { getNapletState().set("count", new Integer(5) ); } catch (NapletStateAccessException nsae) { throw new InvalidNapletException("LoopNaplet state initialization failure"); } setItinerary ( new ICItinerary( servers ) ); } /** * Entry point of a naplet at each server */ public void onStart() throws InterruptedException { String serverName; URN server = getNapletContext().getServerURN(); System.out.println( "Naplet " + this.getNapletID().toString() + " arrives at " + server.toString()); Thread.sleep(300); // Count the number of servers visited. try { Integer count = (Integer)getNapletState().get("count"); int c = count.intValue(); System.out.println("Loop Count = "+ c); getNapletState().set("count", new Integer( c-1 )); getItinerary().travel( this ); } catch (NapletStateAccessException nsfe) { throw new NapletRuntimeException(nsfe); } catch (NapletMigrateException nme) { throw new NapletRuntimeException(nme); } } static void classFinalize() { System.out.println("MyNaplet was finalized"); } /** * The <code>Guardian</code> class defines a function to be checked * as a pre-condition of visits. It implements a * <code>Checkable</code> interface. */ private class Guardian implements Checkable { public boolean check( Naplet nap ) { boolean going = false; try { Integer count = (Integer) getNapletState().get("count"); if ( count.intValue() > 0 ) going = true; } catch (NapletStateAccessException nsae) { throw new NapletRuntimeException( nsae ); } return going; } } /** * An example loop itinerary to be associated with the naplet, * which visits a number of servers repeatedly. */ private class ICItinerary extends Itinerary { public ICItinerary( String[] servers ) throws InvalidItineraryException { Checkable guard = new Guardian(); // Loop itinerary testing ItineraryPattern ip = new SeqPattern( servers ); setRoute( new Loop(guard, ip ) ); System.out.println("ICItinerary = " + getRoute().toString() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -