📄 socketcontroller.java~2~
字号:
{ continue; } BufferedReader bufferedreader1 = new BufferedReader( new InputStreamReader( socket1.getInputStream() ) ); BufferedWriter bufferedwriter1 = new BufferedWriter( new OutputStreamWriter( socket1.getOutputStream() ) ); while ( !bStop ) { // keep reading request, only part of message // coming from TCP channel String in = bufferedreader1.readLine(); log( "from addr:" + socket1.getInetAddress() + ",get a request:" + in ); if ( in == null ) { // the other side has exited, so close this // half socket return; } StringTokenizer stringtokenizer = new StringTokenizer( in, ":" ); String s1 = stringtokenizer.nextToken(); if ( s1.equals( "ConnectMsg" ) ) { // ATTN: there is a : in NapletID String napletID = stringtokenizer.nextToken() + ":" + stringtokenizer.nextToken(); NapletServerSocket nss = ( NapletServerSocket ) nidServerTable.get( napletID ); if ( nss == null ) { // no server is available so exit with // an error message. System.out.println( "atten(server not up:::::wrong info" + nidServerTable + ", for nid:" + napletID ); System.exit( -1 ); } String pri = stringtokenizer.nextToken(); int intpri = Global.LOW_PRIORITY; if ( pri.equalsIgnoreCase( Integer.toString( Global. LOW_PRIORITY ) ) ) { //if client is low, server is high intpri = Global.HIGH_PRIORITY; } // see if should be persistent String persistent = stringtokenizer.nextToken(); boolean ifpersist = false; if ( ( persistent != null ) && ( persistent.equalsIgnoreCase( "true" ) ) ) { //nss.setSocket(socket1,true); ifpersist = true; } String ack = Global.ACKMSG; // use diffie-hellman key exchange if ( SocketController.GENKEY ) { BigInteger seed = new BigInteger( stringtokenizer. nextToken() ); BigInteger y = new BigInteger( Global.KEYSIZE, new Random() ); BigInteger pubKey = seed.modPow( y, Global.N ); log( "@@@pubkey:" + pubKey ); BigInteger tosend = Global.G.modPow( y, Global.N ); ack += tosend; nss.setSocket( socket1, ifpersist, intpri, pubKey ); } else { nss.setSocket( socket1, ifpersist, intpri ); } bufferedwriter1.write( ack ); bufferedwriter1.newLine(); bufferedwriter1.flush(); return; } else if ( s1.equals( "ResumeMsg" ) ) { // socket id String sockID = stringtokenizer.nextToken(); NapletSocket nsocket = ( NapletSocket ) socketTable.get( sockID ); if ( nsocket == null ) { log( "///nsocket is null:" + socketTable + ",and SID:" + sockID ); } // should authenticate with key match if ( SocketController.GENKEY ) { BigInteger pubKey = new BigInteger( stringtokenizer.nextToken() ); if ( !pubKey.equals( nsocket.getPublicKey() ) ) { log( "***resum key not match with socket:" + nsocket.getPublicKey() ); } } if ( nsocket.isSuspendAfterACK() ) { // if suspend after ack, it means the // peer has been approved to // migrate. so this message is to resume // the connection because // it has finished migration. send back // a stop_resume message // since the connection will be suspended next. bufferedwriter1.write( Global.ACK_STOP_RESUME ); } else { bufferedwriter1.write( Global.ACKMSG ); } bufferedwriter1.newLine(); bufferedwriter1.flush(); nsocket.resume( socket1 ); return; } } // end of while(true) readline bufferedreader1.close(); bufferedreader1 = null; bufferedwriter1.close(); bufferedwriter1 = null; socket1.close(); socket1 = null; } catch ( Exception exception1 ) { exception1.printStackTrace(); } } } // end of run /** * Stop control socket thread. */ void stopControlSocketThread() { bStop = true; try { controlServer.close(); } catch ( Exception exception ) { exception.printStackTrace(); } } private static void log( String info ) { if ( debug ) { System.out.println( "SocketController:" + info ); } } public class ControlChannel implements Runnable { /** * Client side of the message channel. Used to send message. */ private DatagramSocket client; /** * Server side of the message channel. Used to receive message. */ private DatagramSocket server; /** * The received message */ 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 SocketTableclass 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 SampleActionclass 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 + -