📄 sockettalknaplet.java~
字号:
/*
* @<#> SocketTalkNaplet.java version 0.0.1, 11/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) 2000 Wayne State University. All Rights Reserved.
*/
package napletSocket1;
/**
* It defines two naplets that talk by napletsocket while migrating through network.
* One is fixed and the other is moving. A parallel itinery with two sequential
* itinery is formed by a list of machines as input parameters. One of the
* sequential itinery only contains one machine for the fixed agent and a
* NapletServerSocket will be set up in that host. The other forms a list of
* hosts that a mobile agent travels.
*
* @version 0.0.1, 11/2003
* @author Xiliang Zhong
*
*/
import java.io.*;
import java.rmi.*;
import java.util.*;
import java.lang.reflect.*;
import java.net.*;
import naplet.*;
import naplet.server.*;
import naplet.itinerary.*;
import naplet.message.*;
import naplet.nsocket.*;
import service.*;
public class SocketTalkNaplet extends Naplet
{
/**napletsocket will be migrated with naplet*/
NapletSocket napletSocket;
private transient boolean DEBUG = true;
public SocketTalkNaplet( String[] servers )
throws InvalidNapletException,
InvalidItineraryException
{
this( null, servers, null );
}
public SocketTalkNaplet( 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 SocketTalkNaplet( 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 SocketTalkNaplet( String name, String[] servers,
NapletListener listener )
throws InvalidNapletException, InvalidItineraryException
{
super( name, listener );
// set server name; Here the assumption is the
// 1st machine has the SocketServer
setServer( servers[0] );
setServerID( getNapletID().toString() + ".1" );
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;
String msg = null;
URN server;
server = getNapletContext().getServerURN();
System.out.println( "Naplet " + this.getNapletID().toString()
+ " arrives at " + server.toString() );
String rank = getNapletID().getVersion();
if ( rank.equals( ".1" ) )
{ // server
if ( napletSocket == null )
{ // first time start a server and accept nsocket
try
{
NapletServerSocket nss
= new NapletServerSocket( getNapletID() );
napletSocket = nss.accept();
}
catch ( NoSocketControllerException nsce )
{
System.out.println( "\nNo SocketController Exception:"
+ server + nsce.getMessage() );
}
catch ( IOException ioe )
{
ioe.printStackTrace();
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end of first time
try
{
PrintWriter out
= new PrintWriter( napletSocket.getOutputStream(), true );
BufferedReader in = new BufferedReader(
new InputStreamReader( napletSocket.getInputStream() ) );
int t = 0;
while ( true )
{ //won't stop when error!!! buffer nothing when read!!
String request = in.readLine();
String response = "echo :" + request;
System.out.println( "get a message:" + request );
out.println( response );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end of server
else
{ // all others are client
try
{
// if first time, setup socket with the server
if ( napletSocket == null )
{
// wait for some time in case it takes more
// time for server to setup ServerSocket
Thread.sleep( 3000 );
// first time for open napletsocket
NapletID mynid = getNapletID();
NapletID destnid = ( NapletID ) mynid.clone();
destnid.setVersion( ".1" );
napletSocket = new NapletSocket( mynid, destnid );
} // end of first time
PrintWriter out
= new PrintWriter( napletSocket.getOutputStream(), true );
BufferedReader in = new BufferedReader(
new InputStreamReader( napletSocket.getInputStream() ) );
String request = null, response = null;
request = "request from:" +
InetAddress.getLocalHost().getHostName();
out.println( request );
response = in.readLine();
System.out.println( "**response from server is:" + response );
}
catch( NoSocketControllerException nsce )
{
System.out.println( "\nNo SocketController Exception: "
+ server + nsce.getMessage() );
}
catch ( IOException ioe )
{
ioe.printStackTrace();
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end of client
try
{
getItinerary().travel( this );
}
catch ( UnableDispatchException ude )
{
System.out.println( ude.getMessage() );
}
catch ( RemoteException re )
{
System.out.println( re.getMessage() );
}
}
private void log( String info )
{
if ( DEBUG )
{
System.out.println( "SocketTalkNaplet:" + info );
}
}
static void classFinalize()
{
System.out.println( "MyNaplet was finalized" );
}
private class ICItinerary extends Itinerary
{
public ICItinerary( String[] servers )
throws InvalidItineraryException
{
ItineraryPattern[] ip = new ItineraryPattern[2];
int first = 1;
String[] dest = new String[first];
for ( int i = 0; i < 1; i++ )
{
dest[i] = new String( servers[i] );
}
String dest2[] = new String[servers.length - first];
for ( int i = 0; i < servers.length - first; i++ )
{
dest2[i] = new String( servers[first + i] );
}
ip[0] = new SeqPattern( dest );
ip[1] = new SeqPattern( dest2 );
setRoute( new ParPattern( ip ) );
System.out.println( "ICItinerary = " + getRoute().toString() );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -