ZMQ专题学习之六:libzmq的订阅代理模式通信方式

ZMQ专题学习之六:libzmq的订阅代理模式通信方式

 接上一节,前面几个专题,分别介绍了三个主要模式:request-reply、publisher-subscribe、pull-push管道模式的几种应用,本节开始,将要介绍订阅代理的模式的实例,这一节就是要将订阅代理模式的实例进行详细的说明。

    订阅代理的模式图如下:

第一步:先建立工程文件,与上一节一样,建立一个testLibzmqPSProxyServer工程,以控制台的项目方式建立。

第二步:参照上节论述,在建立好的工程文件夹中建立两个目录,include和lib两个文件夹。

然后分别把 zmq.h 和zmq_utils.h拷贝到includes文件夹,libzmq.lib和libzmq.dll拷贝到lib文件夹中。(具体从哪里考,这里就不啰嗦了,对于熟悉vs环境的人,一点就透)。

第三步:设置附加包含目录、附加链接库,调试过dll的人都懂的,不再赘述。

第四步:在建立的工程文件的cpp的文件中,添加如下代码;

// testLibzmqPSProxyServer.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h> 
#include <string.h>
#include <time.h> 
#include <assert.h>
#include <stdio.h>

#include "../include/zmq.h"
//#include "../include/zhelpers.h"

static int s_send (void *socket, char *string) {
    int size = zmq_send (socket, string, strlen (string), 0);
    return size;
}


int main (void)
{
    //  Prepare our context and publisher
    void *context = zmq_ctx_new ();
    void *publisher = zmq_socket (context, ZMQ_PUB);
    int rc = zmq_bind (publisher, "tcp://*:5557");
    assert (rc == 0);

    //  Initialize random number generator
    //srandom ((unsigned) time (NULL));
    srand(time(NULL));
    while (1) {
        //  Get values that will fool the boss
        int zipcode, temperature, relhumidity;

        zipcode  =  (100000) * rand() / (RAND_MAX + 1.0) + 1;
        temperature =  (215) * rand() / (RAND_MAX + 1.0)  - 80;
        relhumidity = (59) * rand() / (RAND_MAX + 1.0) + 10;
        //  Send message to all subscribers
        char update [20];
        sprintf (update, "%05d %d %d", zipcode, temperature, relhumidity);
        printf("send message : %s\n", update);
        s_send(publisher,update);
    }
    zmq_close (publisher);
    zmq_ctx_destroy (context);
    return 0;
}

第五步:编译后,会生成对应版本的,比如是release版本还是debug版本,32位,还是64位版本的exe。(本文为64位)

第六步:建立proxy的工程文件一个testLibzmqPSProxy工程,以控制台的项目方式建立。

第七步:同样的操作,在建立好的工程文件夹中建立两个目录,include和lib两个文件夹。

然后分别把 zmq.h 和zmq_utils.h拷贝到includes文件夹,libzmq.lib和libzmq.dll拷贝到lib文件夹中。(具体从哪里考,这里就不啰嗦了,对于熟悉vs环境的人,一点就透)。

第八步:设置附加包含目录、附加链接库,调试过dll的人都懂的,不再赘述。

第九步:在建立的工程文件的cpp的文件中,添加如下代码;

// testLibzmqPSProxy.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h> 
#include <string.h>
#include <time.h> 
#include <assert.h>
#include <stdio.h>

#include "../include/zmq.h"

int main (void)
{
    void *context = zmq_ctx_new ();

    //  This is where the weather server sits
    void *frontend = zmq_socket (context, ZMQ_XSUB);
    int rc =zmq_connect (frontend, "tcp://localhost:5557");
    assert (rc == 0);
    //  This is our public endpoint for subscribers
    void *backend = zmq_socket (context, ZMQ_XPUB);
   rc = zmq_bind (backend, "tcp://127.0.0.1:8111");
   assert (rc == 0);

    //  Run the proxy until the user interrupts us
    printf("zmq_proxy...begin \n");
    zmq_proxy (frontend, backend, NULL);
    printf("zmq_proxy...end \n");
    zmq_close (frontend);
    zmq_close (backend);
    zmq_ctx_destroy (context);
    return 0;
}
 

第十步:编译后,会生成对应版本的,比如是release版本还是debug版本,32位,还是64位版本的exe。(本文为64位)

第十一步:建立client的工程文件一个testLibzmqPSProxyClient工程,以控制台的项目方式建立。

第十二步:同样的操作,在建立好的工程文件夹中建立两个目录,include和lib两个文件夹。

然后分别把 zmq.h 和zmq_utils.h拷贝到includes文件夹,libzmq.lib和libzmq.dll拷贝到lib文件夹中。(具体从哪里考,这里就不啰嗦了,对于熟悉vs环境的人,一点就透)。

第十三步:设置附加包含目录、附加链接库,调试过dll的人都懂的,不再赘述。

第十四步:在建立的工程文件的cpp的文件中,添加如下代码;

// testLibzmqSink.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <stdlib.h> 
#include <string.h>
#include <time.h> 
#include <assert.h>
 
#include "../include/zmq.h"

int s_send (void *socket, char *string) {
    int size = zmq_send (socket, string, strlen (string), 0);
    return size;
}

char *s_recv (void *socket) {
    char buffer [256];
    int size = zmq_recv (socket, buffer, 255, 0);
    if (size == -1)
        return NULL;
    buffer[size] = '\0';

    return buffer;
}

int main (void) 
{
    //  [0]准备上下文和套接字
    void *context = zmq_ctx_new ();
    void *receiver = zmq_socket (context, ZMQ_PULL);
    zmq_bind (receiver, "tcp://*:5558");
    printf("Start Sink!\n");
    //  [1]等待开始信号
    char string[20];

    strcpy(string,s_recv (receiver));
    printf("recive %s\n",string);

    //  [2]开始计时
   
    time_t t;
    t=time(NULL);
    printf("start time %s\n",ctime(&t));
    //  [3]确定任务处理
    int task_nbr;
    for (task_nbr = 0; task_nbr < 100; task_nbr++) {
         strcpy(string,s_recv (receiver));
         printf("sink recieve: %s\n",string);
      
    }
    //  Calculate and report duration of batch
    printf ("Total elapsed time: msec\n"); 
    t=time(NULL);
    printf("%s\n",ctime(&t));

    zmq_close (receiver);
    zmq_ctx_destroy (context);
    return 0;
}

第十五步:编译后,会生成对应版本的,比如是release版本还是debug版本,32位,还是64位版本的exe。(本文为64位)

至此,已经把三个样例工程文件编译完毕,分别在各自的release或者debug目录执行exe文件,执行界面如下:

Server的显示界面

proxy的显示界面

Client的显示界面

 

上面三个图显示了发布订阅代理模式三个程序的相互通信的过程。

demo下载的地址:https://download.csdn.net/download/jyl_sh/12409867

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jyl_sh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值