Example #1
0
int main() {
    IOLoop loop;
    shared_ptr<HttpClient> client = make_shared<HttpClient>(&loop);
    client->request("http://127.0.0.1:11192/", bind(&onResponse, &loop, _1));
    loop.start();
    printf("exit\n");
    return 0;
}
Example #2
0
int main()
{
    IOLoop loop;
    WsClientPtr_t client = std::make_shared<WsClient>(&loop);
    
    client->connect("ws://127.0.0.1:11181/push/ws", std::bind(&onWsConnEvent, _1, _2, _3));
    
    loop.start();
    
    return 0;    
}
Example #3
0
void onTimer(const TimerPtr_t& timer)
{
    cout << "onTimer\t" << time(0) << endl;
    if(++i >= 10)
    {
        IOLoop* loop = timer->loop();
        
        timer->stop();
        
        loop->stop();    
    } 
}
Example #4
0
int main(int argc, char* args[])
{
    Log::rootLog().setLevel(Log::ERROR);
  
    if(argc < 2)
    {
        cout << "args: num [eth0]" << endl;
    }

    int num = num = atoi(args[1]);

    vector<HttpClientPtr_t> clients;

    IOLoop loop;
    
    if(argc == 2)
    {
        clients.push_back(std::make_shared<HttpClient>(&loop));
    }
    else
    {
        for(int i = 0; i < argc - 2; ++i)
        {
            HttpClientPtr_t client = std::make_shared<HttpClient>(&loop);
            client->bindDevice(args[i + 2]);
            clients.push_back(client);
        }
    }

    cout << "request num:" << num << endl;


    TimingWheelPtr_t wheel = std::make_shared<TimingWheel>(1000, 3600);

    int c = 60 * clients.size();
    for(int i = 0; i < c; ++i)
    {
        int reqNum = num / c;
        for(auto it = clients.begin(); it != clients.end(); ++it)
        {
            wheel->add(std::bind(&request, _1, *it, reqNum), i * 1000);
        }
    }

    wheel->start(&loop);

    loop.start();

    return 0;
}
Example #5
0
int main()
{

    IOLoop loop;

    run(&loop);

    cout << "start" << endl;

    loop.start();

    cout << "end" << endl;

    return 0;
    
}
Example #6
0
int main(int argc, char** argv) {

    IOLoop loop;

    SharedClient client = make_shared<echolib::Client>(loop, string(argv[1]));

    if (argc > 2) {

        bool write = false;

        string data = slurp(string(argv[2]));

        function<void(int)> subscribe_callback = [&](int m) {
            write = m > 0;
        };

        TypedPublisher<string,true> pub(client, "chunked");
        SubscriptionWatcher watch(client, "chunked", subscribe_callback);

        int counter = 0;

        while(loop.wait(10)) {
            counter++;
            if (write && counter == 50) {
                std::cout << "Writing data" << std::endl;
                pub.send(data);
            }
            counter = counter % 50;
        }

    } else {
        function<void(shared_ptr<string>)> chunked_callback = [&](shared_ptr<string> m) {

            std::cout << "Received " << m->size() << " bytes" << std::endl;

        };

        TypedSubscriber<string, true> sub(client, "chunked", chunked_callback);

        while(loop.wait(10)) {
        }

    }

    exit(0);
}
Example #7
0
int main(int argc, char** argv) {

    IOLoop loop;

    //Connect to local socket based daemon
    SharedClient client = make_shared<echolib::Client>(loop, string(argv[1]));

    string name;
    cout << "Enter your name\n";
    std::getline(std::cin, name);

    std::mutex mutex;

    function<void(shared_ptr<pair<string, string> >)> chat_callback = [&](shared_ptr<pair<string, string> > m) {
        const string name = m->first;
        const string message = m->second;
        std::cout<< name << ": " << message << std::endl;
    };

    function<void(int)> subscribe_callback = [&](int m) {
        std::cout << "Total subscribers: " << m << std::endl;
    };

    TypedSubscriber<pair<string, string> > sub(client, "chat", chat_callback);
    SubscriptionWatcher watch(client, "chat", subscribe_callback);
    TypedPublisher<pair<string, string> > pub(client, "chat");

    std::thread write([&]() {
        while (client->is_connected()) {
            string message;
            std::getline(std::cin, message);
            pub.send(pair<string, string>(name, message));
        }

    });

    while(loop.wait(10)) {
        // We have to give the write thread some space
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    exit(0);
}
Example #8
0
int main()
{
    IOLoop loop;

    TimingWheelPtr_t t = std::make_shared<TimingWheel>(1000, 20);

    t->add(std::bind(&onWheel, _1, 1), 1000);
    t->add(std::bind(&onWheel, _1, 2), 2000);

    t->start(&loop);

    cout << "start" << endl;
    loop.start();

    t->stop();

    cout << "end" << endl;

    return 0;
}
Example #9
0
 void Set(int fd, unsigned mask, FileEventHandler &handler) {
   loop.Set(fd, mask, handler);
 }
Example #10
0
 /**
  * Remove a file descriptor from the I/O loop.
  *
  * This method is not thread-safe, it may only be called from within
  * the thread.
  */
 void Remove(int fd) {
   loop.Remove(fd);
 }
Example #11
0
 /**
  * Add a file descriptor to the I/O loop.
  *
  * This method is not thread-safe, it may only be called from within
  * the thread.
  */
 void Add(int fd, unsigned mask, FileEventHandler &handler) {
   loop.Add(fd, mask, handler);
 }