Skip to content

Instantly share code, notes, and snippets.

@errzey
Last active August 29, 2015 14:20
Show Gist options
  • Save errzey/01150710fe6001dfe9c5 to your computer and use it in GitHub Desktop.
Save errzey/01150710fe6001dfe9c5 to your computer and use it in GitHub Desktop.
Using the libevhtp thrift module

Using the libevhtp thrift module

Just copy all this stuff to a directory (compile thrift with the libevhtp module of course first)

mkdir build; cd build; cmake .. ; make
./evhtp_thrift_server &
# if you have SO_REUSEPORT support, you can run multiple times.
./test1_client
cmake_minimum_required(VERSION 2.8)
project(evhtp_thrift_test)
set(Test1ThriftSRCS
${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/test1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/test1_types.cpp
${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp/test1_constants.cpp
)
add_custom_command(
OUTPUT ${Test1ThriftSRCS}
COMMAND thrift
ARGS -r --gen cpp:cob_style -o ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/test1.thrift
COMMENT "thrift2cpp"
)
include_directories("/usr/local/include/evhtp")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gen-cpp /usr/local/include/thrift)
add_executable(evhtp_thrift_server evhtp_thrift_server.cpp ${Test1ThriftSRCS})
target_link_libraries(test1_server thriftnbd thriftd)
add_executable(test1_client test1_client.cpp ${Test1ThriftSRCS})
target_link_libraries(test1_client thriftd)
#include "test1.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/async/TAsyncProtocolProcessor.h>
#include <thrift/async/TEvhtpServer.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 ::test1;
class test1Handler : virtual public test1If {
public:
test1Handler() {
printf("test1Handler()\n");
}
int32_t
add_val(const std::string& key, const std::string& val) {
printf("add_val %s %s\n", key.c_str(), val.c_str());
return 0;
}
void
find_val(std::string& _return, const std::string& key) {
printf("find_val\n");
}
void
find_val_struct(Test1& _return, const std::string& key) {
printf("find_val_struct\n");
}
};
class test1AsyncHandler : public test1CobSvIf {
public:
test1AsyncHandler() {
syncHandler_ = std::auto_ptr<test1Handler>(new test1Handler);
printf("test1AsyncHandler()\n");
}
void
add_val(tcxx::function <void(int32_t const& _ret)>cob,
const std::string& key,
const std::string& val) {
int32_t _ret = 0;
_ret = syncHandler_->add_val(key, val);
return cob(_ret);
}
void
find_val(tcxx::function <void(std::string const & _ret)>cob,
const std::string & key) {
std::string _ret;
syncHandler_->find_val(_ret, key);
return cob(_ret);
}
void
find_val_struct(tcxx::function<void(Test1 const& _ret)>cob,
const std::string& key) {
Test1 _ret;
syncHandler_->find_val_struct(_ret, key);
return cob(_ret);
}
protected:
std::auto_ptr<test1Handler> syncHandler_;
};
int
main(int argc, char ** argv) {
shared_ptr<test1AsyncHandler> handler(new test1AsyncHandler());
shared_ptr<TAsyncProcessor> proc(new test1AsyncProcessor(handler));
shared_ptr<TProtocolFactory> pfact(new TBinaryProtocolFactory());
shared_ptr<TAsyncBufferProcessor> bufproc(new TAsyncProtocolProcessor(proc, pfact));
shared_ptr<TEvhtpServer> server(new TEvhtpServer(bufproc, 9090));
server->serve();
return 0;
}
namespace cpp test1
struct Test1 {
1 : string foo;
2 : string bar;
}
service test1 {
i32 add_val(1 : string key, 2 : string val);
string find_val(1 : string key);
Test1 find_val_struct(1 : string key);
}
#include <transport/THttpClient.h>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace ::test1;
int
main(int argc, char ** argv) {
boost::shared_ptr<TTransport> transport(new THttpClient("127.0.0.1", 9090, "/"));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
test1Client client(protocol);
transport->open();
{
printf("%d\n", client.add_val("a", "b"));
}
transport->close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment