uh, a kinda implementation
authorOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 02:12:46 +0000 (21:12 -0500)
committerOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 02:12:46 +0000 (21:12 -0500)
database/gen-cpp/PosBookie_server.skeleton.cpp

index 042e916..3119406 100644 (file)
@@ -3,10 +3,16 @@
 
 #include "PosBookie.h"
 #include <thrift/protocol/TBinaryProtocol.h>
-#include <thrift/server/TSimpleServer.h>
+#include <thrift/server/TNonblockingServer.h>
 #include <thrift/transport/TServerSocket.h>
 #include <thrift/transport/TBufferTransports.h>
 
+#include "Pos.h"
+#include <thrift/transport/TSocket.h>
+#include <thrift/transport/TTransportUtils.h>
+
+#include <stdio.h>
+
 using namespace ::apache::thrift;
 using namespace ::apache::thrift::protocol;
 using namespace ::apache::thrift::transport;
@@ -17,47 +23,76 @@ using boost::shared_ptr;
 using namespace  ::posbookie;
 
 class PosBookieHandler : virtual public PosBookieIf {
+
+  shared_ptr<TTransport> socket;
+  shared_ptr<TTransport> transport;
+  shared_ptr<TProtocol> protocol;
+  PosClient client;
+
  public:
-  PosBookieHandler() {
-    // Your initialization goes here
+  PosBookieHandler(char* serverHost, int serverPort)
+    : socket(new TSocket(serverHost, serverPort))
+    , transport(new TFramedTransport(socket))
+    , protocol(new TBinaryProtocol(transport))
+    , client(protocol)
+  {
+    try{
+      this->client.ping();
+    } catch {
+      printf("failed to contact server");
+      exit(1);
+    }
   }
 
-  void ping() {
-    // Your implementation goes here
-    printf("ping\n");
-  }
+  void ping() { }
 
   void getSalt(std::string& _return) {
-    // Your implementation goes here
-    printf("getSalt\n");
+    this->client.getSalt(_return);
   }
 
   E_PURCHASE_STATUS::type purchaseItems(const std::string& auth, const std::string& dataToHash, const std::vector<int64_t> & upcs) {
-    // Your implementation goes here
-    printf("purchaseItems\n");
+    try {
+      int32_t total_price = 0;
+      std::vector<int64_t>::iterator i;
+      for(i = upcs.begin(); i != upcs.end(); i++) {
+        total_price += this->getUPCPrice(*i);
+      }
+
+      int32_t accountBalance = this->client.getHashAccountBalance(dataToHash);
+      if(accountBalance < total_price) {
+        this->client.performTransactionOnHash("AUTH", dataToHash, total_price * -1);
+        return E_PURCHASE_STATUS::type::EPS_SUCCESS;
+      }
+
+      return E_PURCHASE_STATUS::type::EPS_NOMONEY;
+    } catch(...) {
+      return E_PURCHASE_STATUS::type::EPS_FAIL;
+    }
   }
 
   int32_t getUPCPrice(const int64_t upc) {
-    // Your implementation goes here
-    printf("getUPCPrice\n");
+    return this->client.getUPCPrice(upc);
   }
 
   int32_t getStock(const int64_t upc) {
-    // Your implementation goes here
-    printf("getStock\n");
+    return this->client.getStock(upc);
   }
 
 };
 
 int main(int argc, char **argv) {
-  int port = 9090;
-  shared_ptr<PosBookieHandler> handler(new PosBookieHandler());
+  if(argc != 4) {
+    printf("usage: posbookie <serverhost> <serverport> <listenport>\n");
+    exit(1);
+  }
+
+  int server_port = STRTOINT(argv[2]);
+  int listen_port = STRTOINT(argv[3]);
+
+  shared_ptr<PosBookieHandler> handler(new PosBookieHandler(argv[1], server_port));
   shared_ptr<TProcessor> processor(new PosBookieProcessor(handler));
-  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
-  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
-  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
 
-  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
+  TNonblockingServer server(processor, listen_port);
   server.serve();
   return 0;
 }