Search in sources :

Example 16 with ZMsg

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

the class clone method set.

//  .split set method
//  Set a new value in the shared hashmap. Sends a [SET][key][value][ttl]
//  command through to the agent which does the actual work:
public void set(String key, String value, int ttl) {
    ZMsg msg = new ZMsg();
    msg.add("SET");
    msg.add(key);
    msg.add(value);
    msg.add(String.format("%d", ttl));
    msg.send(pipe);
}
Also used : ZMsg(org.zeromq.ZMsg)

Example 17 with ZMsg

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

the class ticlient method serviceCall.

static ZMsg serviceCall(mdcliapi session, String service, ZMsg request) {
    ZMsg reply = session.send(service, request);
    if (reply != null) {
        ZFrame status = reply.pop();
        if (status.streq("200")) {
            status.destroy();
            return reply;
        } else if (status.streq("400")) {
            System.out.println("E: client fatal error, aborting");
        } else if (status.streq("500")) {
            System.out.println("E: server fatal error, aborting");
        }
        reply.destroy();
    }
    //  Didn't succeed; don't care why not
    return null;
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg)

Example 18 with ZMsg

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

the class ticlient method main.

public static void main(String[] args) throws Exception {
    boolean verbose = (args.length > 0 && args[0].equals("-v"));
    mdcliapi session = new mdcliapi("tcp://localhost:5555", verbose);
    //  1. Send 'echo' request to Titanic
    ZMsg request = new ZMsg();
    request.add("echo");
    request.add("Hello world");
    ZMsg reply = serviceCall(session, "titanic.request", request);
    ZFrame uuid = null;
    if (reply != null) {
        uuid = reply.pop();
        reply.destroy();
        uuid.print("I: request UUID ");
    }
    //  2. Wait until we get a reply
    while (!Thread.currentThread().isInterrupted()) {
        Thread.sleep(100);
        request = new ZMsg();
        request.add(uuid.duplicate());
        reply = serviceCall(session, "titanic.reply", request);
        if (reply != null) {
            String replyString = reply.getLast().toString();
            System.out.printf("Reply: %s\n", replyString);
            reply.destroy();
            //  3. Close request
            request = new ZMsg();
            request.add(uuid.duplicate());
            reply = serviceCall(session, "titanic.close", request);
            reply.destroy();
            break;
        } else {
            System.out.println("I: no reply yet, trying again...");
            //  Try again in 5 seconds
            Thread.sleep(5000);
        }
    }
    uuid.destroy();
    session.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg)

Example 19 with ZMsg

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

the class titanic method serviceSuccess.

//  .split try to call a service
//  Here, we first check if the requested MDP service is defined or not,
//  using a MMI lookup to the Majordomo broker. If the service exists,
//  we send a request and wait for a reply using the conventional MDP
//  client API. This is not meant to be fast, just very simple:
static boolean serviceSuccess(String uuid) {
    //  Load request message, service will be first frame
    String filename = requestFilename(uuid);
    //  If the client already closed request, treat as successful
    if (!new File(filename).exists())
        return true;
    DataInputStream file = null;
    ZMsg request;
    try {
        file = new DataInputStream(new FileInputStream(filename));
        request = ZMsg.load(file);
    } catch (IOException e) {
        e.printStackTrace();
        return true;
    } finally {
        try {
            if (file != null)
                file.close();
        } catch (IOException e) {
        }
    }
    ZFrame service = request.pop();
    String serviceName = service.toString();
    //  Create MDP client session with short timeout
    mdcliapi client = new mdcliapi("tcp://localhost:5555", false);
    //  1 sec
    client.setTimeout(1000);
    //  only 1 retry
    client.setRetries(1);
    //  Use MMI protocol to check if service is available
    ZMsg mmiRequest = new ZMsg();
    mmiRequest.add(service);
    ZMsg mmiReply = client.send("mmi.service", mmiRequest);
    boolean serviceOK = (mmiReply != null && mmiReply.getFirst().toString().equals("200"));
    mmiReply.destroy();
    boolean result = false;
    if (serviceOK) {
        ZMsg reply = client.send(serviceName, request);
        if (reply != null) {
            filename = replyFilename(uuid);
            DataOutputStream ofile = null;
            try {
                ofile = new DataOutputStream(new FileOutputStream(filename));
                ZMsg.save(reply, ofile);
            } catch (IOException e) {
                e.printStackTrace();
                return true;
            } finally {
                try {
                    if (file != null)
                        file.close();
                } catch (IOException e) {
                }
            }
            result = true;
        }
        reply.destroy();
        ;
    } else
        request.destroy();
    client.destroy();
    return result;
}
Also used : ZFrame(org.zeromq.ZFrame) DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZMsg(org.zeromq.ZMsg) FileInputStream(java.io.FileInputStream)

