Search in sources :

Example 16 with Poller

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

the class peering1 method main.

public static void main(String[] argv) {
    //
    if (argv.length < 1) {
        System.out.println("syntax: peering1 me {you}\n");
        System.exit(-1);
    }
    String self = argv[0];
    System.out.println(String.format("I: preparing broker at %s\n", self));
    Random rand = new Random(System.nanoTime());
    ZContext ctx = new ZContext();
    //  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);
    int argn;
    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));
    }
    //  The main loop sends out status messages to peers, and collects
    //  status messages back from peers. The zmq_poll timeout defines
    //  our own heartbeat.
    Poller poller = ctx.createPoller(1);
    poller.register(statefe, Poller.POLLIN);
    while (true) {
        //  Poll for activity, or 1 second timeout
        int rc = poller.poll(1000);
        if (rc == -1)
            //  Interrupted
            break;
        //  Handle incoming status messages
        if (poller.pollin(0)) {
            String peer_name = new String(statefe.recv(0), ZMQ.CHARSET);
            String available = new String(statefe.recv(0), ZMQ.CHARSET);
            System.out.printf("%s - %s workers free\n", peer_name, available);
        } else {
            //  Send random values for worker availability
            statebe.send(self, ZMQ.SNDMORE);
            statebe.send(String.format("%d", rand.nextInt(10)), 0);
        }
    }
    ctx.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Example 17 with Poller

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

the class peering2 method main.

//  The main task begins by setting-up its frontend and backend sockets
//  and then starting its client and worker tasks:
public static void main(String[] argv) {
    //
    if (argv.length < 1) {
        System.out.println("syntax: peering2 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();
    //  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));
    }
    //  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));
    //  Get user to tell us when we can start
    System.out.println("Press Enter when all brokers are started: ");
    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //  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();
    //  Here we handle the request-reply flow. We're using the LRU approach
    //  to poll workers at all times, and clients only when there are one or
    //  more workers available.
    //  Least recently used queue of available workers
    int capacity = 0;
    ArrayList<ZFrame> workers = new ArrayList<ZFrame>();
    Poller backends = ctx.createPoller(2);
    backends.register(localbe, Poller.POLLIN);
    backends.register(cloudbe, Poller.POLLIN);
    Poller frontends = ctx.createPoller(2);
    frontends.register(localfe, Poller.POLLIN);
    frontends.register(cloudfe, Poller.POLLIN);
    while (true) {
        //  First, route any waiting replies from workers
        //  If we have no workers anyhow, wait indefinitely
        int rc = backends.poll(capacity > 0 ? 1000 : -1);
        if (rc == -1)
            //  Interrupted
            break;
        //  Handle reply from local worker
        ZMsg msg = null;
        if (backends.pollin(0)) {
            msg = ZMsg.recvMsg(localbe);
            if (msg == null)
                //  Interrupted
                break;
            ZFrame address = msg.unwrap();
            workers.add(address);
            capacity++;
            //  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 (backends.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);
        while (capacity > 0) {
            rc = frontends.poll(0);
            assert (rc >= 0);
            int reroutable = 0;
            //  We'll do peer brokers first, to prevent starvation
            if (frontends.pollin(1)) {
                msg = ZMsg.recvMsg(cloudfe);
                reroutable = 0;
            } else if (frontends.pollin(0)) {
                msg = ZMsg.recvMsg(localfe);
                reroutable = 1;
            } else
                //  No work, go back to backends
                break;
            //
            if (reroutable != 0 && argv.length > 1 && rand.nextInt(5) == 0) {
                //  Route to random broker peer
                int random_peer = rand.nextInt(argv.length - 1) + 1;
                msg.push(argv[random_peer]);
                msg.send(cloudbe);
            } else {
                ZFrame frame = workers.remove(0);
                msg.wrap(frame);
                msg.send(localbe);
                capacity--;
            }
        }
    }
    //  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) IOException(java.io.IOException) 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 18 with Poller

use of org.zeromq.ZMQ.Poller 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 19 with Poller

use of org.zeromq.ZMQ.Poller 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 20 with Poller

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

the class spqueue method main.

public static void main(String[] args) {
    ZContext ctx = new ZContext();
    Socket frontend = ctx.createSocket(ZMQ.ROUTER);
    Socket backend = ctx.createSocket(ZMQ.ROUTER);
    //  For clients
    frontend.bind("tcp://*:5555");
    //  For workers
    backend.bind("tcp://*:5556");
    //  Queue of available workers
    ArrayList<ZFrame> workers = new ArrayList<ZFrame>();
    Poller poller = ctx.createPoller(2);
    poller.register(backend, Poller.POLLIN);
    poller.register(frontend, Poller.POLLIN);
    //  The body of this example is exactly the same as lruqueue2.
    while (true) {
        boolean workersAvailable = workers.size() > 0;
        int rc = poller.poll(-1);
        //  Poll frontend only if we have available workers
        if (rc == -1)
            //  Interrupted
            break;
        //  Handle worker activity on backend
        if (poller.pollin(0)) {
            //  Use worker address for LRU routing
            ZMsg msg = ZMsg.recvMsg(backend);
            if (msg == null)
                //  Interrupted
                break;
            ZFrame address = msg.unwrap();
            workers.add(address);
            //  Forward message to client if it's not a READY
            ZFrame frame = msg.getFirst();
            if (new String(frame.getData(), ZMQ.CHARSET).equals(WORKER_READY))
                msg.destroy();
            else
                msg.send(frontend);
        }
        if (workersAvailable && poller.pollin(1)) {
            //  Get client request, route to first available worker
            ZMsg msg = ZMsg.recvMsg(frontend);
            if (msg != null) {
                msg.wrap(workers.remove(0));
                msg.send(backend);
            }
        }
    }
    //  When we're done, clean up properly
    while (workers.size() > 0) {
        ZFrame frame = workers.remove(0);
        frame.destroy();
    }
    workers.clear();
    ctx.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ArrayList(java.util.ArrayList) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

Poller (org.zeromq.ZMQ.Poller)22 Socket (org.zeromq.ZMQ.Socket)20 ZContext (org.zeromq.ZContext)17 ZMsg (org.zeromq.ZMsg)10 Random (java.util.Random)7 ZFrame (org.zeromq.ZFrame)7 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Context (org.zeromq.ZMQ.Context)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 RandomAccessFile (java.io.RandomAccessFile)1