blob: 3be19c0bd98af183f0d3d78eee0ffaaa2f4d5aaa [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;
Mark Slee0f2916c2007-04-11 09:16:23 +000072 char buffer[11];
73 sprintf(buffer, "%d", val);
74 ss.value = buffer;
Mark Slee07a3aab2007-03-07 05:45:10 +000075
76 log[logid] = ss;
77
78 return val;
79 }
80
81 void getStruct(SharedStruct &ret, const int32_t logid) {
82 printf("getStruct(%d)\n", logid);
83 ret = log[logid];
84 }
85
86 void zip() {
87 printf("zip()\n");
88 }
89
90protected:
91 map<int32_t, SharedStruct> log;
92
93};
94
95int main(int argc, char **argv) {
96
97 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
98 shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
99 shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
100 shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
101 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
102
103 TSimpleServer server(processor,
104 serverTransport,
105 transportFactory,
106 protocolFactory);
107
108
109 /**
110 * Or you could do one of these
111
112 shared_ptr<ThreadManager> threadManager =
113 ThreadManager::newSimpleThreadManager(workerCount);
114 shared_ptr<PosixThreadFactory> threadFactory =
115 shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
116 threadManager->threadFactory(threadFactory);
117 threadManager->start();
118 TThreadPoolServer server(processor,
119 serverTransport,
120 transportFactory,
121 protocolFactory,
122 threadManager);
123
124 TThreadedServer server(processor,
125 serverTransport,
126 transportFactory,
127 protocolFactory);
128
129 */
130
131 printf("Starting the server...\n");
132 server.serve();
133 printf("done.\n");
134 return 0;
135}