Search in sources :

Example 6 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.

the class SocketTribesReceive method main.

public static void main(String[] args) throws Exception {
    int size = 43800;
    if (args.length > 0) {
        try {
            size = Integer.parseInt(args[0]);
        } catch (Exception e) {
        /* Ignore */
        }
    }
    XByteBuffer xbuf = new XByteBuffer(43800, true);
    try (ServerSocket srvSocket = new ServerSocket(9999)) {
        System.out.println("Listening on 9999");
        Socket socket = srvSocket.accept();
        socket.setReceiveBufferSize(size);
        InputStream in = socket.getInputStream();
        Thread t = new Thread() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        printStats(start, mb, count, df, total);
                    } catch (Exception x) {
                    /* Ignore */
                    }
                }
            }
        };
        t.setDaemon(true);
        t.start();
        while (true) {
            if (first) {
                first = false;
                start = System.currentTimeMillis();
            }
            int len = in.read(buf);
            if (len == -1) {
                printStats(start, mb, count, df, total);
                System.exit(1);
            }
            xbuf.append(buf, 0, len);
            if (bytes.intValue() != len) {
                bytes = new BigDecimal((double) len);
            }
            total = total.add(bytes);
            while (xbuf.countPackages(true) > 0) {
                xbuf.extractPackage(true);
                count++;
            }
            mb += ((double) len) / 1024 / 1024;
            if (((count) % 10000) == 0) {
                printStats(start, mb, count, df, total);
            }
        }
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) BigDecimal(java.math.BigDecimal)

Example 7 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.

the class NioSenderTest method getMessage.

public synchronized ChannelData getMessage(Member mbr) {
    String msg = "Thread-" + Thread.currentThread().getName() + " Message:" + inc();
    ChannelData data = new ChannelData(true);
    data.setMessage(new XByteBuffer(msg.getBytes(), false));
    data.setAddress(mbr);
    return data;
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData)

Example 8 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.

the class SocketNioReceive method main.

public static void main(String[] args) throws Exception {
    Member mbr = new MemberImpl("localhost", 9999, 0);
    ChannelData data = new ChannelData();
    data.setAddress(mbr);
    byte[] buf = new byte[8192 * 4];
    data.setMessage(new XByteBuffer(buf, false));
    buf = XByteBuffer.createDataPackage(data);
    len = buf.length;
    NioReceiver receiver = new NioReceiver();
    receiver.setPort(9999);
    receiver.setHost("localhost");
    MyList list = new MyList();
    receiver.setMessageListener(list);
    receiver.start();
    System.out.println("Listening on 9999");
    while (true) {
        try {
            synchronized (mutex) {
                mutex.wait(5000);
                if (start != 0) {
                    System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, messages " + count + " accepts " + accept + ", total " + mb + " MB.");
                }
            }
        } catch (Throwable x) {
            x.printStackTrace();
        }
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) NioReceiver(org.apache.catalina.tribes.transport.nio.NioReceiver) ChannelData(org.apache.catalina.tribes.io.ChannelData) MemberImpl(org.apache.catalina.tribes.membership.MemberImpl) Member(org.apache.catalina.tribes.Member)

Example 9 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.

the class StaticMembershipInterceptor method sendMemberMessage.

protected void sendMemberMessage(Member[] members, byte[] message) throws ChannelException {
    if (members == null || members.length == 0)
        return;
    ChannelData data = new ChannelData(true);
    data.setAddress(getLocalMember(false));
    data.setTimestamp(System.currentTimeMillis());
    data.setOptions(getOptionFlag());
    data.setMessage(new XByteBuffer(message, false));
    super.sendMessage(members, data, null);
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData)

Example 10 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat by apache.

the class McastServiceImpl method memberBroadcastsReceived.

private void memberBroadcastsReceived(final byte[] b) {
    if (log.isTraceEnabled())
        log.trace("Mcast received broadcasts.");
    XByteBuffer buffer = new XByteBuffer(b, true);
    if (buffer.countPackages(true) > 0) {
        int count = buffer.countPackages();
        final ChannelData[] data = new ChannelData[count];
        for (int i = 0; i < count; i++) {
            try {
                data[i] = buffer.extractPackage(true);
            } catch (IllegalStateException ise) {
                log.debug("Unable to decode message.", ise);
            }
        }
        Runnable t = new Runnable() {

            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                try {
                    Thread.currentThread().setName("Membership-MemberAdded.");
                    for (int i = 0; i < data.length; i++) {
                        try {
                            if (data[i] != null && !member.equals(data[i].getAddress())) {
                                msgservice.messageReceived(data[i]);
                            }
                        } catch (Throwable t) {
                            if (t instanceof ThreadDeath) {
                                throw (ThreadDeath) t;
                            }
                            if (t instanceof VirtualMachineError) {
                                throw (VirtualMachineError) t;
                            }
                            log.error(sm.getString("mcastServiceImpl.unableReceive.broadcastMessage"), t);
                        }
                    }
                } finally {
                    Thread.currentThread().setName(name);
                }
            }
        };
        executor.execute(t);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData)

Aggregations

XByteBuffer (org.apache.catalina.tribes.io.XByteBuffer)10 ChannelData (org.apache.catalina.tribes.io.ChannelData)9 BigDecimal (java.math.BigDecimal)3 Socket (java.net.Socket)3 ChannelException (org.apache.catalina.tribes.ChannelException)3 Member (org.apache.catalina.tribes.Member)3 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)3 DecimalFormat (java.text.DecimalFormat)2 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ConnectException (java.net.ConnectException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ByteMessage (org.apache.catalina.tribes.ByteMessage)1 UniqueId (org.apache.catalina.tribes.UniqueId)1