Example 20 with ZMsg

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

the class titanic method main.

//  .split worker task
//  This is the main thread for the Titanic worker. It starts three child
//  threads; for the request, reply, and close services. It then dispatches
//  requests to workers using a simple brute force disk queue. It receives
//  request UUIDs from the {{titanic.request}} service, saves these to a disk
//  file, and then throws each request at MDP workers until it gets a
//  response.
public static void main(String[] args) {
    boolean verbose = (args.length > 0 && "-v".equals(args[0]));
    ZContext ctx = new ZContext();
    Socket requestPipe = ZThread.fork(ctx, new TitanicRequest());
    ZThread.start(new TitanicReply());
    ZThread.start(new TitanicClose());
    Poller poller = ctx.createPoller(1);
    poller.register(requestPipe, ZMQ.Poller.POLLIN);
    //  Main dispatcher loop
    while (true) {
        //  We'll dispatch once per second, if there's no activity
        int rc = poller.poll(1000);
        if (rc == -1)
            //  Interrupted
            break;
        if (poller.pollin(0)) {
            //  Ensure message directory exists
            new File(TITANIC_DIR).mkdirs();
            //  Append UUID to queue, prefixed with '-' for pending
            ZMsg msg = ZMsg.recvMsg(requestPipe);
            if (msg == null)
                //  Interrupted
                break;
            String uuid = msg.popString();
            BufferedWriter wfile = null;
            try {
                wfile = new BufferedWriter(new FileWriter(TITANIC_DIR + "/queue", true));
                wfile.write("-" + uuid + "\n");
            } catch (IOException e) {
                e.printStackTrace();
                break;
            } finally {
                try {
                    if (wfile != null)
                        wfile.close();
                } catch (IOException e) {
                }
            }
            msg.destroy();
        }
        //  Brute force dispatcher
        //"?........:....:....:....:............:";
        byte[] entry = new byte[37];
        RandomAccessFile file = null;
        try {
            file = new RandomAccessFile(TITANIC_DIR + "/queue", "rw");
            while (file.read(entry) > 0) {
                //  UUID is prefixed with '-' if still waiting
                if (entry[0] == '-') {
                    if (verbose)
                        System.out.printf("I: processing request %s\n", new String(entry, 1, entry.length - 1, ZMQ.CHARSET));
                    if (serviceSuccess(new String(entry, 1, entry.length - 1, ZMQ.CHARSET))) {
                        //  Mark queue entry as processed
                        file.seek(file.getFilePointer() - 37);
                        file.writeBytes("+");
                        file.seek(file.getFilePointer() + 36);
                    }
                }
                //  Skip end of line, LF or CRLF
                if (file.readByte() == '\r')
                    file.readByte();
                if (Thread.currentThread().isInterrupted())
                    break;
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (file != null) {
                try {
                    file.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ZContext(org.zeromq.ZContext) ZMsg(org.zeromq.ZMsg) BufferedWriter(java.io.BufferedWriter) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Socket(org.zeromq.ZMQ.Socket) Poller(org.zeromq.ZMQ.Poller)

Aggregations

ZMsg (org.zeromq.ZMsg)38 ZFrame (org.zeromq.ZFrame)20 ZContext (org.zeromq.ZContext)14 Socket (org.zeromq.ZMQ.Socket)14 Poller (org.zeromq.ZMQ.Poller)10 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ZMQ (org.zeromq.ZMQ)4 IOException (java.io.IOException)3 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 BufferedWriter (java.io.BufferedWriter)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 LinkedList (java.util.LinkedList)1 PollItem (org.zeromq.ZMQ.PollItem)1