Search in sources :

Example 51 with Socket

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

the class peering3 method main.

//  The main task begins by setting-up all its sockets. The local frontend
//  talks to clients, and our local backend talks to workers. The cloud
//  frontend talks to peer brokers as if they were clients, and the cloud
//  backend talks to peer brokers as if they were workers. The state
//  backend publishes regular state messages, and the state frontend
//  subscribes to all state backends to collect these messages. Finally,
//  we use a PULL monitor socket to collect printable messages from tasks:
public static void main(String[] argv) {
    //
    if (argv.length < 1) {
        System.out.println("syntax: peering3 me {you}");
        System.exit(-1);
    }
    self = argv[0];
    System.out.printf("I: preparing broker at %s\n", self);
    Random rand = new Random(System.nanoTime());
    ZContext ctx = new ZContext();
    //  Prepare local frontend and backend
    Socket localfe = ctx.createSocket(ZMQ.ROUTER);
    localfe.bind(String.format("ipc://%s-localfe.ipc", self));
    Socket localbe = ctx.createSocket(ZMQ.ROUTER);
    localbe.bind(String.format("ipc://%s-localbe.ipc", self));
    //  Bind cloud frontend to endpoint
    Socket cloudfe = ctx.createSocket(ZMQ.ROUTER);
    cloudfe.setIdentity(self.getBytes(ZMQ.CHARSET));
    cloudfe.bind(String.format("ipc://%s-cloud.ipc", self));
    //  Connect cloud backend to all peers
    Socket cloudbe = ctx.createSocket(ZMQ.ROUTER);
    cloudbe.setIdentity(self.getBytes(ZMQ.CHARSET));
    int argn;
    for (argn = 1; argn < argv.length; argn++) {
        String peer = argv[argn];
        System.out.printf("I: connecting to cloud forintend at '%s'\n", peer);
        cloudbe.connect(String.format("ipc://%s-cloud.ipc", peer));
    }
    //  Bind state backend to endpoint
    Socket statebe = ctx.createSocket(ZMQ.PUB);
    statebe.bind(String.format("ipc://%s-state.ipc", self));
    //  Connect statefe to all peers
    Socket statefe = ctx.createSocket(ZMQ.SUB);
    statefe.subscribe(ZMQ.SUBSCRIPTION_ALL);
    for (argn = 1; argn < argv.length; argn++) {
        String peer = argv[argn];
        System.out.printf("I: connecting to state backend at '%s'\n", peer);
        statefe.connect(String.format("ipc://%s-state.ipc", peer));
    }
    //  Prepare monitor socket
    Socket monitor = ctx.createSocket(ZMQ.PULL);
    monitor.bind(String.format("ipc://%s-monitor.ipc", self));
    //  Start local workers
    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++) new worker_task().start();
    //  Start local clients
    int client_nbr;
    for (client_nbr = 0; client_nbr < NBR_CLIENTS; client_nbr++) new client_task().start();
    //  Queue of available workers
    int localCapacity = 0;
    int cloudCapacity = 0;
    ArrayList<ZFrame> workers = new ArrayList<ZFrame>();
    //  The main loop has two parts. First we poll workers and our two service
    //  sockets (statefe and monitor), in any case. If we have no ready workers,
    //  there's no point in looking at incoming requests. These can remain on
    //  their internal 0MQ queues:
    Poller primary = ctx.createPoller(4);
    primary.register(localbe, Poller.POLLIN);
    primary.register(cloudbe, Poller.POLLIN);
    primary.register(statefe, Poller.POLLIN);
    primary.register(monitor, Poller.POLLIN);
    Poller secondary = ctx.createPoller(2);
    secondary.register(localfe, Poller.POLLIN);
    secondary.register(cloudfe, Poller.POLLIN);
    while (true) {
        //  First, route any waiting replies from workers
        //  If we have no workers anyhow, wait indefinitely
        int rc = primary.poll(localCapacity > 0 ? 1000 : -1);
        if (rc == -1)
            //  Interrupted
            break;
        //  Track if capacity changes during this iteration
        int previous = localCapacity;
        //  Handle reply from local worker
        ZMsg msg = null;
        if (primary.pollin(0)) {
            msg = ZMsg.recvMsg(localbe);
            if (msg == null)
                //  Interrupted
                break;
            ZFrame address = msg.unwrap();
            workers.add(address);
            localCapacity++;
            //  If it's READY, don't route the message any further
            ZFrame frame = msg.getFirst();
            if (new String(frame.getData(), ZMQ.CHARSET).equals(WORKER_READY)) {
                msg.destroy();
                msg = null;
            }
        } else //  Or handle reply from peer broker
        if (primary.pollin(1)) {
            msg = ZMsg.recvMsg(cloudbe);
            if (msg == null)
                //  Interrupted
                break;
            //  We don't use peer broker address for anything
            ZFrame address = msg.unwrap();
            address.destroy();
        }
        //  Route reply to cloud if it's addressed to a broker
        for (argn = 1; msg != null && argn < argv.length; argn++) {
            byte[] data = msg.getFirst().getData();
            if (argv[argn].equals(new String(data, ZMQ.CHARSET))) {
                msg.send(cloudfe);
                msg = null;
            }
        }
        //  Route reply to client if we still need to
        if (msg != null)
            msg.send(localfe);
        if (primary.pollin(2)) {
            String peer = statefe.recvStr();
            String status = statefe.recvStr();
            cloudCapacity = Integer.parseInt(status);
        }
        if (primary.pollin(3)) {
            String status = monitor.recvStr();
            System.out.println(status);
        }
        while (localCapacity + cloudCapacity > 0) {
            rc = secondary.poll(0);
            assert (rc >= 0);
            if (secondary.pollin(0)) {
                msg = ZMsg.recvMsg(localfe);
            } else if (localCapacity > 0 && secondary.pollin(1)) {
                msg = ZMsg.recvMsg(cloudfe);
            } else
                //  No work, go back to backends
                break;
            if (localCapacity > 0) {
                ZFrame frame = workers.remove(0);
                msg.wrap(frame);
                msg.send(localbe);
                localCapacity--;
            } else {
                //  Route to random broker peer
                int random_peer = rand.nextInt(argv.length - 1) + 1;
                msg.push(argv[random_peer]);
                msg.send(cloudbe);
            }
        }
        if (localCapacity != previous) {
            //  We stick our own address onto the envelope
            statebe.sendMore(self);
            //  Broadcast new capacity
            statebe.send(String.format("%d", localCapacity), 0);
        }
    }
    //  When we're done, clean up properly
    while (workers.size() > 0) {
        ZFrame frame = workers.remove(0);
        frame.destroy();
    }
    ctx.destroy();
}
Also used : ArrayList(java.util.ArrayList) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) ZFrame(org.zeromq.ZFrame) Random(java.util.Random) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 52 with Socket

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

