Search in sources :

Example 1 with TCPBroadcaster

use of org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster in project jackrabbit-oak by apache.

the class BroadcastTest method listen.

private static void listen() throws InterruptedException {
    String config = "key 123";
    ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    ca.setContext(lc);
    PatternLayout pl = new PatternLayout();
    pl.setPattern("%msg%n");
    pl.setContext(lc);
    pl.start();
    ca.setLayout(pl);
    ca.start();
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(TCPBroadcaster.class);
    logger.addAppender(ca);
    logger.setLevel(Level.DEBUG);
    TCPBroadcaster receiver = new TCPBroadcaster(config);
    receiver.addListener(new Broadcaster.Listener() {

        @Override
        public void receive(ByteBuffer buff) {
            int end = buff.position();
            StringBuilder sb = new StringBuilder();
            while (buff.remaining() > 0) {
                char c = (char) buff.get();
                if (c >= ' ' && c < 128) {
                    sb.append(c);
                } else if (c <= 9) {
                    sb.append((char) ('0' + c));
                } else {
                    sb.append('.');
                }
            }
            String dateTime = new Timestamp(System.currentTimeMillis()).toString().substring(0, 19);
            System.out.println(dateTime + " Received " + sb);
            buff.position(end);
        }
    });
    Random r = new Random();
    int x = r.nextInt();
    System.out.println("Sending " + x);
    for (int i = 0; i < 10; i++) {
        Thread.sleep(10);
        ByteBuffer buff = ByteBuffer.allocate(1024);
        buff.putInt(0);
        buff.putInt(x);
        buff.put(new byte[100]);
        buff.flip();
        receiver.send(buff);
        if (!receiver.isRunning()) {
            System.out.println("Did not start or already stopped");
            break;
        }
    }
    Thread.sleep(Integer.MAX_VALUE);
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) TCPBroadcaster(org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster) PatternLayout(ch.qos.logback.classic.PatternLayout) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) ByteBuffer(java.nio.ByteBuffer) Timestamp(java.sql.Timestamp) Random(java.util.Random) Broadcaster(org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.Broadcaster) TCPBroadcaster(org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster)

Example 2 with TCPBroadcaster

use of org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster in project jackrabbit-oak by apache.

the class PersistentCache method initBroadcast.

private void initBroadcast(String broadcast) {
    if (broadcast == null) {
        return;
    }
    if (broadcast.equals("disabled")) {
        return;
    } else if (broadcast.equals("inMemory")) {
        broadcaster = InMemoryBroadcaster.INSTANCE;
    } else if (broadcast.startsWith("udp:")) {
        String config = broadcast.substring("udp:".length(), broadcast.length());
        broadcaster = new UDPBroadcaster(config);
    } else if (broadcast.startsWith("tcp:")) {
        String config = broadcast.substring("tcp:".length(), broadcast.length());
        broadcaster = new TCPBroadcaster(config);
    } else {
        throw new IllegalArgumentException("Unknown broadcaster type " + broadcast);
    }
    broadcaster.addListener(this);
}
Also used : UDPBroadcaster(org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.UDPBroadcaster) TCPBroadcaster(org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster)

Aggregations

TCPBroadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 PatternLayout (ch.qos.logback.classic.PatternLayout)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 ByteBuffer (java.nio.ByteBuffer)1 Timestamp (java.sql.Timestamp)1 Random (java.util.Random)1 Broadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.Broadcaster)1 UDPBroadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.UDPBroadcaster)1