Code Monkey home page Code Monkey logo

cpp_gossip's Introduction

cpp_gossip

cpp_gossip is an header-only cross-platform gossip(aka. anti-entropy) protocol written in c++11

Getting started

  1. git clone [email protected]:zhaoguojie2010/cpp_gossip.git
  2. include "gossip.hpp"
  3. create join/leave handler, the signature should be void(std::string)
  4. create a config object: gossip::config conf(addr). addr format is ip:port
  5. create a gossiper instance: gossip::gossiper<1> g(conf);
  6. register join/leave handler: g.RegisterNotifier(notify_join, notify_leave);
  7. join a seed node of the target cluster: g.Join(seed_addr);
  • say now you have a main.cpp
#include <iostream>
#include <thread>
#include <chrono>
#include "gossip.hpp"
void notify_join(std::string node) {
    std::cout << "node " << node << " joined." << std::endl;
}
void notify_leave(std::string node) {
    std::cout << "node " << node << " left." << std::endl;
}
int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cout << "example: ./a.out <local addr> <join addr>";
        return 0;
    }

    std::string addr(argv[1]);
    gossip::config conf(addr);
    gossip::gossiper<1> g(conf);
    g.RegisterNotifier(notify_join, notify_leave);
    g.Join(std::string(argv[2]));

    std::this_thread::sleep_for(std::chrono::seconds(10000));
    return 0;
}
  • compile

g++ main.cpp -std=c++0x -I path/to/cppgossip -I path/to/cppgossip/thirdparty

  • test
    • in one terminal:

    ./a.out 127.0.0.1:29011 127.0.0.1:29011

    • in another terminal:

    ./a.out 127.0.0.1:29013 127.0.0.1:29011

API

  • void gossiper::Join(const std::string &peer)
    • Join the target cluster, peer denotes the seed node of the cluster
  • void gossiper::RegisterNotifier(event_notifier notify_join, notify_leave)
    • Register join/leave event handler. If not specified, gossiper does nothing when such events occur
    • WARNING: make sure that neither notify_join or notify_leave will block, and since they runs in a separate thread, make sure they are thread-safe
  • std::vectorstd::string gossiper::GetAliveNodes()
    • Return all alive nodes of the cluster. Each element is a ip:port pair

NOTE

  • when you create a gossiper as mentioned before: gossip::gossiper<THREAD_NUM> g(conf);, normally set THREAD_NUM to 1 would be recommended. This means that all the gossip routines run in one separate OS thread. However, if you need more threads to handle the gossip routines, says 3 threads, just change the code as follow: gossip::gossiper<3> g(conf);

cpp_gossip's People

Contributors

zhaoguojie2010 avatar

Watchers

 avatar

Forkers

sunyotech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.