📄 socketcontroller.java~1~
字号:
*/
private byte[] buf = new byte[256];
public ControlChannel()
throws IOException
{
server = new DatagramSocket( UDPPort );
client = new DatagramSocket();
// set UDP timeout value
client.setSoTimeout( Global.UDPTIMEOUT );
}
/**
* Send message to destination
* @param msg
* @param addr
* @param port
* @throws IOException
*/
public void sendMessage( String msg, InetAddress addr, int port )
throws IOException
{
buf = new byte[msg.length()];
buf = msg.getBytes();
DatagramPacket packet = new DatagramPacket( buf, buf.length,
addr, port );
client.send( packet );
}
/**
* Receive a message from peer. Return a null when timout.
* The caller must retransmit the message when getting a null.
* @return
* @throws IOException
*/
public String receiveMessage()
throws IOException
{
buf = new byte[256];
DatagramPacket packet = new DatagramPacket( buf, buf.length );
try
{
client.receive( packet );
}
catch ( SocketTimeoutException se )
{
log( "get a timeout" );
return null;
}
String received = new String( packet.getData() );
return received;
}
/**
* Message handler for message exchanged by UDP channel.
*/
public void run()
{
while ( true )
{
try
{
byte[] inbuf = new byte[256];
DatagramPacket packet = new DatagramPacket( inbuf,
inbuf.length );
// keep reading message
server.receive( packet );
String in = new String( packet.getData(), 0,
packet.getLength() );
InetAddress clientaddr = packet.getAddress();
int clientport = packet.getPort();
log( "get a request in control channel:" + in );
if ( in == null )
{
// the other side has existed, so
// close this half socket
return;
}
StringTokenizer stringtokenizer
= new StringTokenizer( in, ":" );
String s1 = stringtokenizer.nextToken();
if ( s1.equals( "SuspendMsg" ) )
{
log( "get a Suspend request in Control Thread" );
// socketID
String sockID = stringtokenizer.nextToken();
NapletSocket nsocket
= ( NapletSocket ) socketTable.get( sockID );
if ( SocketController.GENKEY )
{ // key
BigInteger pubKey
= new BigInteger( stringtokenizer.nextToken() );
if ( !pubKey.equals( nsocket.getPublicKey() ) )
{
log( "***suspend key not match with socket:"
+ nsocket.getPublicKey() );
}
}
String ack = Global.ACKMSG;
if ( ( ( nsocket.getPriority()
== Global.HIGH_PRIORITY )
&& ( nsocket.getState()
== Global.SUSPEND_SENT ) )
|| ( nsocket.getState()
== Global.SUSPEND_ACK_RCD ) )
{
ack = Global.ACK_WAIT_MSG;
nsocket.setDualMigration( true );
log( "send an ack wait msg with state:" +
nsocket.getState() );
}
log( "msg:" + ack + ", before send ack,pri:" +
nsocket.getPriority() +
",state:" + nsocket.getState() );
nsocket.setState( Global.SUSPEND_SENT );
nsocket.setSuspendACKSent( true );
//sendMessage(ack, clientaddr, clientport);
byte[] buf = new byte[ack.length()];
buf = ack.getBytes();
packet = new DatagramPacket( buf, buf.length,
clientaddr, clientport );
server.send( packet );
if ( ack.equalsIgnoreCase( Global.ACKMSG ) )
{ // only do this when snd ack
nsocket.suspendRemotly();
}
}
else if ( s1.equals( "CloseMsg" ) )
{ // for gracefully close
log( "get a close request in ControlTh" );
// send back ackmsg
String ack = Global.ACKMSG;
byte[] buf = new byte[ack.length()];
buf = ack.getBytes();
packet = new DatagramPacket( buf, buf.length,
clientaddr, clientport );
server.send( packet );
String sockID = stringtokenizer.nextToken();
NapletSocket nsocket
= ( NapletSocket ) socketTable.get( sockID );
nsocket.closeRemotely();
}
else if ( s1.equalsIgnoreCase( "HalfResumeMsg" ) )
{
log( "get a haf resume msg" );
// send back ack msg
String ack = Global.ACKMSG;
byte[] buf = new byte[ack.length()];
buf = ack.getBytes();
packet = new DatagramPacket( buf, buf.length,
clientaddr, clientport );
server.send( packet );
String sockID = stringtokenizer.nextToken();
NapletSocket nsocket
= ( NapletSocket ) socketTable.get( sockID );
synchronized ( nsocket )
{
nsocket.setState( Global.HALF_RESUMED );
nsocket.setRemoteAddress( clientaddr );
nsocket.notifyAll();
}
}
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end of while true
} // end of run
} //end of Class ControlChannel
} // end of Class SocketController
/**
* A wrapper for Hashtable storing some control information for NapletSocket
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class SocketTable
{
private Hashtable socketTable = new Hashtable();
protected SocketTable()
{}
protected void put( String sid, Object socket )
{
socketTable.put( sid, socket );
}
protected void remove( String nid )
{
String key = nid.toString();
socketTable.remove( key );
}
protected Object get( String nid )
{
String key = nid.toString();
return socketTable.get( key );
}
protected Enumeration getKeys()
{
return socketTable.keys();
}
protected boolean containsKey( Object key )
{
return socketTable.containsKey( key );
}
public String toString()
{
return socketTable.toString();
}
public Hashtable getTable()
{
return socketTable;
}
} // end of Class SocketTable
class SampleAction
implements PrivilegedAction
{
/**
* <p> This Sample PrivilegedAction performs the following operations:
* <ul>
* <li> Access the System property, <i>java.home</i>
* <li> Access the System property, <i>user.home</i>
* <li> Access the file, <i>foo.txt</i>
* </ul>
*
* @return <code>null</code> in all cases.
*
* @exception SecurityException if the caller does not have permission
* to perform the operations listed above.
*/
public Object run()
{
/// Here I do some security check for demostration. A better way is to
/// setup serversocket/socket here and return a reference.
AccessController.checkPermission( new java.net.SocketPermission(
"localhost",
"connect" ) );
try
{
ServerSocket ss = new ServerSocket( 7011 );
}
catch ( java.net.BindException be )
{
}
catch ( IOException ex )
{
ex.printStackTrace();
}
System.out.println( "pass security check" );
return null;
}
} // end of class SampleAction
class SamplePrincipal
implements Principal, java.io.Serializable
{
/**
* @serial
*/
private String name;
/**
* Create a SamplePrincipal with a Sample username.
*
* <p>
*
* @param name the Sample username for this user.
*
* @exception NullPointerException if the <code>name</code>
* is <code>null</code>.
*/
public SamplePrincipal( String name )
{
if ( name == null )
{
throw new NullPointerException( "illegal null input" );
}
this.name = name;
}
/**
* Return the Sample username for this <code>SamplePrincipal</code>.
*
* <p>
*
* @return the Sample username for this <code>SamplePrincipal</code>
*/
public String getName()
{
return name;
}
/**
* Return a string representation of this <code>SamplePrincipal</code>.
*
* <p>
*
* @return a string representation of this <code>SamplePrincipal</code>.
*/
public String toString()
{
return ( "SamplePrincipal: " + name );
}
/**
* Compares the specified Object with this <code>SamplePrincipal</code>
* for equality. Returns true if the given object is also a
* <code>SamplePrincipal</code> and the two SamplePrincipals
* have the same username.
*
* <p>
*
* @param o Object to be compared for equality with this
* <code>SamplePrincipal</code>.
*
* @return true if the specified Object is equal equal to this
* <code>SamplePrincipal</code>.
*/
public boolean equals( Object o )
{
if ( o == null )
{
return false;
}
if ( this == o )
{
return true;
}
if ( ! ( o instanceof SamplePrincipal ) )
{
return false;
}
SamplePrincipal that = ( SamplePrincipal ) o;
if ( this.getName().equals( that.getName() ) )
{
return true;
}
return false;
}
/**
* Return a hash code for this <code>SamplePrincipal</code>.
*
* <p>
*
* @return a hash code for this <code>SamplePrincipal</code>.
*/
public int hashCode()
{
return name.hashCode();
}
} // end of class SamplePrincipal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -