Faruk Akgul

ztree: A New Structure For ZeroMQ's CZMQ: High Level C API

December 01, 2012 | View Comments

CZMQ is a high-level C binding for ZeroMQ. Its main purposes are:

  • To hide differences between different versions of ZeroMQ.
  • To provide a space for development of more sophisticated API semantics.
  • To wrap ZeroMQ core API in semantics that are natural and lead to shorter and more readable applications.

CZMQ provides two different structures out of the box:

  • zlist: A Singly linked list.
  • zhash: A Hash table.

I have implemented a new structure: ztree.

ztree is a generic binary search tree implementation. Sample usage:

int main (int argc, char const *argv[]) {

  zctx_t* context = zctx_new();
  void* socket = zsocket_new(context, ZMQ_REP);
  zsocket_bind(socket, "tcp://*:5050");

  printf("Starting server...\n");

  ztree_t* tree = ztree_new((ztree_compare_fn) compare);
  ztree_insert(tree, "hello");
  ztree_insert(tree, "world");
  ztree_insert(tree, "bonjour");
  char* s = ztree_find_max(tree);
  for(;;) {

    char* msg = zstr_recv(socket);

    char* mss = ztree_find_rec(tree, msg);
    printf("Received: %s\n", msg);

    if(mss) {
      printf("Found: %s\n", mss);
      zstr_send(socket, s);
    } else {
      printf("Not Found: %s\n", mss);
      zstr_send(socket, "Not Found");
    }

    free(msg);
  }

  zsocket_destroy(context, socket);
  zctx_destroy(&context);

  return 0;
}

Source code is on github if anyone is interested.

Share:Tweet

blog comments powered by Disqus