FileDocCategorySizeDatePackage
TestNioSender.javaAPI DocApache Tomcat 6.0.144282Fri Jul 20 04:20:32 BST 2007org.apache.catalina.tribes.test

TestNioSender

public class TestNioSender extends Object

Title:

Description:

Company:

author
not attributable
version
1.0

Fields Summary
private Selector
selector
private int
counter
org.apache.catalina.tribes.membership.MemberImpl
mbr
private static int
testOptions
Constructors Summary
public TestNioSender()

       
        
    
Methods Summary
public synchronized org.apache.catalina.tribes.io.ChannelDatagetMessage(org.apache.catalina.tribes.Member mbr)

        String msg = new String("Thread-"+Thread.currentThread().getName()+" Message:"+inc());
        ChannelData data = new ChannelData(true);
        data.setMessage(new XByteBuffer(msg.getBytes(),false));
        data.setAddress(mbr);
        
        return data;
    
public synchronized intinc()

        return ++counter;
    
public voidinit()

        selector = Selector.open();
        mbr = new MemberImpl("localhost",4444,0);
        NioSender sender = new NioSender();
        sender.setDestination(mbr);
        sender.setDirectBuffer(true);
        sender.setSelector(selector);
        sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr)));
        sender.connect();
    
public static voidmain(java.lang.String[] args)

        TestNioSender sender = new TestNioSender();
        sender.init();
        sender.run();
    
public voidrun()

        while (true) {

            int selectedKeys = 0;
            try {
                selectedKeys = selector.select(100);
                //               if ( selectedKeys == 0 ) {
                //                   System.out.println("No registered interests. Sleeping for a second.");
                //                   Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }

            if (selectedKeys == 0) {
                continue;
            }

            Iterator it = selector.selectedKeys().iterator();
            while (it.hasNext()) {
                SelectionKey sk = (SelectionKey) it.next();
                it.remove();
                try {
                    int readyOps = sk.readyOps();
                    sk.interestOps(sk.interestOps() & ~readyOps);
                    NioSender sender = (NioSender) sk.attachment();
                    if ( sender.process(sk, (testOptions&Channel.SEND_OPTIONS_USE_ACK)==Channel.SEND_OPTIONS_USE_ACK) ) {
                        System.out.println("Message completed for handler:"+sender);
                        Thread.currentThread().sleep(2000);
                        sender.reset();
                        sender.setMessage(XByteBuffer.createDataPackage(getMessage(mbr)));
                    }
                    

                } catch (Throwable t) {
                    t.printStackTrace();
                    return;
                }
            }
        }