Search in sources :

Example 1 with XByteBuffer

use of org.apache.catalina.tribes.io.XByteBuffer in project tomcat70 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);
            } catch (IOException x) {
                log.debug("Unable to decode message.", x);
            }
        }
        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("Unable to receive broadcast message.", 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) IOException(java.io.IOException)

Example 2 with XByteBuffer

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

the class TcpPingInterceptor method sendPingMessage.

protected void sendPingMessage(Member[] members) {
    if (members == null || members.length == 0)
        return;
    // generates a unique Id
    ChannelData data = new ChannelData(true);
    data.setAddress(getLocalMember(false));
    data.setTimestamp(System.currentTimeMillis());
    data.setOptions(getOptionFlag());
    data.setMessage(new XByteBuffer(TCP_PING_DATA, false));
    try {
        super.sendMessage(members, data, null);
    } catch (ChannelException x) {
        log.warn("Unable to send TCP ping.", x);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) ChannelData(org.apache.catalina.tribes.io.ChannelData) ChannelException(org.apache.catalina.tribes.ChannelException)

Example 3 with XByteBuffer

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

the class SocketNioSend method main.

public static void main(String[] args) throws Exception {
    Selector selector = Selector.open();
    Member mbr = new MemberImpl("localhost", 9999, 0);
    ChannelData data = new ChannelData();
    data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
    data.setAddress(mbr);
    byte[] buf = new byte[8192 * 4];
    data.setMessage(new XByteBuffer(buf, false));
    buf = XByteBuffer.createDataPackage(data);
    int len = buf.length;
    BigDecimal total = new BigDecimal((double) 0);
    BigDecimal bytes = new BigDecimal((double) len);
    NioSender sender = new NioSender();
    sender.setDestination(mbr);
    sender.setDirectBuffer(true);
    sender.setSelector(selector);
    sender.setTxBufSize(1024 * 1024);
    sender.connect();
    sender.setMessage(buf);
    System.out.println("Writing to 9999");
    long start = 0;
    double mb = 0;
    boolean first = true;
    int count = 0;
    DecimalFormat df = new DecimalFormat("##.00");
    while (count < 100000) {
        if (first) {
            first = false;
            start = System.currentTimeMillis();
        }
        sender.setMessage(buf);
        int selectedKeys = 0;
        try {
            selectedKeys = selector.select(0);
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
        if (selectedKeys == 0) {
            continue;
        }
        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        while (it.hasNext()) {
            SelectionKey sk = it.next();
            it.remove();
            try {
                int readyOps = sk.readyOps();
                sk.interestOps(sk.interestOps() & ~readyOps);
                if (sender.process(sk, false)) {
                    total = total.add(bytes);
                    sender.reset();
                    sender.setMessage(buf);
                    mb += ((double) len) / 1024 / 1024;
                    if (((++count) % 10000) == 0) {
                        long time = System.currentTimeMillis();
                        double seconds = ((double) (time - start)) / 1000;
                        System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total " + mb + " MB, total " + total + " bytes.");
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace();
                return;
            }
        }
        selector.selectedKeys().clear();
    }
    System.out.println("Complete, sleeping 15 seconds");
    Thread.sleep(15000);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) NioSender(org.apache.catalina.tribes.transport.nio.NioSender) MemberImpl(org.apache.catalina.tribes.membership.MemberImpl) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ChannelData(org.apache.catalina.tribes.io.ChannelData) Member(org.apache.catalina.tribes.Member) Selector(java.nio.channels.Selector)

Example 4 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 5 with XByteBuffer

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

the class SocketSend method main.

public static void main(String[] args) throws Exception {
    Member mbr = new MemberImpl("localhost", 9999, 0);
    ChannelData data = new ChannelData();
    data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
    data.setAddress(mbr);
    byte[] buf = new byte[8192 * 4];
    data.setMessage(new XByteBuffer(buf, false));
    buf = XByteBuffer.createDataPackage(data);
    int len = buf.length;
    System.out.println("Message size:" + len + " bytes");
    BigDecimal total = new BigDecimal((double) 0);
    BigDecimal bytes = new BigDecimal((double) len);
    try (Socket socket = new Socket("localhost", 9999)) {
        System.out.println("Writing to 9999");
        OutputStream out = socket.getOutputStream();
        long start = 0;
        double mb = 0;
        boolean first = true;
        int count = 0;
        DecimalFormat df = new DecimalFormat("##.00");
        while (count < 1000000) {
            if (first) {
                first = false;
                start = System.currentTimeMillis();
            }
            out.write(buf, 0, buf.length);
            mb += ((double) buf.length) / 1024 / 1024;
            total = total.add(bytes);
            if (((++count) % 10000) == 0) {
                long time = System.currentTimeMillis();
                double seconds = ((double) (time - start)) / 1000;
                System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds messages " + count + ", total " + mb + " MB, total " + total + " bytes.");
            }
        }
        out.flush();
        System.out.println("Complete, sleeping 5 seconds");
        Thread.sleep(5000);
    }
}
Also used : XByteBuffer(org.apache.catalina.tribes.io.XByteBuffer) MemberImpl(org.apache.catalina.tribes.membership.MemberImpl) OutputStream(java.io.OutputStream) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ChannelData(org.apache.catalina.tribes.io.ChannelData) Member(org.apache.catalina.tribes.Member) Socket(java.net.Socket)

Aggregations

XByteBuffer (org.apache.catalina.tribes.io.XByteBuffer)27 ChannelData (org.apache.catalina.tribes.io.ChannelData)23 ChannelException (org.apache.catalina.tribes.ChannelException)8 BigDecimal (java.math.BigDecimal)6 Socket (java.net.Socket)6 Member (org.apache.catalina.tribes.Member)6 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)6 DecimalFormat (java.text.DecimalFormat)4 Test (org.junit.Test)4 RemoteProcessException (org.apache.catalina.tribes.RemoteProcessException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ConnectException (java.net.ConnectException)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 ServerSocket (java.net.ServerSocket)2 SocketTimeoutException (java.net.SocketTimeoutException)2 SelectionKey (java.nio.channels.SelectionKey)2