the class ppworker method worker_socket.

//  Helper function that returns a new configured socket
//  connected to the Paranoid Pirate queue
private static Socket worker_socket(ZContext ctx) {
    Socket worker = ctx.createSocket(ZMQ.DEALER);
    worker.connect("tcp://localhost:5556");
    //  Tell queue we're ready for work
    System.out.println("I: worker ready\n");
    ZFrame frame = new ZFrame(PPP_READY);
    frame.send(worker, 0);
    return worker;
}
Also used : ZFrame(org.zeromq.ZFrame) Socket(org.zeromq.ZMQ.Socket)

Example 53 with Socket

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

the class rrbroker method main.

public static void main(String[] args) {
    //  Prepare our context and sockets
    Context context = ZMQ.context(1);
    Socket frontend = context.socket(ZMQ.ROUTER);
    Socket backend = context.socket(ZMQ.DEALER);
    frontend.bind("tcp://*:5559");
    backend.bind("tcp://*:5560");
    System.out.println("launch and connect broker.");
    //  Initialize poll set
    Poller items = context.poller(2);
    items.register(frontend, Poller.POLLIN);
    items.register(backend, Poller.POLLIN);
    boolean more = false;
    byte[] message;
    //  Switch messages between sockets
    while (!Thread.currentThread().isInterrupted()) {
        //  poll and memorize multipart detection
        items.poll();
        if (items.pollin(0)) {
            while (true) {
                // receive message
                message = frontend.recv(0);
                more = frontend.hasReceiveMore();
                // Broker it
                backend.send(message, more ? ZMQ.SNDMORE : 0);
                if (!more) {
                    break;
                }
            }
        }
        if (items.pollin(1)) {
            while (true) {
                // receive message
                message = backend.recv(0);
                more = backend.hasReceiveMore();
                // Broker it
                frontend.send(message, more ? ZMQ.SNDMORE : 0);
                if (!more) {
                    break;
                }
            }
        }
    }
    //  We never get here but clean up anyhow
    frontend.close();
    backend.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 54 with Socket

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

the class rrworker method main.

public static void main(String[] args) throws Exception {
    Context context = ZMQ.context(1);
    //  Socket to talk to server
    Socket responder = context.socket(ZMQ.REP);
    responder.connect("tcp://localhost:5560");
    while (!Thread.currentThread().isInterrupted()) {
        //  Wait for next request from client
        String string = responder.recvStr(0);
        System.out.printf("Received request: [%s]\n", string);
        //  Do some 'work'
        Thread.sleep(1000);
        //  Send reply back to client
        responder.send("World");
    }
    //  We never get here but clean up anyhow
    responder.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 55 with Socket

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

the class rtdealer method main.

/**
     * While this example runs in a single process, that is just to make
     * it easier to start and stop the example. Each thread has its own
     * context and conceptually acts as a separate process.
     */
public static void main(String[] args) throws Exception {
    Context context = ZMQ.context(1);
    Socket broker = context.socket(ZMQ.ROUTER);
    broker.bind("tcp://*:5671");
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) {
        Thread worker = new Worker();
        worker.start();
    }
    //  Run for five seconds and then tell workers to end
    long endTime = System.currentTimeMillis() + 5000;
    int workersFired = 0;
    while (true) {
        //  Next message gives us least recently used worker
        String identity = broker.recvStr();
        broker.sendMore(identity);
        //  Envelope delimiter
        broker.recv(0);
        //  Response from worker
        broker.recv(0);
        broker.sendMore("");
        //  Encourage workers until it's time to fire them
        if (System.currentTimeMillis() < endTime)
            broker.send("Work harder");
        else {
            broker.send("Fired!");
            if (++workersFired == NBR_WORKERS)
                break;
        }
    }
    broker.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

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