📄 unbufferedionaplet.java~2~
字号:
/* * @<#> UnbufferedIONaplet.java version 0.0.1, 5/13/2003 * * 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) 2003 Wayne State University. All Rights Reserved. *//** * The <code>UnbufferedIONaplet</code> class implements an example of Naplets * that require priviledged services on visited servers by unbuffered * character stream. It functions as a comparison of the buffered IO * between naplets and servers. * * In this example, the naplet reads file "testData" from /tmp/ directory and * sends it to each visited server. After finishing that, it tries to read * the responce from servers. Corresponding services should be implemented * in the directory of "ServiceImpl" at naplet server side. * * @version 0.0.1, 1/1/2000 * @author C. Xu * */package unbufferedIO;import java.io.*;import java.rmi.*;import java.util.*;import java.lang.reflect.*;import naplet.*;import naplet.itinerary.*;import naplet.serviceChannel.*;public class UnbufferedIONaplet extends Naplet{ public UnbufferedIONaplet( String[] servers ) throws InvalidNapletException, InvalidItineraryException { this( null, servers, null ); } public UnbufferedIONaplet( 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 UnbufferedIONaplet( 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 UnbufferedIONaplet( 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 ( NoSuchFieldException nsfe ) { throw new InvalidNapletException(); } setItinerary( new ICItinerary( servers ) ); } /** * Entry point of a naplet at each server */ public void onStart() throws InterruptedException { String serverName; serverName = getNapletContext().getServerURN().getHostName(); System.out.println( "Naplet " + this.getNapletID().toString() + " is running at " + serverName ); ServiceChannel channel = null; try { channel = ( ServiceChannel ) getNapletContext().getServiceChannel( "serviceImpl.UnbufferedIOTestService" ); } catch ( NoSuchServiceException nse ) { System.out.println( "No such service" ); } catch ( IllegalAccessException iae ) { System.out.println( "Access to the service is denied" ); } try { if ( channel == null ) { System.out.println( "No such service" ); } else { // get the read and write ends at Naplet side of // service channel. NapletOutputStream outStream = channel.getNapletOutputStream(); NapletWriter writer = channel.getNapletWriter(); NapletInputStream inStream = channel.getNapletInputStream(); NapletReader reader = channel.getNapletReader(); char[] buf; // Read file /tmp/testData from disk and write it to // each server naplet visits try { int i = 0; final int len = 10000000; for ( ; i < len; i++ ) { writer.write( i ); } System.out.println( "\nNaplet writes " + i + " characters to unbufferedIOTestService" ); } catch ( IOException e ) { System.err.println( "Error: " + e.getMessage() ); } finally { writer.close(); outStream.close(); } // Read the responce from the server. try { int j = 0; int ch; while ( ( ch = reader.read() ) != -1 ) { j++; } System.out.println( "\nNaplet reads " + j + " characters from UnbufferedIOTestService" ); } catch ( Exception e ) { System.err.println( e.getMessage() ); } try { getItinerary().travel( this ); } catch ( UnableDispatchException ude ) { System.out.println( ude.getMessage() ); } catch ( RemoteException re ) { System.out.println( re.getMessage() ); } } } catch ( Exception e ) { System.err.println( e.getMessage() ); } } static void classFinalize() { System.out.println( "MyNaplet was finalized" ); } private class ICItinerary extends Itinerary { public ICItinerary( String[] servers ) throws InvalidItineraryException { // seq(s0,s1,s2) setRoute( new SeqPattern( servers ) ); System.out.println( "ICItinerary = " + getRoute().toString() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -