thrift-异步client&server使用例子
来源:互联网 发布:推倒女神的体验知乎 编辑:程序博客网 时间:2024/06/07 12:56
研究完异步client&server源码后,当然需要写个测试例子来验证一下!
同样采用test.thrift作为例子
/* @file : test.thrift */
namespace cpp thrift.example
service Twitter {
string sendString(1:string data);
}
输入如下命令,自动生成实现异步client&server所需的类:
thrift -r -strict --gen cpp:cob_style -o ./ test.thrift
用户只要关心TwitterCobClient、TwitterCobSvIf和TwitterAsyncProcessor这三个类。
一、实现异步client
/*@file: testclient.cpp*/
#include <stdio.h>
#include <getopt.h>
#include <event.h>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include <async/TEvhttpClientChannel.h>
#include "Twitter.h"
using namespace std;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::async;
using namespace ::boost;
using namespace ::thrift::example;
using ::boost::bind;
class testClient : public TwitterCobClient
{
public:
testClient(boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel, TProtocolFactory* protocolFactory)
: TwitterCobClient(channel, protocolFactory)
{ };
virtual void completed__(bool success)
{
if (success)
{
printf("respone : %s \n", res.c_str()); // 输出返回结果
}
else
{
printf("failed to respone\n");
}
fflush(0);
};
string res;
};
// callback function
static void my_recv_sendString(TwitterCobClient* client)
{
client->recv_sendString(dynamic_cast<testClient *>(client)->res);
};
static void sendString(
testClient& client
)
{
printf("sendString start\n");
function<void(TwitterCobClient* client)> cob = bind(&my_recv_sendString, _1);
client.sendString(cob, "hello"); // 发送并注册回调函数
printf("sendString end\n");
}
static void DoSimpleTest(
const std::string& host, int port
)
{
printf( "running DoSimpleTest( %s, %d) ...\n",
host.c_str(), port);
event_base* base = event_base_new();
boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel1( new TEvhttpClientChannel( host, "/", host.c_str(), port, base ) );
testClient client1( channel1, new TBinaryProtocolFactory() );
sendString(client1); // 发送第一个请求
boost::shared_ptr< ::apache::thrift::async::TAsyncChannel> channel2( new TEvhttpClientChannel( host, "/", host.c_str(), port, base ) );
testClient client2( channel2, new TBinaryProtocolFactory() );
sendString(client2); // 发送第二个请求
event_base_dispatch(base);
event_base_free(base);
printf( "done DoSimpleTest().\n" );
}
int main( int argc, char* argv[] )
{
DoSimpleTest( "172.19.101.61", 14488 );
return 0;
}
编译生成async_client_d。
二、异步server实现
/*@file: testserver.cpp*/
#include "Twitter.h"
#include <protocol/TBinaryProtocol.h>
#include <async/TEvhttpServer.h>
#include <stdio.h>
#include <getopt.h>
#include <event.h>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include <async/TAsyncProtocolProcessor.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::async;
using boost::shared_ptr;
using namespace thrift::example;
class TwitterAsyncHandler : public TwitterCobSvIf {
public:
TwitterAsyncHandler() {
// Your initialization goes here
}
void sendString(std::tr1::function<void(std::string const& _return)> cob, const std::string& data) {
printf("sendString rec:%s\n", data.c_str()); // 输出收到的数据
std::string _return = "world"; // 返回world字符串给客户端
return cob(_return);
}
};
int main(int argc, char **argv) {
shared_ptr<TAsyncProcessor> underlying_pro(new TwitterAsyncProcessor( shared_ptr<TwitterCobSvIf>(new TwitterAsyncHandler()) ) );
shared_ptr<TAsyncBufferProcessor> processor( new TAsyncProtocolProcessor( underlying_pro, shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory()) ) );
TEvhttpServer server(processor, 14488);
server.serve();
return 0;
}
编绎生成async_server_d。
三、运行async_client_d和async_server_d
(1)先运行server:
$./async_server_d
(2)然后运行async_client_d
$./async_client_d
结果输出:
(1)server输出:
sendString rec:hello
sendString rec:hello
收到两个请求,且内容为hello
(2)client输出:
running DoSimpleTest( 172.19.101.61, 14488) ...
sendString start
sendString end
sendString start
sendString end
respone : world
respone : world
done DoSimpleTest()
发出两个请求,且回复都是world。
OK,发送数据和接收数据都正确。
- thrift-异步client&server使用例子
- thrift源码研究-异步client&server
- thrift-client异步+server非阻塞
- 错误理解 thrift 异步client 和 异步server的教训
- thrift C++做server, C++,python, java做Client例子
- thrift C++做server, C++,python, java做Client例子
- thrift C++做server, C++,python, java做Client例子
- Python&Thrift--Server&Client
- Thrift使用入门(2) - 用Thrift实现一个简单的Server/Client应用程序
- Thrift tutorial(python server and php client)
- Spring集成Thrift--Server AND Client
- thrift语法及PHP-server-client demo
- Thrift c++ server & java client配置
- Thrift Server nodejs Client C# ---学习笔记
- Thrift 非阻塞异步I/O例子
- java socket client-server-client小例子
- Thrift 各种server 使用模式
- jstorm-yarn中thrift server/client的更换
- Advanced – Written Tutorials.
- 单例模式和全局变量
- Nginx配置文件nginx.conf中文详解
- Android WebView总结
- python登录baidu机器人
- thrift-异步client&server使用例子
- 对php代码优化的一些解释
- 制造企业3.0 基于流程的新的信息管理范例
- [CSS]清除浮动的三个方法
- 数据结构基础_高仿remove(value)方法
- A^B%C 快速幂模版
- HDU 3584 三维树状数组
- 监听器
- 个人笔记