手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux编程之从零开始搭建RPC分布式系统

[日期:2017-02-09] 来源:Linux社区  作者:skyfsm [字体: ]
3.rpcgen -Sc -o my_client.c my.x 生成my_client.c
使用该指令后我们就生成了客户端.c文件,这个文件很重要,因为以后我们做业务开发就在这里做,我们的调用都会从这里开始。
 
 my_client.c:
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "my.h"


void
my_rpc_prog_1(char *host)
{
    CLIENT *clnt;
    int  *result_1;
    my_io_data_t  my_rpcc_1_arg;

#ifndef    DEBUG
    clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS1, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif    /* DEBUG */

    result_1 = my_rpcc_1(&my_rpcc_1_arg, clnt);
    if (result_1 == (int *) NULL) {
        clnt_perror (clnt, "call failed");
    }
#ifndef    DEBUG
    clnt_destroy (clnt);
#endif     /* DEBUG */
}


void
my_rpc_prog_2(char *host)
{
    CLIENT *clnt;
    my_io_data_t  *result_1;
    my_io_data_t  my_rpcc_2_arg;

#ifndef    DEBUG
    clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS2, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif    /* DEBUG */

    result_1 = my_rpcc_2(&my_rpcc_2_arg, clnt);
    if (result_1 == (my_io_data_t *) NULL) {
        clnt_perror (clnt, "call failed");
    }
#ifndef    DEBUG
    clnt_destroy (clnt);
#endif     /* DEBUG */
}


int
main (int argc, char *argv[])
{
    char *host;

    if (argc < 2) {
        printf ("usage: %s server_host\n", argv[0]);
        exit (1);
    }
    host = argv[1];
    my_rpc_prog_1 (host);
    my_rpc_prog_2 (host);
exit (0);
}
 
现在我们就可以在该文件编写客户端的代码了。
 
5.rpcgen -Ss -o my_server.c my.x生成文件my_server.c
使用该指令后我们就生成了服务器.c文件,这个文件很重要,因为以后我们做业务开发就在这里做,我们将在这里编写处理客户端请求的代码。
my_server.c:
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "my.h"

int *
my_rpcc_1_svc(my_io_data_t *argp, struct svc_req *rqstp)
{
    static int  result;

    /*
     * insert server code here
     */

    return &result;
}

my_io_data_t *
my_rpcc_2_svc(my_io_data_t *argp, struct svc_req *rqstp)
{
    static my_io_data_t  result;

    /*
     * insert server code here
     */

    return &result;
}

6.在my_server.c和my_client.c添加测试代码

 
所有利用rpcgen生成的文件都已经生成完毕,接下来我们需要添加测试代码来验证该RPC骨架是否正常工作。
my_client.c:
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "my.h"


void
my_rpc_prog_1(char *host)
{
    CLIENT *clnt;
    int  *result_1;
    my_io_data_t  my_rpcc_1_arg;

#ifndef    DEBUG
    clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS1, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif    /* DEBUG */

    result_1 = my_rpcc_1(&my_rpcc_1_arg, clnt);
    if (result_1 == (int *) NULL) {
        clnt_perror (clnt, "call failed");
    }
#ifndef    DEBUG
    clnt_destroy (clnt);
#endif     /* DEBUG */
}


void
my_rpc_prog_2(char *host)
{
    CLIENT *clnt;
    my_io_data_t  *result_1;
    my_io_data_t  my_rpcc_2_arg;

#ifndef    DEBUG
    clnt = clnt_create (host, MY_RPC_PROG, MY_RPC_VERS2, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
#endif    /* DEBUG */
    my_rpcc_2_arg.mtype = 3;
    my_rpcc_2_arg.len = 18;
    result_1 = my_rpcc_2(&my_rpcc_2_arg, clnt);
    if (result_1 == (my_io_data_t *) NULL) {
        clnt_perror (clnt, "call failed");
    }
    fprintf(stderr,"recv msg from server! mtype:%d  len:%d \n",result_1->mtype,result_1->len); 
#ifndef    DEBUG
    clnt_destroy (clnt);
#endif     /* DEBUG */
}


int
main (int argc, char *argv[])
{
    char *host;

    if (argc < 2) {
        printf ("usage: %s server_host\n", argv[0]);
        exit (1);
    }
    host = argv[1];
    //my_rpc_prog_1 (host);
    my_rpc_prog_2 (host);
exit (0);
}

值得注意的是,我们client使用的是UDP协议,当然我们用户也可以根据自己需要选用TCP协议进行开发。

my_server.c

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "my.h"

int *
my_rpcc_1_svc(my_io_data_t *argp, struct svc_req *rqstp)
{
    static int  result;

    /*
     * insert server code here
     */
    return &result;
}

my_io_data_t *
my_rpcc_2_svc(my_io_data_t *argp, struct svc_req *rqstp)
{
    static my_io_data_t  result;

    /*
     * insert server code here
     */
    printf("recv msg from client! len:%d, mt:%d \n",argp->len,argp->mtype);
    result.mtype = 33;
    result.len = 12;
    return &result;
}
 
7.测试现象
编译这client和server
 
gcc -o client my_clnt.c my_client.c my_xdr.c
gcc -o server my_svc.c my_server.c my_xdr.c
 
我在主机172.0.5.183运行server程序,在172.0.5.183运行client程序,测试现象如下:
 
server端

client端

以上测试已经证明了我们创建的RPC是可以正常通信的,那我们继续在此框架下完善代码,构建出可供业务开发的分布式系统的系统框架。
linux
相关资讯       Linux编程  RPC分布式系统 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款