zmq: transport bridging, the pub-sub forward proxy

zmq: transport bridging, the pub-sub forward proxy_第1张图片

/*
g++ pub-sub-bridge-publisher.cpp -lzmq -g -O0 -o objs/pub-sub-bridge-publisher
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zmq.h>
int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* publisher = zmq_socket(context, ZMQ_PUB);
    assert(zmq_bind(publisher, "tcp://*:1990") == 0);
    
    srand(0);
    while(true){
        char buf[64];
        snprintf(buf, sizeof(buf), "xxx a=%d b=%d c=%d", rand(), rand(), rand());
        
        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);
        
        usleep(rand()%100 *1000);
    }
    
    zmq_close(publisher);
    zmq_ctx_destroy(context);
    return 0;
}

/*
g++ pub-sub-bridge-subscriber.cpp -lzmq -g -O0 -o objs/pub-sub-bridge-subscriber
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zmq.h>
int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* subscriber = zmq_socket(context, ZMQ_SUB);
    assert(zmq_connect(subscriber, "tcp://localhost:1990") == 0);
    
    const char* filter = "xxx";
    assert(zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter)) == 0);
    
    while(true){
        zmq_msg_t msg;
        zmq_msg_init(&msg);
        zmq_msg_recv(&msg, subscriber, 0);
        printf("get msg: %s\n", zmq_msg_data(&msg));
        zmq_msg_close(&msg);
    }
    
    zmq_close(subscriber);
    zmq_ctx_destroy(context);
    return 0;
}

/*
g++ pub-sub-bridge-forwarder.cpp -lzmq -g -O0 -o objs/pub-sub-bridge-forwarder
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zmq.h>
int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* frontend_xsub = zmq_socket(context, ZMQ_XSUB);
    void* backend_xpub = zmq_socket(context, ZMQ_XPUB);
    
    assert(zmq_connect(frontend_xsub, "tcp://localhost:1990") == 0);
    assert(zmq_bind(backend_xpub, "tcp://*:1991") == 0);
    
    zmq_proxy(frontend_xsub, backend_xpub, NULL);
    
    zmq_close(backend_xpub);
    zmq_close(frontend_xsub);
    zmq_ctx_destroy(context);
    return 0;
}

/*
g++ pub-sub-bridge-subscriber2.cpp -lzmq -g -O0 -o objs/pub-sub-bridge-subscriber2
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <zmq.h>
int main(int argc, char** argv){
    void* context = zmq_ctx_new();
    void* subscriber2 = zmq_socket(context, ZMQ_SUB);
    assert(zmq_connect(subscriber2, "tcp://localhost:1991") == 0);
    
    const char* filter = "xxx";
    assert(zmq_setsockopt(subscriber2, ZMQ_SUBSCRIBE, filter, strlen(filter)) == 0);
    
    while(true){
        zmq_msg_t msg;
        zmq_msg_init(&msg);
        zmq_msg_recv(&msg, subscriber2, 0);
        printf("subscriber2 get msg: %s\n", zmq_msg_data(&msg));
        zmq_msg_close(&msg);
    }
    
    zmq_close(subscriber2);
    zmq_ctx_destroy(context);
    return 0;
}

运行:

./objs/pub-sub-bridge-publisher
./objs/pub-sub-bridge-subscriber
./objs/pub-sub-bridge-forwarder
./objs/pub-sub-bridge-subscriber2


你可能感兴趣的:(zmq: transport bridging, the pub-sub forward proxy)