Search in sources :

Example 61 with Socket

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

the class syncsub method main.

public static void main(String[] args) {
    Context context = ZMQ.context(1);
    //  First, connect our subscriber socket
    Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://localhost:5561");
    subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
    //  Second, synchronize with publisher
    Socket syncclient = context.socket(ZMQ.REQ);
    syncclient.connect("tcp://localhost:5562");
    //  - send a synchronization request
    syncclient.send(ZMQ.MESSAGE_SEPARATOR, 0);
    //  - wait for synchronization reply
    syncclient.recv(0);
    //  Third, get our updates and report how many we got
    int update_nbr = 0;
    while (true) {
        String string = subscriber.recvStr(0);
        if (string.equals("END")) {
            break;
        }
        update_nbr++;
    }
    System.out.println("Received " + update_nbr + " updates.");
    subscriber.close();
    syncclient.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 62 with Socket

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

the class flserver2 method main.

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.printf("I: syntax: flserver2 <endpoint>\n");
        System.exit(0);
    }
    ZContext ctx = new ZContext();
    Socket server = ctx.createSocket(ZMQ.REP);
    server.bind(args[0]);
    System.out.printf("I: echo service is ready at %s\n", args[0]);
    while (true) {
        ZMsg request = ZMsg.recvMsg(server);
        if (request == null)
            //  Interrupted
            break;
        //  Fail nastily if run against wrong client
        assert (request.size() == 2);
        ZFrame identity = request.pop();
        request.destroy();
        ZMsg reply = new ZMsg();
        reply.add(identity);
        reply.add("OK");
        reply.send(server);
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf("W: interrupted\n");
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 63 with Socket

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

the class flserver3 method main.

public static void main(String[] args) {
    boolean verbose = (args.length > 0 && args[0].equals("-v"));
    ZContext ctx = new ZContext();
    //  Prepare server socket with predictable identity
    String bindEndpoint = "tcp://*:5555";
    String connectEndpoint = "tcp://localhost:5555";
    Socket server = ctx.createSocket(ZMQ.ROUTER);
    server.setIdentity(connectEndpoint.getBytes(ZMQ.CHARSET));
    server.bind(bindEndpoint);
    System.out.printf("I: service is ready at %s\n", bindEndpoint);
    while (!Thread.currentThread().isInterrupted()) {
        ZMsg request = ZMsg.recvMsg(server);
        if (verbose && request != null)
            request.dump(System.out);
        if (request == null)
            //  Interrupted
            break;
        //  Frame 0: identity of client
        //  Frame 1: PING, or client control frame
        //  Frame 2: request body
        ZFrame identity = request.pop();
        ZFrame control = request.pop();
        ZMsg reply = new ZMsg();
        if (control.equals(new ZFrame("PING")))
            reply.add("PONG");
        else {
            reply.add(control);
            reply.add("OK");
        }
        request.destroy();
        reply.push(identity);
        if (verbose && reply != null)
            reply.dump(System.out);
        reply.send(server);
    }
    if (Thread.currentThread().isInterrupted())
        System.out.printf("W: interrupted\n");
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket)

Example 64 with Socket

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

the class identity method main.

public static void main(String[] args) throws InterruptedException {
    Context context = ZMQ.context(1);
    Socket sink = context.socket(ZMQ.ROUTER);
    sink.bind("inproc://example");
    //  First allow 0MQ to set the identity, [00] + random 4byte
    Socket anonymous = context.socket(ZMQ.REQ);
    anonymous.connect("inproc://example");
    anonymous.send("ROUTER uses a generated UUID", 0);
    ZHelper.dump(sink);
    //  Then set the identity ourself
    Socket identified = context.socket(ZMQ.REQ);
    identified.setIdentity("Hello".getBytes(ZMQ.CHARSET));
    identified.connect("inproc://example");
    identified.send("ROUTER socket uses REQ's socket identity", 0);
    ZHelper.dump(sink);
    sink.close();
    anonymous.close();
    identified.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 65 with Socket

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

the class lbbroker method main.

/**
     * This is the main task. It starts the clients and workers, and then
     * routes requests between the two layers. Workers signal READY when
     * they start; after that we treat them as ready when they reply with
     * a response back to a client. The load-balancing data structure is
     * just a queue of next available workers.
     */
public static void main(String[] args) {
    Context context = ZMQ.context(1);
    //  Prepare our context and sockets
    Socket frontend = context.socket(ZMQ.ROUTER);
    Socket backend = context.socket(ZMQ.ROUTER);
    frontend.bind("ipc://frontend.ipc");
    backend.bind("ipc://backend.ipc");
    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) new ClientTask().start();
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) new WorkerTask().start();
    //  Here is the main loop for the least-recently-used queue. It has two
    //  sockets; a frontend for clients and a backend for workers. It polls
    //  the backend in all cases, and polls the frontend only when there are
    //  one or more workers ready. This is a neat way to use 0MQ's own queues
    //  to hold messages we're not ready to process yet. When we get a client
    //  reply, we pop the next available worker, and send the request to it,
    //  including the originating client identity. When a worker replies, we
    //  re-queue that worker, and we forward the reply to the original client,
    //  using the reply envelope.
    //  Queue of available workers
    Queue<String> workerQueue = new LinkedList<String>();
    while (!Thread.currentThread().isInterrupted()) {
        //  Initialize poll set
        Poller items = context.poller(2);
        //  Always poll for worker activity on backend
        items.register(backend, Poller.POLLIN);
        //  Poll front-end only if we have available workers
        if (workerQueue.size() > 0)
            items.register(frontend, Poller.POLLIN);
        if (items.poll() < 0)
            //  Interrupted
            break;
        //  Handle worker activity on backend
        if (items.pollin(0)) {
            //  Queue worker address for LRU routing
            workerQueue.add(backend.recvStr());
            //  Second frame is empty
            String empty = backend.recvStr();
            assert (empty.length() == 0);
            //  Third frame is READY or else a client reply address
            String clientAddr = backend.recvStr();
            //  If client reply, send rest back to frontend
            if (!clientAddr.equals("READY")) {
                empty = backend.recvStr();
                assert (empty.length() == 0);
                String reply = backend.recvStr();
                frontend.sendMore(clientAddr);
                frontend.sendMore("");
                frontend.send(reply);
                if (--clientNbr == 0)
                    break;
            }
        }
        if (items.pollin(1)) {
            //  Now get next client request, route to LRU worker
            //  Client request is [address][empty][request]
            String clientAddr = frontend.recvStr();
            String empty = frontend.recvStr();
            assert (empty.length() == 0);
            String request = frontend.recvStr();
            String workerAddr = workerQueue.poll();
            backend.sendMore(workerAddr);
            backend.sendMore("");
            backend.sendMore(clientAddr);
            backend.sendMore("");
            backend.send(request);
        }
    }
    frontend.close();
    backend.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) LinkedList(java.util.LinkedList) 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