blob: 4b7fcaf1765c09e5d43a0706b68e10976f9946f0 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#include <concurrency/ThreadManager.h>
2#include <concurrency/PosixThreadFactory.h>
3#include <protocol/TBinaryProtocol.h>
4#include <server/TSimpleServer.h>
5#include <server/TThreadPoolServer.h>
6#include <server/TThreadedServer.h>
7#include <transport/TServerSocket.h>
8#include <transport/TTransportUtils.h>
9
10#include <iostream>
11#include <stdexcept>
12#include <sstream>
13
14#include "../gen-cpp/Calculator.h"
15
16using namespace std;
17using namespace facebook::thrift;
18using namespace facebook::thrift::protocol;
19using namespace facebook::thrift::transport;
20using namespace facebook::thrift::server;
21
22using namespace tutorial;
23using namespace shared;
24
25using namespace boost;
26
27class CalculatorHandler : public CalculatorIf {
28 public:
29 CalculatorHandler() {}
30
31 void ping() {
32 printf("ping()\n");
33 }
34
35 int32_t add(const int32_t n1, const int32_t n2) {
36 printf("add(%d,%d)\n", n1, n2);
37 return n1 + n2;
38 }
39
40 int32_t calculate(const int32_t logid, const Work &work) {
41 printf("calculate(%d,{%d,%d,%d})\n", logid, work.op, work.num1, work.num2);
42 int32_t val;
43
44 switch (work.op) {
45 case ADD:
46 val = work.num1 + work.num2;
47 break;
48 case SUBTRACT:
49 val = work.num1 - work.num2;
50 break;
51 case MULTIPLY:
52 val = work.num1 * work.num2;
53 break;
54 case DIVIDE:
55 if (work.num2 == 0) {
56 InvalidOperation io;
57 io.what = work.op;
58 io.why = "Cannot divide by 0";
59 throw io;
60 }
61 val = work.num1 / work.num2;
62 break;
63 default:
64 InvalidOperation io;
65 io.what = work.op;
66 io.why = "Invalid Operation";
67 throw io;
68 }
69
70 SharedStruct ss;
71 ss.key = logid;
72 ss.value = "5";
73
74 log[logid] = ss;
75
76 return val;
77 }
78
79 void getStruct(SharedStruct &ret, const int32_t logid) {
80 printf("getStruct(%d)\n", logid);
81 ret = log[logid];
82 }
83
84 void zip() {
85 printf("zip()\n");
86 }
87
88protected:
89 map<int32_t, SharedStruct> log;
90
91};
92
93int main(int argc, char **argv) {
94
95 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
96 shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
97 shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
98 shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
99 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
100
101 TSimpleServer server(processor,
102 serverTransport,
103 transportFactory,
104 protocolFactory);
105
106
107 /**
108 * Or you could do one of these
109
110 shared_ptr<ThreadManager> threadManager =
111 ThreadManager::newSimpleThreadManager(workerCount);
112 shared_ptr<PosixThreadFactory> threadFactory =
113 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
114 threadManager->threadFactory(threadFactory);
115 threadManager->start();
116 TThreadPoolServer server(processor,
117 serverTransport,
118 transportFactory,
119 protocolFactory,
120 threadManager);
121
122 TThreadedServer server(processor,
123 serverTransport,
124 transportFactory,
125 protocolFactory);
126
127 */
128
129 printf("Starting the server...\n");
130 server.serve();
131 printf("done.\n");
132 return 0;
133}