zmq: basic patterns Request-Reply, Publisher-Subscriber, Denvilator-worker-sink.

zmq: basic patterns Request-Reply, Publisher-Subscriber, Denvilator-worker-sink._第1张图片

#include<zmq.h>
#include <string.h>

/*
g++ req-rep-server.cpp -lzmq -g -O0 -o objs/req-rep-server
*/

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* responder = zmq_socket(context, ZMQ_REP);
    zmq_bind(responder, "tcp://*:1990");
    
    while(true){
        zmq_msg_t request;
        zmq_msg_init(&request);
        zmq_msg_recv(&request, responder, 0);
        zmq_msg_close(&request);
        
        zmq_msg_t reply;
        zmq_msg_init_size(&reply, 6);
        memcpy(zmq_msg_data(&reply), "World", 6);
        zmq_msg_send(&reply, responder, 0);
        zmq_msg_close(&reply);
    }
    
    zmq_close(responder);
    zmq_ctx_destroy(context);
    
    return 0;
}
#include <zmq.h>
#include <string.h>
#include <unistd.h>

/*
g++ req-rep-client.cpp -lzmq -g -O0 -o objs/req-rep-client
*/

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* requester = zmq_socket(context, ZMQ_REQ);
    zmq_connect(requester, "tcp://localhost:1990");
    
    for(;;){
        zmq_msg_t request;
        zmq_msg_init_size(&request, 5);
        memcpy(zmq_msg_data(&request), "Hello", 5);
        zmq_msg_send(&request, requester, 0);
        zmq_msg_close(&request);
        
        sleep(1);
        
        zmq_msg_t reply;
        zmq_msg_init(&reply);
        zmq_msg_recv(&reply, requester, 0);
        printf("get reply: %s\n", (char*)zmq_msg_data(&reply));
        zmq_msg_close(&reply);
        
        sleep(1);
    }
    
    zmq_close(requester);
    zmq_ctx_destroy(context);
    
    return 0;
}
zmq: basic patterns Request-Reply, Publisher-Subscriber, Denvilator-worker-sink._第2张图片
/**
g++ pub-sub-publisher.cpp -lzmq -g -O0 -g -o objs/pub-sub-publisher
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zmq.h>

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* publisher = zmq_socket(context, ZMQ_PUB);
    zmq_bind(publisher, "tcp://*:1990");
    
    srand(0);
    char* filter = (argc > 1)? argv[1] : (char*)"xxxx ";
    while(true){
        char buf[64];
        snprintf(buf, sizeof(buf), "%s a=%d b=%d c=%d", filter, rand()%1000, rand()%1000, rand()%1000);
        
        zmq_msg_t msg;
        zmq_msg_init_size(&msg, strlen(buf)+1);
        memcpy(zmq_msg_data(&msg), buf, strlen(buf)+1);
        zmq_msg_send(&msg, publisher, 0);
        zmq_msg_close(&msg);
        
        printf("filter=\"%s\", publish message: %s\n", filter, buf);
        sleep(1);
    }
    
    zmq_close(publisher);
    zmq_ctx_destroy(context);
    
    return 0;
}

/**
g++ pub-sub-subscriber.cpp -lzmq -g -O0 -g -o objs/pub-sub-subscriber
*/
#include <stdlib.h>
#include <string.h>
#include <zmq.h>

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* subscriber = zmq_socket(context, ZMQ_SUB);
    zmq_connect(subscriber, "tcp://localhost:1990");
    
    char* filter = (argc > 1)? argv[1] : (char*)"xxxx ";
    zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter));
    
    while(true){
        zmq_msg_t msg;
        zmq_msg_init(&msg);
        zmq_msg_recv(&msg, subscriber, 0);
        
        printf("filter=\"%s\" msg: %s\n", filter, zmq_msg_data(&msg));
        
        zmq_msg_close(&msg);
    }
    
    zmq_close(subscriber);
    zmq_ctx_destroy(context);
    
    return 0;
}

/**
g++ div-con-ventilator.cpp -lzmq -g -O0 -o objs/div-con-ventilator
*/
#include <zmq.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* ventilator = zmq_socket(context, ZMQ_PUSH);
    zmq_bind(ventilator, "tcp://*:1990");
    
    srand(0);
    int task_num = (argc > 1)? atoi(argv[1]) : 100;
    printf("ventilator will dispatch %d task to worker.\n", task_num);
    
    // we must wait for worker to connect to the ventilator,
    // if there is 10/100 workers connected, we will dispatch to the 10 workers,
    // the left 90 workers will get workers when they connect to ventilator.
    printf("when all worker is online, press any key to dispatch tasks...");
    getchar();
    
    while(true){
        for(int i = 0; i < task_num; i++){
            printf("start to dispatch %d task\n", i + 1);
            zmq_msg_t msg;
            zmq_msg_init_size(&msg, 4);
            int time = rand() % 10;
            memcpy(zmq_msg_data(&msg), &time, 4);
            zmq_msg_send(&msg, ventilator, 0);
            printf("dispatch %d task completed\n", i + 1);
        }
        
        sleep(10);
        // if there is new worker, we can dispatch task to it.
        printf("dispatch the task again!\n");
    }
    
    zmq_close(ventilator);
    zmq_ctx_destroy(context);
    return 0;
}

/**
g++ div-con-worker.cpp -lzmq -g -O0 -o objs/div-con-worker
*/
#include <zmq.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    
    void* worker_receiver = zmq_socket(context, ZMQ_PULL);
    zmq_connect(worker_receiver, "tcp://localhost:1990");
    
    void* sink_sender = zmq_socket(context, ZMQ_PUSH);
    zmq_connect(sink_sender, "tcp://localhost:1991");
    
    while(true){
        int time;
        
        // pull work
        if(true){
            zmq_msg_t msg;
            zmq_msg_init(&msg);
            zmq_msg_recv(&msg, worker_receiver, 0);
            memcpy(&time, zmq_msg_data(&msg), 4);
            zmq_msg_close(&msg);
            
            printf("get a worker, time=%d\n", time);
        }
        
        // do work
        sleep(time);
        printf("work completed.\n");
        
        // report work
        if(true){
            char buf[64];
            snprintf(buf, sizeof(buf), "time=%d", time);
            
            zmq_msg_t msg;
            zmq_msg_init_size(&msg, strlen(buf) + 1);
            memcpy(zmq_msg_data(&msg), buf, strlen(buf) + 1);
            zmq_msg_send(&msg, sink_sender, 0);
            zmq_msg_close(&msg);
            
            printf("work report to sink completed, %s\n", buf);
        }
    }
    
    zmq_close(sink_sender);
    zmq_close(worker_receiver);
    zmq_ctx_destroy(context);
    return 0;
}

/**
g++ div-con-sink.cpp -lzmq -g -O0 -o objs/div-con-sink
*/
#include <zmq.h>
#include <stdlib.h>

int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* sink = zmq_socket(context, ZMQ_PULL);
    zmq_bind(sink, "tcp://*:1991");
    
    while(true){
        zmq_msg_t msg;
        zmq_msg_init(&msg);
        zmq_msg_recv(&msg, sink, 0);
        
        printf("sink get a work report: %s\n", zmq_msg_data(&msg));
    }
    
    zmq_close(sink);
    zmq_ctx_destroy(context);
    return 0;
}


你可能感兴趣的:(zmq: basic patterns Request-Reply, Publisher-Subscriber, Denvilator-worker-sink.)