Package org.zeromq.ZMQ

Examples of org.zeromq.ZMQ.Context


*/
public class rrbroker{

    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 = new 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();
    }
View Full Code Here


public class psenvsub {

    public static void main (String[] args) {

        // Prepare our context and subscriber
        Context context = ZMQ.context(1);
        Socket subscriber = context.socket(ZMQ.SUB);

        subscriber.connect("tcp://localhost:5563");
        subscriber.subscribe("B".getBytes());
        while (!Thread.currentThread ().isInterrupted ()) {
            // Read envelope with address
            String address = subscriber.recvStr ();
            // Read message contents
            String contents = subscriber.recvStr ();
            System.out.println(address + " : " + contents);
        }
        subscriber.close ();
        context.term ();
    }
View Full Code Here

class ClientThread extends Thread
{
  public void run()
  {
    Context context = ZMQ.context(1);

    //  Prepare our context and sockets
    Socket client  = context.socket(ZMQ.REQ);

    //  Initialize random number generator
    Random srandom = new Random(System.nanoTime());
    String id = String.format("%04x-%04x", srandom.nextInt(0x10000)+1,srandom.nextInt(0x10000)+1);
    client.setIdentity(id.getBytes());
View Full Code Here

class WorkerThread extends Thread
{
  public void run()
  {
    Context context = ZMQ.context(1);

    //  Prepare our context and sockets
    Socket worker  = context.socket(ZMQ.REQ);

    //  Initialize random number generator
    Random srandom = new Random(System.nanoTime());
    String id = String.format("%04x-%04x", srandom.nextInt(0x10000)+1,srandom.nextInt(0x10000)+1);
    worker.setIdentity(id.getBytes())//  Makes tracing easier
View Full Code Here

}

public class lruqueue {

  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 client_nbr;
    for (client_nbr = 0; client_nbr < 10; client_nbr++)
      new ClientThread().start();

    int worker_nbr;
    for (worker_nbr = 0; worker_nbr < 3; worker_nbr++)
      new WorkerThread().start();

    //  Logic of LRU loop
    //  - Poll backend always, frontend only if 1+ worker ready
    //  - If worker replies, queue worker as ready and forward reply
    //    to client if necessary
    //  - If client requests, pop next worker and send request to it
    //
    //  A very simple queue structure with known max size
    Queue<String> worker_queue = 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(worker_queue.size()>0)
        items.register(frontend, Poller.POLLIN);

      items.poll();

      //  Handle worker activity on backend
      if (items.pollin(0)) {

        //  Queue worker address for LRU routing
        worker_queue.add(new String(backend.recv(0)));

        //  Second frame is empty
        String empty = new String(backend.recv(0));
        assert empty.length()==0 | true;

        //  Third frame is READY or else a client reply address
        String client_addr = new String(backend.recv(0));

        //  If client reply, send rest back to frontend
        if (!client_addr.equals("READY")) {

          empty = new String(backend.recv(0));
          assert empty.length()==0 | true;

          String reply = new String(backend.recv(0));
          frontend.send(client_addr.getBytes(), ZMQ.SNDMORE);
          frontend.send("".getBytes(), ZMQ.SNDMORE);
          frontend.send(reply.getBytes(), 0);

          if (--client_nbr == 0)
            break;
        }

      }

      if (items.pollin(1)) {
        //  Now get next client request, route to LRU worker
        //  Client request is [address][empty][request]
        String client_addr = new String(frontend.recv(0));

        String empty = new String(frontend.recv(0));
        assert empty.length()==0 | true;

        String request = new String(frontend.recv(0));

        String worker_addr = worker_queue.poll();//worker_queue [0];

        backend.send(worker_addr.getBytes(), ZMQ.SNDMORE);
        backend.send("".getBytes(), ZMQ.SNDMORE);
        backend.send(client_addr.getBytes(), ZMQ.SNDMORE);
        backend.send("".getBytes(), ZMQ.SNDMORE);
        backend.send(request.getBytes(), 0);

      }

    }

    frontend.close();
    backend.close();
    context.term();

    System.exit(0);

  }
View Full Code Here

*
*/
public class rrbroker{
  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();
  }
View Full Code Here

*
* Christophe Huntzinger <chuntzin_at_wanadoo.fr>
*/
public class rrclient{
  public static void main (String[] args) {
    Context context = ZMQ.context(1);

    //  Socket to talk to server
    Socket requester = context.socket(ZMQ.REQ);
    requester.connect("tcp://localhost:5559");
   
    System.out.println("launch and connect client.");

    for (int request_nbr = 0; request_nbr < 10; request_nbr++) {
      requester.send("Hello".getBytes(), 0);
      byte[] reply = requester.recv(0);
      String replyValue = new String(reply);
      System.out.println("Received reply "+request_nbr+" ["+replyValue+"]");
    }
   
    //  We never get here but clean up anyhow
    requester.close();
    context.term();
  }
View Full Code Here

*
* Christophe Huntzinger <chuntzin_at_wanadoo.fr>
*/
public class rrserver{
  public static void main (String[] args) {
    Context context = ZMQ.context(1);

    //  Socket to talk to clients
    Socket responder  = context.socket(ZMQ.REP);
    responder.connect("tcp://localhost:5560");
   
    System.out.println("launch and connect server.");

    while (!Thread.currentThread().isInterrupted()) {
      //  Wait for next request from client
      byte[] request = responder.recv(0);
      String string = new String(request);
      System.out.println("Received request: ["+string+"].");

      //  Do some 'work'
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      //  Send reply back to client
      responder.send("World".getBytes(), 0);
    }
   
    //  We never get here but clean up anyhow
    responder.close();
    context.term();
  }
View Full Code Here

*/
public class msgqueue{

  public static void main (String[] args) {
    //  Prepare our context and sockets
    Context context = ZMQ.context(1);

    //  Socket facing clients
    Socket frontend = context.socket(ZMQ.ROUTER);
    frontend.bind("tcp://*:5559");

    //  Socket facing services
    Socket backend = context.socket(ZMQ.DEALER);
    backend.bind("tcp://*:5560");

    //  Start built-in device
    ZMQQueue queue = new ZMQQueue(context, frontend, backend);
    // have fun!
   
    //  We never get here but clean up anyhow
    frontend.close();
    backend.close();
    context.term();
  }
View Full Code Here

   * We wait for 10 subscribers
   */
  protected static int SUBSCRIBERS_EXPECTED = 10;

  public static void main (String[] args) {
    Context context = ZMQ.context(1);

    //  Socket to talk to clients
    Socket publisher = context.socket(ZMQ.PUB);
    publisher.bind("tcp://*:5561");

    //  Socket to receive signals
    Socket syncservice = context.socket(ZMQ.REP);
    syncservice.bind("tcp://*:5562");

    //  Get synchronization from subscribers
    int subscribers = 0;
    while (subscribers < SUBSCRIBERS_EXPECTED) {
      //  - wait for synchronization request
      byte[] value = syncservice.recv(0);

      //  - send synchronization reply
      syncservice.send("".getBytes(), 0);
      subscribers++;
    }
    //  Now broadcast exactly 1M updates followed by END
    int update_nbr;
    for (update_nbr = 0; update_nbr < 1000000; update_nbr++){
      publisher.send("Rhubarb".getBytes(), 0);
    }

    publisher.send("END".getBytes(), 0);

    //  Give 0MQ/2.0.x time to flush output

    try {
      Thread.sleep (1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }        

    // clean up
    publisher.close();
    syncservice.close();
    context.term();
  }
View Full Code Here

TOP

Related Classes of org.zeromq.ZMQ.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.