ØMQ Zero Message Query

Leveldb is quite faster for sequencial write and read, compared with other Key-Value Store. But a problem is that leveldb is embedded database, not server-client. Leveldb is used in Riak, as storage engine. And also I found leveldb-server, server-client leveldb using Zeromq written by python. My next research will be included distributed key value store. This is a kind of survey for creating level-server using C++.

ØMQ \zeromq\:
 Ø  The socket library that acts as a concurrency framework.
 Ø  Faster than TCP, for clustered products and supercomputing.
 Ø  Carries messages across inproc, IPC, TCP, and multicast.
 Ø  Connect N-to-N via fanout, pubsub, pipeline, request-reply.
 Ø  Asynch I/O for scalable multicore message-passing apps.
 Ø  Large and active open source community.
 Ø  30+ languages including C, C++, Java, .NET, Python.
 Ø  Most OSes including Linux, Windows, OS X.
 Ø  LGPL free software with full commercial support from iMatix.



Send multi message
client.cpp

#include <zmq.hpp>
#include <string>
#include <iostream>

int main ()
{
    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REQ);

    std::cout << "Connecting to hello world server…" << std::endl;
    socket.connect ("tcp://localhost:5555");

    //  Do 10 requests, waiting each time for a response
    for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
        zmq::message_t request (6);
        memcpy ((void *) request.data (), "Hogeh", 5);
        //wait next one
        socket.send (request,ZMQ_SNDMORE);
       //next value
        zmq::message_t request2 (6);
        memcpy ((void *) request2.data (), "12345", 5);
       //send
        socket.send (request2);
        std::cout << "Sending Hello " << request_nbr << "…" << std::endl;

        //  Get the reply.
        zmq::message_t reply;
        socket.recv (&reply);
        std::cout << "Received World " << request_nbr << std::endl;
    }
    return 0;
}  

Receive multiple message
If there are no message parts to follow, or if the message is not composed of multiple parts, ZMQ_RCVMORE shall report a value of zero. Otherwise, ZMQ_RCVMORE shall report a value of 1, indicating that more message parts are to follow.

		do {
	                 socket.getsockopt (ZMQ_RCVMORE, &more, &more_size);
		} while (more);

sever.cpp

#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>

int main () {
    //  Prepare our context and socket
	int64_t more;
	size_t more_size = sizeof more;
	char *test;
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REP);
    socket.bind ("tcp://*:5555");

    while (true) {
		/* Create an empty Q message to hold the message part */
        zmq::message_t request;

		do {
			socket.recv (&request);
			test = (char *)(request.data());
			std::cout << "Received Hello" << std::endl;
			std::cout << test  << std::endl;
		/* Block until a message is available to be received from socket */
			socket.recv (&request);
			test = (char *)(request.data());
			std::cout << "Received Hello" << std::endl;
			std::cout << test  << std::endl;
		/* Determine if more message parts are to follow */
			socket.getsockopt (ZMQ_RCVMORE, &more, &more_size);
		} while (more);


        //  Send reply back to client
        zmq::message_t reply (5);
        memcpy ((void *) reply.data (), "World", 5);
        socket.send (reply);
    }
    return 0;
}

ref
The Intelligent Transport Layer ØMQ
http://www.zeromq.org/

はじめてのZeroMQ その1
http://blog.liris.org/2011/01/zeromq-1.html

ØMQ(zeromq)について調査する。
http://blog.wktk.co.jp/archives/71

ØMQをつかってLevelDBをClient-Server型にする
http://siguniang.wordpress.com/2012/12/02/async-leveldb-server-using-zeromq/