Search in sources :

Example 26 with ZContext

use of org.zeromq.ZContext in project jeromq by zeromq.

the class suisnail method main.

//  .split main task
//  The main task simply starts a client and a server, and then
//  waits for the client to signal that it has died:
public static void main(String[] args) throws Exception {
    ZContext ctx = new ZContext();
    Socket pubpipe = ZThread.fork(ctx, new Publisher());
    Socket subpipe = ZThread.fork(ctx, new Subscriber());
    subpipe.recvStr();
    pubpipe.send("break");
    Thread.sleep(100);
    ctx.destroy();
}
Also used : ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Example 27 with ZContext

use of org.zeromq.ZContext 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 28 with ZContext

use of org.zeromq.ZContext 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 29 with ZContext

use of org.zeromq.ZContext in project jeromq by zeromq.

the class lbbroker3 method main.

/**
     * And the main task now sets-up child tasks, then starts its reactor.
     * If you press Ctrl-C, the reactor exits and the main task shuts down.
     */
public static void main(String[] args) {
    ZContext context = new ZContext();
    LBBroker arg = new LBBroker();
    //  Prepare our context and sockets
    arg.frontend = context.createSocket(ZMQ.ROUTER);
    arg.backend = context.createSocket(ZMQ.ROUTER);
    arg.frontend.bind("ipc://frontend.ipc");
    arg.backend.bind("ipc://backend.ipc");
    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) ZThread.start(new ClientTask());
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) ZThread.start(new WorkerTask());
    //  Queue of available workers
    arg.workers = new LinkedList<ZFrame>();
    //  Prepare reactor and fire it up
    ZLoop reactor = new ZLoop(context);
    PollItem item = new PollItem(arg.backend, ZMQ.Poller.POLLIN);
    reactor.addPoller(item, backendHandler, arg);
    reactor.start();
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) PollItem(org.zeromq.ZMQ.PollItem) ZLoop(org.zeromq.ZLoop) ZContext(org.zeromq.ZContext)

Example 30 with ZContext

use of org.zeromq.ZContext in project jeromq by zeromq.

the class pathopub method main.

public static void main(String[] args) throws Exception {
    ZContext context = new ZContext();
    Socket publisher = context.createSocket(ZMQ.PUB);
    if (args.length == 1)
        publisher.connect(args[0]);
    else
        publisher.bind("tcp://*:5556");
    //  Ensure subscriber connection has time to complete
    Thread.sleep(1000);
    //  Send out all 1,000 topic messages
    int topicNbr;
    for (topicNbr = 0; topicNbr < 1000; topicNbr++) {
        publisher.send(String.format("%03d", topicNbr), ZMQ.SNDMORE);
        publisher.send("Save Roger");
    }
    //  Send one random update per second
    Random rand = new Random(System.currentTimeMillis());
    while (!Thread.currentThread().isInterrupted()) {
        Thread.sleep(1000);
        publisher.send(String.format("%03d", rand.nextInt(1000)), ZMQ.SNDMORE);
        publisher.send("Off with his head!");
    }
    context.destroy();
}
Also used : Random(java.util.Random) ZContext(org.zeromq.ZContext) Socket(org.zeromq.ZMQ.Socket)

Aggregations

ZContext (org.zeromq.ZContext)33 Socket (org.zeromq.ZMQ.Socket)30 Poller (org.zeromq.ZMQ.Poller)17 ZMsg (org.zeromq.ZMsg)14 ZFrame (org.zeromq.ZFrame)13 Random (java.util.Random)11 ArrayList (java.util.ArrayList)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Entry (java.util.Map.Entry)2 ZLoop (org.zeromq.ZLoop)2 PollItem (org.zeromq.ZMQ.PollItem)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 LinkedList (java.util.LinkedList)1