📄 bufferedionaplet.java
字号:
/* * @<#> BufferedIONaplet.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. */package bufferedIO;/** * The <code>BufferedIONaplet</code> class implements an example of Naplets * that require priviledged services on visited servers by buffered * character stream. It aims to test the efficiency of the buffered IO * between naplets and servers. * * In this example, the naplet reads file "/tmp/testData" 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.18, 2/7/2004 * @author Q. Li * @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 BufferedIONaplet extends Naplet{ public BufferedIONaplet( String[] servers ) throws InvalidNapletException, InvalidItineraryException { this( null, servers, null ); } public BufferedIONaplet( 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 BufferedIONaplet( 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 BufferedIONaplet( 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( "Naplet state initialization exception" ); } 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.BufferedIOTestService" ); } catch ( ServiceAccessException sae ) { throw new NapletRuntimeException( sae ); } try { // get the read and write ends at Naplet side of // service channel and wrap them in buffered read and write. NapletOutputStream outStream = channel.getNapletOutputStream(); BufferedNapletOutputStream bOutStream = new BufferedNapletOutputStream( outStream ); NapletWriter writer = channel.getNapletWriter(); BufferedNapletWriter bWriter = new BufferedNapletWriter( writer ); NapletInputStream inStream = channel.getNapletInputStream(); BufferedNapletInputStream bInStream = new BufferedNapletInputStream( inStream ); NapletReader reader = channel.getNapletReader(); BufferedNapletReader bReader = new BufferedNapletReader( reader ); // write 10000000 integers to each server naplet visits try { int i = 0; final int len = 10000000; for ( ; i < len; i++ ) { bWriter.write( i ); } System.out.println( "\nNaplet writes " + i + " characters to BufferedIOTestService" ); } catch ( IOException e ) { System.err.println( "Error: " + e.getMessage() ); } finally { bWriter.close(); bOutStream.close(); } // Read the responce from the server. try { int j = 0; int ch; while ( ( ch = bReader.read() ) != -1 ) { j++; } System.out.println( "\nNaplet reads " + j + " characters from BufferedIOTestService" ); } catch ( Exception e ) { System.err.println( e.getMessage() ); } try { getItinerary().travel( this ); } catch ( NapletMigrateException nme ) { System.out.println( nme.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 + -