// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TThreadedServer.h>

#include "Test.h"

using namespace apache::thrift;
using namespace apache::thrift::stdcxx;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;

class TestHandler : virtual public TestIf {
    private:
        int32_t counter;
    public:
        TestHandler() { counter = 0; }
        int32_t add(const int32_t a, const int32_t b) { return a + b; }
        void dup(std::string& _return, const std::string& s) { _return = s + s; }
        void process(Data& _return, const Data& d) { _return.iv = d.iv + 1; _return.sv = d.sv + "X"; }
        int32_t getCounter() { counter++; return counter; }
        void noop() { }
};

const int PORT = 12348;

int main()
{
    TThreadedServer server(make_shared<TestProcessor>(make_shared<TestHandler>()),
                           make_shared<TServerSocket>(PORT),
                           make_shared<TBufferedTransportFactory>(),
                           make_shared<TBinaryProtocolFactory>());
    server.serve();
    return 0;
}
