publicclassCommonClient{privateSocketChannel clientSocket;privateByteBuffer recvBuffer;privateSelectionKey key;privateCallback callback;privateString msg;publicCommonClient(SocketChannel clientSocket,SelectionKey key){this.clientSocket = clientSocket;this.key = key;recvBuffer =ByteBuffer.allocate(8);try{this.clientSocket.configureBlocking(false);key.interestOps(SelectionKey.OP_WRITE);}catch(IOException e){}}publicvoidclose(){try{clientSocket.close();key.cancel();}catch(IOException e){};}// an rpc to notify client to send a numberpublicvoidsendMessage(String msg,Callback cback){this.callback = cback;try{try{recvBuffer.clear();recvBuffer.put(msg.getBytes());recvBuffer.flip();clientSocket.write(recvBuffer);key.interestOps(SelectionKey.OP_READ);}catch(IOException e){e.printStackTrace();}}catch(Exception e){}}// when key is writable, resume the fiber to continue// to write.publicvoidonWrite(){sendMessage("divident",newCallback(){@OverridepublicvoidonSucceed(int data){int a = data;sendMessage("divisor",newCallback(){@OverridepublicvoidonSucceed(int data){int b = data;sendMessage(String.valueOf(a / b),null);}});}});}publicvoidonRead(){int res =0;try{try{recvBuffer.clear();// read may fail even SelectionKey is readable// when read fails, the fiber should suspend, waiting for next// time the key is ready.int n = clientSocket.read(recvBuffer);while(n ==0){n = clientSocket.read(recvBuffer);}if(n ==-1){close();return;}System.out.println("received "+ n +" bytes from client");}catch(IOException e){e.printStackTrace();}recvBuffer.flip();res =getInt(recvBuffer);// when read ends, we are no longer interested in reading,// but in writing.key.interestOps(SelectionKey.OP_WRITE);}catch(Exception e){}this.callback.onSucceed(res);}publicintgetInt(ByteBuffer buf){int r =0;while(buf.hasRemaining()){r *=10;r += buf.get()-'0';}return r;}}