title: Implementing a Server/Client with HTTP protocol with Apache Thrift
body: <p>I'm new to apache thrift. I wanted to use it to help me implement a server that accepts input from a client, parse it and based on that direct it to the proper function which in turn would return a response for that specific request.</p>
  
  <p>For example:</p>
  
  <ol>
  <li>Client sends: /get_list/1 (This should return values within list#1 from server)</li>
  <li>Server parses input to the following: get_list &amp;&amp; 1</li>
  <li>Server maps this to a get_list_req which is a struct that has only one parameter: i32_t id (which in this case would be 1)</li>
  <li>Server sends this to the getListReq function which should return get_list_resp struct which has 1 parameter: list list_content.</li>
  </ol>
  
  <p>Now I have defined the structs necessary in the mylist.thrift:</p>
  
  <pre><code>struct TGetListRequest {
      1: i32_t id,
  }
  struct TGetListResponse {
      1: string response_code, //FAIL or SUCCESS
      2: list&lt;string&gt; nodes, 
  }
  
  service TmyListService {
  
      TGetListResponse getListReq( 1:TGetListRequest arg ),
  }
  </code></pre>
  
  <p>Now this generates the necessary files:</p>
  
  <pre><code>TmyListService_server.skeleton.cpp 
  TmyListService.cpp
  TmyListService.h
  mylistconstants.cpp
  mylistconstants.h
  mylisttypes.cpp
  mylisttypes.h
  </code></pre>
  
  <p>First file's content is copied to TmyListService_server.cpp where the class is implemented with its method: getListReq as follows:</p>
  
  <pre><code>class TGetListRequestHandler: public TGetListRequestIf {
  public:
      TGetListResponse getListReq(TGetListRequest arg){
          ...
      }
  };
  </code></pre>
  
  <p>I have it provided in it the following code to start server in main() function:</p>
  
  <pre><code>int main(int argc, char **argv)  {
    int port = 9090;
    shared_ptr&lt;TmyListServiceHandler&gt; handler(new TmyListServiceHandler());
    shared_ptr&lt;TProcessor&gt; processor(new TmyListServiceProcessor(handler));
    shared_ptr&lt;TServerTransport&gt; serverTransport(new TServerSocket(port));
    shared_ptr&lt;TTransportFactory&gt; transportFactory(new  TBufferedTransportFactory());
    shared_ptr&lt;TProtocolFactory&gt; protocolFactory(new TBinaryProtocolFactory());
  
    TSimpleServer server(processor, serverTransport, transportFactory,     protocolFactory);
    server.serve();
    return 0;
  }
  </code></pre>
  
  <p>Now comes the tricky part for me:</p>
  
  <ol>
  <li><p>Where do I implement the parsing of the input coming from a client to the server? I was told that it is simple implementation using thrift without the need to write your our parser or use a parser generator? I cannot seem to figure this one out? Where am I telling my server what to do with input i.e. which Req function to direct it to?</p></li>
  <li><p>In terms of implementing the client, I have the following:</p>
  
  <pre><code>int main(int argc, char **argv) {
   boost::shared_ptr&lt;TSocket&gt; socket(new TSocket("localhost", 9090));
   boost::shared_ptr&lt;TTransport&gt; transport(new TBufferedTransport(socket));
   boost::shared_ptr&lt;TProtocol&gt; protocol(new TBinaryProtocol(transport));
  
   TmyListServiceClient client(protocol);
   TGetListRequest arg;
   arg.id = 1;
   TGetListResponse argz;
   transport-&gt;open();
   client.send_getListReq( arg );
   printf("DONE SEDNING");
   client.recv_getListReq( argz );
   transport-&gt;close();
  
  return 0;
  }
  </code></pre></li>
  </ol>
  
  <p>This basically does nothing aside from opening the client connection. It doesn't accept any input at all. I'm sure this is also related to the implementation of the processing logic within the server first, but what should I be doing here to have a client ready to be tested against the server?</p>