json server for js
authorOwen Smith <owen@omsmith.ca>
Wed, 21 Nov 2012 00:57:32 +0000 (19:57 -0500)
committerOwen Smith <owen@omsmith.ca>
Wed, 21 Nov 2012 00:57:32 +0000 (19:57 -0500)
database/client.cpp
database/gen-cpp/PosBookie_server.skeleton.cpp

index 06167d6..75f62ec 100644 (file)
@@ -54,12 +54,22 @@ int main(int argc, char** argv) {
         getline(cin,s);
         getline(cin,card);
         client.getSalt(s);
-        s = SHA1Hash(s+"balls").toHex();
+        s = SHA1Hash(s+"PSK").toHex();
         client.associateHashWithName(s, card, user);
         break;
       }
       case 2: {
-        cout << "Not implemented.\n";
+        uint64_t upc;
+               cout << "UPC: ";
+               cin >> upc;
+
+               int32_t price;
+               cout << "price: ";
+               cin >> price;
+
+               client.getSalt(s);
+               s = SHA1Hash(s+"PSK").toHex();
+               client.setUPCPrice(s, upc, price);
         break;
       }
       case 3: {
@@ -79,10 +89,10 @@ int main(int argc, char** argv) {
           break;
         }
         client.getSalt(s);
-        s = SHA1Hash(s+"balls").toHex();
+        s = SHA1Hash(s+"PSK").toHex();
         client.doTransactionOnName(s, user, -price);
         client.getSalt(s);
-        s = SHA1Hash(s+"balls").toHex();
+        s = SHA1Hash(s+"PSK").toHex();
         client.doStockChange(s, upc, -1);
         balance = client.getNameAccountBalance(user);
         cout << "\tNew balance is " << (double)balance/100.0f << ".\n";
@@ -96,7 +106,7 @@ int main(int argc, char** argv) {
         cout << "Amount to change balance by in dollars: ";
         cin >> delta;
         client.getSalt(s);
-        s = SHA1Hash(s+"balls").toHex();
+        s = SHA1Hash(s+"PSK").toHex();
         client.doTransactionOnName(s, user, (int)(100.0*delta));
         break;
       }
@@ -115,7 +125,7 @@ int main(int argc, char** argv) {
         cout << "Price: ";
         cin >> f;
         client.getSalt(s);
-        s = SHA1Hash(s+"balls").toHex();
+        s = SHA1Hash(s+"PSK").toHex();
         client.setUPCPrice(s, upc, (int32_t)(f*100.0));
         break;
       }
@@ -128,7 +138,7 @@ int main(int argc, char** argv) {
   for (uint32_t i =0; i < 5000; i++) {
     std::string s;
     client.getSalt(s);
-    s = SHA1Hash(s + "balls").toHex();
+    s = SHA1Hash(s + "PSK").toHex();
     client.doTransactionOnName(s, "j3parker", 1);
   }
 
index 3c111a1..c1a9e67 100644 (file)
@@ -3,11 +3,13 @@
 
 #include "PosBookie.h"
 #include <thrift/protocol/TBinaryProtocol.h>
-#include <thrift/server/TNonblockingServer.h>
+#include <thrift/server/TSimpleServer.h>
 #include <thrift/transport/TServerSocket.h>
 #include <thrift/transport/TBufferTransports.h>
+#include <thrift/transport/THttpServer.h>
 
 #include "Pos.h"
+#include <thrift/protocol/TJSONProtocol.h>
 #include <thrift/transport/TSocket.h>
 #include <thrift/transport/TTransportUtils.h>
 
@@ -67,7 +69,7 @@ class PosBookieHandler : virtual public PosBookieIf {
   }
 
   E_PURCHASE_STATUS::type purchaseItems(const std::string& auth, const std::string& dataToHash, const std::vector<int64_t> & upcs) {
-    if(saltStore->consumeKey(auth)) {  
+    if(saltStore->consumeKey(auth)) {
       try {
         int32_t total_price = 0;
         std::vector<int64_t>::const_iterator i;
@@ -76,7 +78,7 @@ class PosBookieHandler : virtual public PosBookieIf {
         }
 
         int32_t accountBalance = this->client->getHashAccountBalance(dataToHash);
-        if(accountBalance < total_price) {
+        if(accountBalance > total_price) {
           this->client->doTransactionOnHash(this->getAuthString(), dataToHash, total_price * -1);
           return E_PURCHASE_STATUS::EPS_SUCCESS;
         }
@@ -132,9 +134,14 @@ int main(int argc, char **argv) {
   
   shared_ptr<PosBookieHandler> handler(new PosBookieHandler((char*)server_host, server_port, saltStore));
   shared_ptr<TProcessor> processor(new PosBookieProcessor(handler));
+  shared_ptr<TServerTransport> serverTransport(new TServerSocket(listen_port));
+  shared_ptr<TTransportFactory> transportFactory(new THttpServerTransportFactory());
+  shared_ptr<TProtocolFactory> protocolFactory(new TJSONProtocolFactory());
+
+  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
 
-  TNonblockingServer server(processor, listen_port);
   server.serve();
+
   delete saltStore;
   return 0;
 }