From b32f3c6ad76536e6a8e334064fb09f8c6ddebb9e Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Mon, 5 Mar 2007 04:48:48 +0000 Subject: [PATCH] Threading libraries test for Thrift C++ git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665041 13f79535-47bb-0310-9956-ffa450edef68 --- test/threads/Makefile | 46 ++++++++++++++ test/threads/ThreadsServer.cpp | 104 ++++++++++++++++++++++++++++++++ test/threads/ThreadsTest.thrift | 11 ++++ 3 files changed, 161 insertions(+) create mode 100644 test/threads/Makefile create mode 100644 test/threads/ThreadsServer.cpp create mode 100755 test/threads/ThreadsTest.thrift diff --git a/test/threads/Makefile b/test/threads/Makefile new file mode 100644 index 00000000..79c0134c --- /dev/null +++ b/test/threads/Makefile @@ -0,0 +1,46 @@ +# Makefile for Thrift test project. +# +# Author: +# Mark Slee + +# Default target is everything + +ifndef thrift_home +thrift_home=../../ +endif #thrift_home + +target: all + +ifndef boost_home +boost_home=/usr/local/include/boost-1_33_1 +endif #boost_home +target: all + +include_paths = $(thrift_home)/lib/cpp/src \ + $(boost_home) + +include_flags = $(patsubst %,-I%, $(include_paths)) + +# Tools +ifndef THRIFT +THRIFT = ../../compiler/cpp/thrift +endif # THRIFT + +CC = g++ +LD = g++ + +# Compiler flags +LFL = -L$(thrift_home)/lib/cpp/.libs -lthrift +CCFL = -Wall -O3 -g -I./gen-cpp $(include_flags) +CFL = $(CCFL) $(LFL) + +all: server + +stubs: ThreadsTest.thrift + $(THRIFT) -cpp -py ThreadsTest.thrift + +server: stubs + g++ -o ThreadsServer $(CFL) ThreadsServer.cpp ./gen-cpp/ThreadsTest.cpp ./gen-cpp/ThreadsTest_types.cpp + +clean: + rm -fr *.o ThreadsServer gen-cpp diff --git a/test/threads/ThreadsServer.cpp b/test/threads/ThreadsServer.cpp new file mode 100644 index 00000000..1e3494cc --- /dev/null +++ b/test/threads/ThreadsServer.cpp @@ -0,0 +1,104 @@ +// This autogenerated skeleton file illustrates how to build a server. +// You should copy it to another filename to avoid overwriting it. + +#include "ThreadsTest.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace facebook::thrift; +using namespace facebook::thrift::protocol; +using namespace facebook::thrift::transport; +using namespace facebook::thrift::server; +using namespace facebook::thrift::concurrency; + + +class ThreadsTestHandler : virtual public ThreadsTestIf { + public: + ThreadsTestHandler() { + // Your initialization goes here + } + + void threadOne(const int32_t sleep) { + // Your implementation goes here + printf("threadOne\n"); + go2sleep(1, sleep); + } + + void threadTwo(const int32_t sleep) { + // Your implementation goes here + printf("threadTwo\n"); + go2sleep(2, sleep); + } + + void threadThree(const int32_t sleep) { + // Your implementation goes here + printf("threadThree\n"); + go2sleep(3, sleep); + } + + void threadFour(const int32_t sleep) { + // Your implementation goes here + printf("threadFour\n"); + go2sleep(4, sleep); + } + + void stop() { + printf("stop\n"); + server_->stop(); + } + + void setServer(boost::shared_ptr server) { + server_ = server; + } + +protected: + void go2sleep(int thread, int seconds) { + Monitor m; + for (int i = 0; i < seconds; ++i) { + fprintf(stderr, "Thread %d: sleep %d\n", thread, i); + m.wait(1000); + } + fprintf(stderr, "THREAD %d DONE\n", thread); + } + +private: + boost::shared_ptr server_; + +}; + +int main(int argc, char **argv) { + int port = 9090; + shared_ptr handler(new ThreadsTestHandler()); + shared_ptr processor(new ThreadsTestProcessor(handler)); + shared_ptr serverTransport(new TServerSocket(port)); + shared_ptr transportFactory(new TBufferedTransportFactory()); + shared_ptr protocolFactory(new TBinaryProtocolFactory()); + + shared_ptr threadManager = + ThreadManager::newSimpleThreadManager(10); + shared_ptr threadFactory = + shared_ptr(new PosixThreadFactory()); + threadManager->threadFactory(threadFactory); + threadManager->start(); + + shared_ptr threadPoolServer = + shared_ptr(new TThreadPoolServer(processor, + serverTransport, + transportFactory, + protocolFactory, + threadManager)); + + handler->setServer(threadPoolServer); + + threadPoolServer->serve(); + + fprintf(stderr, "done.\n"); + + return 0; +} + diff --git a/test/threads/ThreadsTest.thrift b/test/threads/ThreadsTest.thrift new file mode 100755 index 00000000..ebf2513e --- /dev/null +++ b/test/threads/ThreadsTest.thrift @@ -0,0 +1,11 @@ +#!/usr/local/bin/thrift -cpp -py + +service ThreadsTest { + void threadOne(1: i32 sleep=15), + void threadTwo(2: i32 sleep=15), + void threadThree(3: i32 sleep=15), + void threadFour(4: i32 sleep=15) + + void stop(); + +} -- 2.17.1