Grizzly Study Notes (2)

Source: Internet
Author: User

The client in the previous article is relatively simple. The following is a complex point: package org. guojje. grizzly;
 
Import java. io. IOException;
Import java.net. InetAddress;
Import java.net. InetSocketAddress;
Import java.net. UnknownHostException;
Import java. nio. ByteBuffer;
Import java. nio. channels. SelectionKey;
Import java. util. concurrent. CountDownLatch;
Import java. util. logging. Level;
 
Import com. sun. grizzly. CallbackHandler;
Import com. sun. grizzly. Context;
Import com. sun. grizzly. Controller;
Import com. sun. grizzly. ControllerStateListenerAdapter;
Import com. sun. grizzly. IOEvent;
Import com. sun. grizzly. TCPConnectorHandler;
Import com. sun. grizzly. TCPSelectorHandler;
Import com. sun. grizzly. Controller. Protocol;
Import com. sun. grizzly. util. WorkerThreadImpl;
 
Public class Client {
 
Public static void main (String [] args) throws UnknownHostException,
IOException {
 
Controller controller = new Controller ();
 
// It is true for client
TCPSelectorHandler tcpHandler = new TCPSelectorHandler (true );
Controller. setSelectorHandler (tcpHandler );
 
Final CountDownLatch latch = new CountDownLatch (1 );
Controller. addStateListener (new ControllerStateListenerAdapter (){
 
Public void onStarted (){
System. out. println ("on started ");
}
 
Public void onReady (){
System. out. println ("on ready ");
Latch. countDown ();
}
 
Public void onException (Throwable e ){
If (latch. getCount ()> 0 ){
Controller. logger (). log (Level. SEVERE,
"Exception during" + "starting the controller", e );
Latch. countDown ();
} Else {
Controller. logger (). log (Level. SEVERE,
"Exception during" + "controller processing", e );
}
}
});
// Controller. start (); controller. start () cannot be used to start controller.
// Because it will block the current thread
New WorkerThreadImpl ("client_work", controller). start ();
 
Try {
Latch. await ();
} Catch (InterruptedException e1 ){
// Do nothing
}
 
If (! Controller. isStarted ()){
Throw new IllegalStateException ("Controller is not started! ");
}
 
Final CountDownLatch waitLatch = new CountDownLatch (1 );
 
Final TCPConnectorHandler tch = (TCPConnectorHandler) controller
. AcquireConnectorHandler (Protocol. TCP );
Tch. connect (new InetSocketAddress (InetAddress. getLocalHost (), 1900 ),
New CallbackHandler <Context> (){
 
Public void onConnect (IOEvent <Context> ioEvent ){
Try {
SelectionKey k = ioEvent. attachment ()
. GetSelectionKey ();
Try {
Tch. finishConnect (k );
// Log ("finishConnect ");
} Catch (java. io. IOException ex ){
// IoExceptionHandler. handle (ex );
Return;
} Catch (Throwable ex ){
// Logger (). log (Level. SEVERE, "onConnect", ex );
Return;
}
// Register the Read event
IoEvent. attachment (). getSelectorHandler (). register (
K, SelectionKey. OP_READ );
 
} Finally {
If (waitLatch! = Null ){
WaitLatch. countDown ();
}
}
}
 
Public void onRead (IOEvent <Context> ioEvent ){
ByteBuffer bb = ByteBuffer. allocate (100 );
Try {
Tch. read (bb, false );
} Catch (IOException e ){
E. printStackTrace ();
}
Bb. flip ();
System. out. println (new String (bb. array (), 0, bb
. Remaining ()));
 
SelectionKey k = ioEvent. attachment (). getSelectionKey ();
// Re-register the read event. For the reason, refer to the onReadInterest/onWriteInterest method of TcpSelectorHandler.
IoEvent. attachment (). getSelectionKey (). interestOps (
K. interestOps () | SelectionKey. OP_READ );
}
 
Public void onWrite (IOEvent <Context> ioEvent ){
}
});
 
System. out. println ("current connection:" + tch. getUnderlyingChannel ());
 
Try {
WaitLatch. await ();
} Catch (Exception e ){
// Do nothing
}
 
ByteBuffer bb = ByteBuffer. wrap ("xxxx". getBytes ());
Tch. write (bb, true );
}
}
 
This article is from the "no thieves in the world" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.