VSOA开发
本节内容介绍几种基础通信接口在 AutoCode 中的使用方法。AutoCode 实现了 VSOA RPC(CALL),订阅发布(PUB-SUB), 数据报(DATAGRAM)通信接口,AutoCode 将这些接口抽象为 VSOA 事件。
开发须知
本节示例可以从模板
demo-base
创建工程。事件资源的使用请参考 VSOA-Port 资源管理。VSOA 事件的介绍请参考开发环境。
服务模型
本实例由两个服务组成,展示 CALL,PUB,DGRAM,三种接口的使用方法。
说明:
serc1
向serc2
发起RPC的调用请求。serc2
作为服务端向serc1
发布事件。serc2
作为服务端接收到serc1
发送的数据。
serc1
组件模型与接口
serc1
组件中定义了多个定时器事件, even1
每 1 秒向 serc2
请求一次 RPC。 even2
每 1 秒向 serc2
发送一次数据。 even3
每 1 秒接收一次事件。
com1.event1
RPC call 接口发送数据。
/*
* @func: serc1_com1_event1_stimer_vreq_io
* @thread: timer
*/
ac_result_t serc1_com1_event1_stimer_vreq_io(ac_t *acs)
{
ac_result_t ret = AC_RET_OK;
bool call_ret = true;
vsoa_payload_t out;
elem_req_t _jout;
elem_req_t *jout = &_jout;
ac_payload_init(&out, NULL, 0, NULL, 0);
elem_req_init(jout);
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE_INIT
* @desc: make you code below to handle input data, init output data.
*-------------------------------------------------------------------------------------------------*/
jout->name = "call";
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE_INIT
*-------------------------------------------------------------------------------------------------*/
out.param = elem_req_json_stringify(jout);
out.param_len = strlen(out.param);
call_ret = ac_port_call(acs, "/call", VSOA_CLIENT_RPC_METHOD_GET, 0, &out);
elem_req_json_stringify_free(out.param);
ret = call_ret ? AC_RET_OK : AC_RET_BREAK;
/*---------------------------------------------------------------------------------------------------
* @begin: FREE
* @desc: make you code below to free output data if needed.
*-------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------
* @end: FREE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
com1.event2
DGRAM 接口发送数据。
/*
* @func: serc1_com1_event2_stimer_vsend_io
* @thread: timer
*/
ac_result_t serc1_com1_event2_stimer_vsend_io(ac_t *acs)
{
ac_result_t ret = AC_RET_OK;
bool port_ret = true;
vsoa_payload_t out;
elem_req_t _jout;
elem_req_t *jout = &_jout;
ac_payload_init(&out, NULL, 0, NULL, 0);
elem_req_init(jout);
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE_INIT
* @desc: make you code below to handle input data, init output data.
*-------------------------------------------------------------------------------------------------*/
jout->name = "datagaram";
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE_INIT
*-------------------------------------------------------------------------------------------------*/
out.param = elem_req_json_stringify(jout);
out.param_len = strlen(out.param);
port_ret = ac_port_datagram(acs, "/datagram", &out, FALSE);
elem_req_json_stringify_free(out.param);
ret = port_ret ? AC_RET_OK : AC_RET_BREAK;
/*---------------------------------------------------------------------------------------------------
* @begin: FREE
* @desc: make you code below to free output data if needed.
*-------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------
* @end: FREE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
com1.event3
PUB 接口中打印接收的结果。
/*
* @func: serc1_com1_event3_vsub_i
* @thread: serc2-client
*/
ac_result_t serc1_com1_event3_vsub_i(ac_t *acs, elem_req_t *param, void *data, size_t len)
{
ac_result_t ret = AC_RET_OK;
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE
* @desc: make you code below to handle input data.
*-------------------------------------------------------------------------------------------------*/
printf("Rpc pub result: %s\n", param->name);
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
serc2
组件模型与接口
serc2
组件中定义了, even1
每 1 秒向 serc2
请求一次 RPC。 even2
每 1 秒向 serc2
发送一次数据。 even3
每 1 秒接收一次事件。
com1.event1
RPC reply 接口中打印接收的结果。
/*
* @func: serc2_com1_event1_vcall_i
* @thread: serc2-service
*/
ac_result_t serc2_com1_event1_vcall_i(ac_t *acs, elem_req_t *param, void *data, size_t len)
{
ac_result_t ret = AC_RET_OK;
bool reply_ret = true;
vsoa_payload_t reply;
uint8_t status = VSOA_STATUS_SUCCESS;
elem_req_t _jreply;
elem_req_t *jreply = &_jreply;
ac_payload_init(&reply, NULL, 0, NULL, 0);
elem_req_init(jreply);
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE_INIT
* @desc: make you code below to handle input data, init output data.
*-------------------------------------------------------------------------------------------------*/
printf("Rpc call result: %s\n", param->name);
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE_INIT
*-------------------------------------------------------------------------------------------------*/
reply.param = elem_req_json_stringify(jreply);
reply.param_len = strlen(reply.param);
reply_ret = ac_port_reply(acs, status, &reply);
elem_req_json_stringify_free(reply.param);
ret = reply_ret ? AC_RET_OK : AC_RET_BREAK;
/*---------------------------------------------------------------------------------------------------
* @begin: FREE
* @desc: make you code below to free output data if needed.
*-------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------
* @end: FREE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
com1.event2
DGRAM reply 接口中打印接收的结果。
/*
* @func: serc2_com1_event2_vrecv_i
* @thread: serc2-service
*/
ac_result_t serc2_com1_event2_vrecv_i(ac_t *acs, elem_req_t *param, void *data, size_t len)
{
ac_result_t ret = AC_RET_OK;
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE
* @desc: make you code below to handle input data.
*-------------------------------------------------------------------------------------------------*/
printf("Rpc datagram result: %s\n", param->name);
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
com1.event3
PUB 接口中发送事件。
/*
* @func: serc2_com1_event3_stimer_vpub_io
* @thread: timer
*/
ac_result_t serc2_com1_event3_stimer_vpub_io(ac_t *acs)
{
ac_result_t ret = AC_RET_OK;
bool port_ret = true;
vsoa_payload_t out;
elem_req_t _jout;
elem_req_t *jout = &_jout;
ac_payload_init(&out, NULL, 0, NULL, 0);
elem_req_init(jout);
/*---------------------------------------------------------------------------------------------------
* @begin: HANDLE_INIT
* @desc: make you code below to handle input data, init output data.
*-------------------------------------------------------------------------------------------------*/
jout->name = "pub";
/*---------------------------------------------------------------------------------------------------
* @end: HANDLE_INIT
*-------------------------------------------------------------------------------------------------*/
out.param = elem_req_json_stringify(jout);
out.param_len = strlen(out.param);
port_ret = ac_port_pub(acs, "/pub", &out, FALSE);
elem_req_json_stringify_free(out.param);
ret = port_ret ? AC_RET_OK : AC_RET_BREAK;
/*---------------------------------------------------------------------------------------------------
* @begin: FREE
* @desc: make you code below to free output data if needed.
*-------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------
* @end: FREE
*-------------------------------------------------------------------------------------------------*/
return (ret);
} /* @func-end */
运行结果
serc1
中运行结果如下图所示。
serc2
中运行结果如下图所示。