Advertisement
Guest User

ThriftWindowsTest

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