2009-03-30 21:35:00 +00:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2007-03-05 04:48:48 +00:00
|
|
|
// 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 <protocol/TBinaryProtocol.h>
|
|
|
|
#include <server/TThreadPoolServer.h>
|
2007-03-07 05:17:25 +00:00
|
|
|
#include <server/TThreadedServer.h>
|
2007-03-05 04:48:48 +00:00
|
|
|
#include <transport/TServerSocket.h>
|
|
|
|
#include <transport/TTransportUtils.h>
|
|
|
|
#include <thrift/concurrency/Monitor.h>
|
|
|
|
#include <thrift/concurrency/ThreadManager.h>
|
|
|
|
#include <thrift/concurrency/PosixThreadFactory.h>
|
|
|
|
|
2007-03-07 05:17:25 +00:00
|
|
|
using boost::shared_ptr;
|
2009-01-31 22:36:20 +00:00
|
|
|
using namespace apache::thrift;
|
|
|
|
using namespace apache::thrift::protocol;
|
|
|
|
using namespace apache::thrift::transport;
|
|
|
|
using namespace apache::thrift::server;
|
|
|
|
using namespace apache::thrift::concurrency;
|
2007-03-05 04:48:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadsTestHandler : virtual public ThreadsTestIf {
|
|
|
|
public:
|
|
|
|
ThreadsTestHandler() {
|
|
|
|
// Your initialization goes here
|
|
|
|
}
|
|
|
|
|
2007-03-05 05:40:37 +00:00
|
|
|
int32_t threadOne(const int32_t sleep) {
|
2007-03-05 04:48:48 +00:00
|
|
|
// Your implementation goes here
|
|
|
|
printf("threadOne\n");
|
|
|
|
go2sleep(1, sleep);
|
2007-03-05 05:40:37 +00:00
|
|
|
return 1;
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 05:40:37 +00:00
|
|
|
int32_t threadTwo(const int32_t sleep) {
|
2007-03-05 04:48:48 +00:00
|
|
|
// Your implementation goes here
|
|
|
|
printf("threadTwo\n");
|
|
|
|
go2sleep(2, sleep);
|
2007-03-05 05:40:37 +00:00
|
|
|
return 1;
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 05:40:37 +00:00
|
|
|
int32_t threadThree(const int32_t sleep) {
|
2007-03-05 04:48:48 +00:00
|
|
|
// Your implementation goes here
|
|
|
|
printf("threadThree\n");
|
|
|
|
go2sleep(3, sleep);
|
2007-03-05 05:40:37 +00:00
|
|
|
return 1;
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 05:40:37 +00:00
|
|
|
int32_t threadFour(const int32_t sleep) {
|
2007-03-05 04:48:48 +00:00
|
|
|
// Your implementation goes here
|
|
|
|
printf("threadFour\n");
|
|
|
|
go2sleep(4, sleep);
|
2007-03-05 05:40:37 +00:00
|
|
|
return 1;
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 05:40:37 +00:00
|
|
|
int32_t stop() {
|
2007-03-05 04:48:48 +00:00
|
|
|
printf("stop\n");
|
|
|
|
server_->stop();
|
2007-03-05 05:40:37 +00:00
|
|
|
return 1;
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setServer(boost::shared_ptr<TServer> 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);
|
2007-05-22 23:59:54 +00:00
|
|
|
try {
|
|
|
|
m.wait(1000);
|
|
|
|
} catch(TimedOutException& e) {
|
|
|
|
}
|
2007-03-05 04:48:48 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "THREAD %d DONE\n", thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
boost::shared_ptr<TServer> server_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int port = 9090;
|
|
|
|
shared_ptr<ThreadsTestHandler> handler(new ThreadsTestHandler());
|
|
|
|
shared_ptr<TProcessor> processor(new ThreadsTestProcessor(handler));
|
|
|
|
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
|
|
|
|
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
|
|
|
|
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
|
|
|
|
|
2007-03-07 05:17:25 +00:00
|
|
|
/*
|
2008-02-06 22:18:40 +00:00
|
|
|
shared_ptr<ThreadManager> threadManager =
|
2007-03-05 04:48:48 +00:00
|
|
|
ThreadManager::newSimpleThreadManager(10);
|
|
|
|
shared_ptr<PosixThreadFactory> threadFactory =
|
|
|
|
shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
|
|
|
|
threadManager->threadFactory(threadFactory);
|
|
|
|
threadManager->start();
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2007-03-07 05:17:25 +00:00
|
|
|
shared_ptr<TServer> server =
|
2007-03-05 04:48:48 +00:00
|
|
|
shared_ptr<TServer>(new TThreadPoolServer(processor,
|
|
|
|
serverTransport,
|
|
|
|
transportFactory,
|
|
|
|
protocolFactory,
|
|
|
|
threadManager));
|
2007-03-07 05:17:25 +00:00
|
|
|
*/
|
2007-03-05 04:48:48 +00:00
|
|
|
|
2007-03-07 05:17:25 +00:00
|
|
|
shared_ptr<TServer> server =
|
|
|
|
shared_ptr<TServer>(new TThreadedServer(processor,
|
|
|
|
serverTransport,
|
|
|
|
transportFactory,
|
|
|
|
protocolFactory));
|
2007-03-05 04:48:48 +00:00
|
|
|
|
2007-03-07 05:17:25 +00:00
|
|
|
handler->setServer(server);
|
|
|
|
|
|
|
|
server->serve();
|
2007-03-05 04:48:48 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "done.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|