Search in sources :

Example 41 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class clonecli1 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5556");
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    while (true) {
        kvsimple kvMsg = kvsimple.recv(subscriber);
        if (kvMsg == null)
            break;
        clonecli1.kvMap.put(kvMsg.getKey(), kvMsg);
        System.out.println("receiving " + kvMsg);
        sequence.incrementAndGet();
    }
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 42 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class clonecli2 method run.

public void run() {
    Context ctx = ZMQ.context(1);
    Socket snapshot = ctx.socket(ZMQ.DEALER);
    snapshot.connect("tcp://localhost:5556");
    Socket subscriber = ctx.socket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5557");
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    // get state snapshot
    snapshot.send("ICANHAZ?".getBytes(ZMQ.CHARSET), 0);
    long sequence = 0;
    while (true) {
        kvsimple kvMsg = kvsimple.recv(snapshot);
        if (kvMsg == null)
            break;
        sequence = kvMsg.getSequence();
        if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
            System.out.println("Received snapshot = " + kvMsg.getSequence());
            // done
            break;
        }
        System.out.println("receiving " + kvMsg.getSequence());
        clonecli2.kvMap.put(kvMsg.getKey(), kvMsg);
    }
    // now apply pending updates, discard out-of-getSequence messages
    while (true) {
        kvsimple kvMsg = kvsimple.recv(subscriber);
        if (kvMsg == null)
            break;
        if (kvMsg.getSequence() > sequence) {
            sequence = kvMsg.getSequence();
            System.out.println("receiving " + sequence);
            clonecli2.kvMap.put(kvMsg.getKey(), kvMsg);
        }
    }
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 43 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class clonecli5 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket snapshot = ctx.createSocket(ZMQ.DEALER);
    snapshot.connect("tcp://localhost:5556");
    Socket subscriber = ctx.createSocket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5557");
    subscriber.subscribe(SUBTREE.getBytes(ZMQ.CHARSET));
    Socket publisher = ctx.createSocket(ZMQ.PUSH);
    publisher.connect("tcp://localhost:5558");
    Map<String, kvmsg> kvMap = new HashMap<String, kvmsg>();
    // get state snapshot
    snapshot.sendMore("ICANHAZ?");
    snapshot.send(SUBTREE);
    long sequence = 0;
    while (true) {
        kvmsg kvMsg = kvmsg.recv(snapshot);
        if (kvMsg == null)
            //  Interrupted
            break;
        sequence = kvMsg.getSequence();
        if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
            System.out.println("Received snapshot = " + kvMsg.getSequence());
            kvMsg.destroy();
            // done
            break;
        }
        System.out.println("receiving " + kvMsg.getSequence());
        kvMsg.store(kvMap);
    }
    Poller poller = ctx.createPoller(1);
    poller.register(subscriber);
    Random random = new Random();
    // now apply pending updates, discard out-of-getSequence messages
    long alarm = System.currentTimeMillis() + 5000;
    while (true) {
        int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
        if (rc == -1)
            //  Context has been shut down
            break;
        if (poller.pollin(0)) {
            kvmsg kvMsg = kvmsg.recv(subscriber);
            if (kvMsg == null)
                //  Interrupted
                break;
            if (kvMsg.getSequence() > sequence) {
                sequence = kvMsg.getSequence();
                System.out.println("receiving " + sequence);
                kvMsg.store(kvMap);
            } else
                kvMsg.destroy();
        }
        if (System.currentTimeMillis() >= alarm) {
            kvmsg kvMsg = new kvmsg(0);
            kvMsg.fmtKey("%s%d", SUBTREE, random.nextInt(10000));
            kvMsg.fmtBody("%d", random.nextInt(1000000));
            kvMsg.setProp("ttl", "%d", random.nextInt(30));
            kvMsg.send(publisher);
            kvMsg.destroy();
            alarm = System.currentTimeMillis() + 1000;
        }
    }
    ctx.destroy();
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 44 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class clonesrv1 method run.

public void run() {
    Context ctx = ZMQ.context(1);
    Socket publisher = ctx.socket(ZMQ.PUB);
    publisher.bind("tcp://*:5556");
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Random random = new Random();
    while (true) {
        long currentSequenceNumber = sequence.incrementAndGet();
        int key = random.nextInt(10000);
        int body = random.nextInt(1000000);
        ByteBuffer b = ByteBuffer.allocate(4);
        b.asIntBuffer().put(body);
        kvsimple kvMsg = new kvsimple(key + "", currentSequenceNumber, b.array());
        kvMsg.send(publisher);
        System.out.println("sending " + kvMsg);
    }
}
Also used : Context(org.zeromq.ZMQ.Context) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) Socket(org.zeromq.ZMQ.Socket)

Example 45 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class clonesrv3 method run.

public void run() {
    ZContext ctx = new ZContext();
    Socket snapshot = ctx.createSocket(ZMQ.ROUTER);
    snapshot.bind("tcp://*:5556");
    Socket publisher = ctx.createSocket(ZMQ.PUB);
    publisher.bind("tcp://*:5557");
    Socket collector = ctx.createSocket(ZMQ.PULL);
    collector.bind("tcp://*:5558");
    Poller poller = ctx.createPoller(2);
    poller.register(collector, Poller.POLLIN);
    poller.register(snapshot, Poller.POLLIN);
    long sequence = 0;
    while (!Thread.currentThread().isInterrupted()) {
        if (poller.poll(1000) < 0)
            //  Context has been shut down
            break;
        // apply state updates from main thread
        if (poller.pollin(0)) {
            kvsimple kvMsg = kvsimple.recv(collector);
            if (//  Interrupted
            kvMsg == null)
                break;
            kvMsg.setSequence(++sequence);
            kvMsg.send(publisher);
            clonesrv3.kvMap.put(kvMsg.getKey(), kvMsg);
            System.out.printf("I: publishing update %5d\n", sequence);
        }
        // execute state snapshot request
        if (poller.pollin(1)) {
            byte[] identity = snapshot.recv(0);
            if (identity == null)
                //  Interrupted
                break;
            String request = snapshot.recvStr();
            if (!request.equals("ICANHAZ?")) {
                System.out.println("E: bad request, aborting");
                break;
            }
            Iterator<Entry<String, kvsimple>> iter = kvMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<String, kvsimple> entry = iter.next();
                kvsimple msg = entry.getValue();
                System.out.println("Sending message " + entry.getValue().getSequence());
                this.sendMessage(msg, identity, snapshot);
            }
            // now send end message with getSequence number
            System.out.println("Sending state snapshot = " + sequence);
            snapshot.send(identity, ZMQ.SNDMORE);
            kvsimple message = new kvsimple("KTHXBAI", sequence, ZMQ.SUBSCRIPTION_ALL);
            message.send(snapshot);
        }
    }
    System.out.printf(" Interrupted\n%d messages handled\n", sequence);
    ctx.destroy();
}
Also used : Entry(java.util.Map.Entry) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

Socket (org.zeromq.ZMQ.Socket)84 Context (org.zeromq.ZMQ.Context)32 ZContext (org.zeromq.ZContext)30 Test (org.junit.Test)26 Poller (org.zeromq.ZMQ.Poller)20 ZMsg (org.zeromq.ZMsg)14 Random (java.util.Random)13 ZFrame (org.zeromq.ZFrame)13 PollItem (org.zeromq.ZMQ.PollItem)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 SelectableChannel (java.nio.channels.SelectableChannel)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Ignore (org.junit.Ignore)2 Actor (org.zeromq.ZActor.Actor)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1