make things work mostly
authorOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 05:52:16 +0000 (00:52 -0500)
committerOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 05:52:16 +0000 (00:52 -0500)
16 files changed:
database/Makefile
database/bookie.conf [new file with mode: 0644]
database/db.conf
database/gen-cpp/PosBookie.h
database/gen-cpp/PosBookie_server.skeleton.cpp
database/gen-cpp/pos-bookie_constants.cpp [deleted file]
database/gen-cpp/pos-bookie_constants.h [deleted file]
database/gen-cpp/pos-bookie_types.cpp [deleted file]
database/gen-cpp/pos-bookie_types.h [deleted file]
database/gen-cpp/posbookie_constants.cpp [new file with mode: 0644]
database/gen-cpp/posbookie_constants.h [new file with mode: 0644]
database/gen-cpp/posbookie_types.cpp [new file with mode: 0644]
database/gen-cpp/posbookie_types.h [new file with mode: 0644]
database/pos-bookie.thrift [deleted file]
database/posbookie.thrift [new file with mode: 0644]
database/salt_store.h

index 8255767..b4f93b2 100644 (file)
@@ -8,7 +8,7 @@ CXXFLAGS = -O3 -fexpensive-optimizations -ffast-math -fno-math-errno -Wall -I/us
 endif\r
 LDFLAGS = /users/m4burns/thrift/lib/libthriftnb.a /users/m4burns/thrift/lib/libthrift.a -levent\r
 \r
-SOURCES = ConfigFile.cpp posdb.cpp db_if.cpp linus_sha1.c log.cpp nameserver.cpp sha1.cpp gen-cpp/pos_constants.cpp gen-cpp/Pos.cpp gen-cpp/pos_types.cpp\r
+SOURCES = ConfigFile.cpp posdb.cpp db_if.cpp linus_sha1.c log.cpp nameserver.cpp sha1.cpp gen-cpp/pos_constants.cpp gen-cpp/Pos.cpp gen-cpp/pos_types.cpp salt_store.cpp gen-cpp/PosBookie.cpp\r
 \r
 CPPOBJECTS = $(patsubst %.cpp,%.o,$(SOURCES))\r
 OBJECTS = $(patsubst %.c,%.o,$(CPPOBJECTS))\r
@@ -18,12 +18,12 @@ DEPS = $(patsubst %.c,%.d,$(CPPDEPS))
 SERVER = server\r
 CLIENT = client\r
 DUMP = dump\r
-\r
+BOOKIE = bookie\r
 ##################################################################\r
 \r
 .PHONY : all clean\r
 \r
-all : $(SERVER) $(CLIENT) $(DUMP)\r
+all : $(SERVER) $(CLIENT) $(DUMP) $(BOOKIE)\r
 \r
 %.d : %.cpp\r
        $(CXX) -MM $(CXXFLAGS) $^ > $@\r
@@ -50,6 +50,9 @@ $(CLIENT) : $(OBJECTS) client.o
 $(DUMP) : $(OBJECTS) dump.o\r
        $(CXX) $(CXXFLAGS) $(OBJECTS) dump.o -o $@ $(LDFLAGS)\r
 \r
+$(BOOKIE) : $(OBJECTS) gen-cpp/PosBookie_server.skeleton.o\r
+       $(CXX) $(CXXFLAGS) $(OBJECTS) gen-cpp/PosBookie_server.skeleton.o -o $@ $(LDFLAGS)\r
+\r
 #################################################################\r
 \r
 clean :\r
diff --git a/database/bookie.conf b/database/bookie.conf
new file mode 100644 (file)
index 0000000..5ea2479
--- /dev/null
@@ -0,0 +1,6 @@
+server_host = localhost
+server_port = 9090
+listen_port = 9091
+key_lifetime = 120
+salt_size = 256
+secret = balls
index 77a458e..f86e6ae 100644 (file)
@@ -1,4 +1,4 @@
 key_lifetime = 120
 salt_size = 256
-secret = balls
+secret = PSK
 db_file = db
index 46bc2be..481348b 100644 (file)
@@ -8,7 +8,7 @@
 #define PosBookie_H
 
 #include <thrift/TDispatchProcessor.h>
-#include "pos-bookie_types.h"
+#include "posbookie_types.h"
 
 namespace posbookie {
 
index 71a8d49..3c111a1 100644 (file)
@@ -15,6 +15,8 @@
 #include "sha1.h"
 #include <stdio.h>
 
+#include "ConfigFile.h"
+
 using namespace ::apache::thrift;
 using namespace ::apache::thrift::protocol;
 using namespace ::apache::thrift::transport;
@@ -23,34 +25,36 @@ using namespace ::apache::thrift::server;
 using boost::shared_ptr;
 
 using namespace  ::posbookie;
+using namespace  ::pos;
 
 class PosBookieHandler : virtual public PosBookieIf {
 
   shared_ptr<TTransport> socket;
   shared_ptr<TTransport> transport;
   shared_ptr<TProtocol> protocol;
-  PosClient client;
+  PosClient client;
 
   SaltStore * saltStore;
 
   std::string getAuthString() {
     std::string s;
-    this->client.getSalt(s);
+    this->client->getSalt(s);
     return SHA1Hash(s+"PSK").toHex();
   }
 
  public:
   PosBookieHandler(char* serverHost, int serverPort, SaltStore* saltStore)
-    : socket(new TSocket(serverHost, serverPort))
-    , transport(new TFramedTransport(socket))
-    , protocol(new TBinaryProtocol(transport))
-    , client(protocol)
   {
+    socket = shared_ptr<TTransport>(new TSocket(serverHost, serverPort));
+    transport = shared_ptr<TTransport>(new TFramedTransport(socket));
+    protocol = shared_ptr<TProtocol>(new TBinaryProtocol(transport));
+    client = new PosClient(protocol);
        this->saltStore = saltStore;
 
-    try{
-      this->client.ping();
-    } catch {
+    try {
+         transport->open();
+      this->client->ping();
+    } catch(...) {
       fprintf(stderr, "Failed to contact server '%s:%d'!\n", serverHost, serverPort);
       abort();
     }
@@ -66,32 +70,32 @@ class PosBookieHandler : virtual public PosBookieIf {
     if(saltStore->consumeKey(auth)) {  
       try {
         int32_t total_price = 0;
-        std::vector<int64_t>::iterator i;
+        std::vector<int64_t>::const_iterator i;
         for(i = upcs.begin(); i != upcs.end(); i++) {
           total_price += this->getUPCPrice(*i);
         }
 
-        int32_t accountBalance = this->client.getHashAccountBalance(dataToHash);
+        int32_t accountBalance = this->client->getHashAccountBalance(dataToHash);
         if(accountBalance < total_price) {
-          this->client.performTransactionOnHash(this->getAuthString(), dataToHash, total_price * -1);
-          return E_PURCHASE_STATUS::type::EPS_SUCCESS;
+          this->client->doTransactionOnHash(this->getAuthString(), dataToHash, total_price * -1);
+          return E_PURCHASE_STATUS::EPS_SUCCESS;
         }
 
-        return E_PURCHASE_STATUS::type::EPS_NOMONEY;
+        return E_PURCHASE_STATUS::EPS_NOMONEY;
       } catch(...) {
-        return E_PURCHASE_STATUS::type::EPS_FAIL;
+        return E_PURCHASE_STATUS::EPS_FAIL;
       }
     }
 
-    return E_PURCHASE_STATUS::type::EPS_FAIL;
+    return E_PURCHASE_STATUS::EPS_FAIL;
   }
 
   int32_t getUPCPrice(const int64_t upc) {
-    return this->client.getUPCPrice(upc);
+    return this->client->getUPCPrice(upc);
   }
 
   int32_t getStock(const int64_t upc) {
-    return this->client.getStock(upc);
+    return this->client->getStock(upc);
   }
 
 };
@@ -104,13 +108,13 @@ int main(int argc, char **argv) {
 
   ConfigFile conf(argv[1]);
 
-  char* server_host;
+  const char* server_host;
   int server_port;
   int listen_port;
   SaltStore * saltStore;
 
   try {
-    server_host = conf.read<char*>("server_host", "localhost");
+    server_host = conf.read<std::string>("server_host", "localhost").c_str();
     server_port = conf.read<int>("server_port", 9090);
     listen_port = conf.read<int>("listen_port", 9091);
     saltStore = new SaltStore(
@@ -126,7 +130,7 @@ int main(int argc, char **argv) {
     abort();
   }
   
-  shared_ptr<PosBookieHandler> handler(new PosBookieHandler(server_host, server_port, saltStore));
+  shared_ptr<PosBookieHandler> handler(new PosBookieHandler((char*)server_host, server_port, saltStore));
   shared_ptr<TProcessor> processor(new PosBookieProcessor(handler));
 
   TNonblockingServer server(processor, listen_port);
diff --git a/database/gen-cpp/pos-bookie_constants.cpp b/database/gen-cpp/pos-bookie_constants.cpp
deleted file mode 100644 (file)
index 97f75c5..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/**
- * Autogenerated by Thrift Compiler (0.9.0)
- *
- * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
- *  @generated
- */
-#include "pos-bookie_constants.h"
-
-namespace posbookie {
-
-const pos-bookieConstants g_pos-bookie_constants;
-
-pos-bookieConstants::pos-bookieConstants() {
-}
-
-} // namespace
-
diff --git a/database/gen-cpp/pos-bookie_constants.h b/database/gen-cpp/pos-bookie_constants.h
deleted file mode 100644 (file)
index 6d16960..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/**
- * Autogenerated by Thrift Compiler (0.9.0)
- *
- * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
- *  @generated
- */
-#ifndef pos-bookie_CONSTANTS_H
-#define pos-bookie_CONSTANTS_H
-
-#include "pos-bookie_types.h"
-
-namespace posbookie {
-
-class pos-bookieConstants {
- public:
-  pos-bookieConstants();
-
-};
-
-extern const pos-bookieConstants g_pos-bookie_constants;
-
-} // namespace
-
-#endif
diff --git a/database/gen-cpp/pos-bookie_types.cpp b/database/gen-cpp/pos-bookie_types.cpp
deleted file mode 100644 (file)
index 308ca59..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * Autogenerated by Thrift Compiler (0.9.0)
- *
- * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
- *  @generated
- */
-#include "pos-bookie_types.h"
-
-#include <algorithm>
-
-namespace posbookie {
-
-int _kE_PURCHASE_STATUSValues[] = {
-  E_PURCHASE_STATUS::EPS_FAIL,
-  E_PURCHASE_STATUS::EPS_NOMONEY,
-  E_PURCHASE_STATUS::EPS_SUCCESS
-};
-const char* _kE_PURCHASE_STATUSNames[] = {
-  "EPS_FAIL",
-  "EPS_NOMONEY",
-  "EPS_SUCCESS"
-};
-const std::map<int, const char*> _E_PURCHASE_STATUS_VALUES_TO_NAMES(::apache::thrift::TEnumIterator(3, _kE_PURCHASE_STATUSValues, _kE_PURCHASE_STATUSNames), ::apache::thrift::TEnumIterator(-1, NULL, NULL));
-
-} // namespace
diff --git a/database/gen-cpp/pos-bookie_types.h b/database/gen-cpp/pos-bookie_types.h
deleted file mode 100644 (file)
index 034794f..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/**
- * Autogenerated by Thrift Compiler (0.9.0)
- *
- * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
- *  @generated
- */
-#ifndef pos-bookie_TYPES_H
-#define pos-bookie_TYPES_H
-
-#include <thrift/Thrift.h>
-#include <thrift/TApplicationException.h>
-#include <thrift/protocol/TProtocol.h>
-#include <thrift/transport/TTransport.h>
-
-
-
-namespace posbookie {
-
-struct E_PURCHASE_STATUS {
-  enum type {
-    EPS_FAIL = 0,
-    EPS_NOMONEY = 1,
-    EPS_SUCCESS = 2
-  };
-};
-
-extern const std::map<int, const char*> _E_PURCHASE_STATUS_VALUES_TO_NAMES;
-
-} // namespace
-
-#endif
diff --git a/database/gen-cpp/posbookie_constants.cpp b/database/gen-cpp/posbookie_constants.cpp
new file mode 100644 (file)
index 0000000..5f1f7fb
--- /dev/null
@@ -0,0 +1,17 @@
+/**
+ * Autogenerated by Thrift Compiler (0.9.0)
+ *
+ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
+ *  @generated
+ */
+#include "posbookie_constants.h"
+
+namespace posbookie {
+
+const posbookieConstants g_posbookie_constants;
+
+posbookieConstants::posbookieConstants() {
+}
+
+} // namespace
+
diff --git a/database/gen-cpp/posbookie_constants.h b/database/gen-cpp/posbookie_constants.h
new file mode 100644 (file)
index 0000000..0617240
--- /dev/null
@@ -0,0 +1,24 @@
+/**
+ * Autogenerated by Thrift Compiler (0.9.0)
+ *
+ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
+ *  @generated
+ */
+#ifndef posbookie_CONSTANTS_H
+#define posbookie_CONSTANTS_H
+
+#include "posbookie_types.h"
+
+namespace posbookie {
+
+class posbookieConstants {
+ public:
+  posbookieConstants();
+
+};
+
+extern const posbookieConstants g_posbookie_constants;
+
+} // namespace
+
+#endif
diff --git a/database/gen-cpp/posbookie_types.cpp b/database/gen-cpp/posbookie_types.cpp
new file mode 100644 (file)
index 0000000..25795dc
--- /dev/null
@@ -0,0 +1,25 @@
+/**
+ * Autogenerated by Thrift Compiler (0.9.0)
+ *
+ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
+ *  @generated
+ */
+#include "posbookie_types.h"
+
+#include <algorithm>
+
+namespace posbookie {
+
+int _kE_PURCHASE_STATUSValues[] = {
+  E_PURCHASE_STATUS::EPS_FAIL,
+  E_PURCHASE_STATUS::EPS_NOMONEY,
+  E_PURCHASE_STATUS::EPS_SUCCESS
+};
+const char* _kE_PURCHASE_STATUSNames[] = {
+  "EPS_FAIL",
+  "EPS_NOMONEY",
+  "EPS_SUCCESS"
+};
+const std::map<int, const char*> _E_PURCHASE_STATUS_VALUES_TO_NAMES(::apache::thrift::TEnumIterator(3, _kE_PURCHASE_STATUSValues, _kE_PURCHASE_STATUSNames), ::apache::thrift::TEnumIterator(-1, NULL, NULL));
+
+} // namespace
diff --git a/database/gen-cpp/posbookie_types.h b/database/gen-cpp/posbookie_types.h
new file mode 100644 (file)
index 0000000..c7ec6b6
--- /dev/null
@@ -0,0 +1,31 @@
+/**
+ * Autogenerated by Thrift Compiler (0.9.0)
+ *
+ * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
+ *  @generated
+ */
+#ifndef posbookie_TYPES_H
+#define posbookie_TYPES_H
+
+#include <thrift/Thrift.h>
+#include <thrift/TApplicationException.h>
+#include <thrift/protocol/TProtocol.h>
+#include <thrift/transport/TTransport.h>
+
+
+
+namespace posbookie {
+
+struct E_PURCHASE_STATUS {
+  enum type {
+    EPS_FAIL = 0,
+    EPS_NOMONEY = 1,
+    EPS_SUCCESS = 2
+  };
+};
+
+extern const std::map<int, const char*> _E_PURCHASE_STATUS_VALUES_TO_NAMES;
+
+} // namespace
+
+#endif
diff --git a/database/pos-bookie.thrift b/database/pos-bookie.thrift
deleted file mode 100644 (file)
index a985c68..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-namespace cpp posbookie
-
-enum E_PURCHASE_STATUS { EPS_FAIL, EPS_NOMONEY, EPS_SUCCESS }
-
-service PosBookie {
-       void ping(),
-
-       string getSalt(),
-
-  E_PURCHASE_STATUS
-    purchaseItems(1:string auth, 2:string dataToHash, 3:list<i64> upcs),
-
-
-       i32 getUPCPrice(1:i64 upc),
-  i32 getStock(1:i64 upc)
-}
-
-/* POS interface :
-       i32 getAccountFromHash(1:string dataToHash),
-       list<string> getHashesFromAccountId(1:i32 account),
-       list<string> getHashesFromAccountName(1:string account),
-
-       i32 getHashAccountBalance(1:string dataToHash),
-       i32 getIdAccountBalance(1:i32 account),
-       i32 getNameAccountBalance(1:string account),
-
-       i64 associateHashWithId(1:string auth, 2:string dataToHash, 3:i32 account),
-       i64 associateHashWithName(1:string auth, 2:string dataToHash, 3:string account),
-       i64 deassociateHash(1:string auth, 2:string dataToHash),
-
-       i32 getUPCPrice(1:i64 upc),
-       i64 setUPCPrice(1:string auth, 2:i64 upc, 3:i32 price),
-
-       i64 doTransactionOnHash(1:string auth, 2:string dataToHash, 3:i32 delta),
-       i64 doTransactionOnId(1:string auth, 2:i32 account, 3:i32 delta),
-       i64 doTransactionOnName(1:string auth, 2:string account, 3:i32 delta),
-       i64 revertTransaction(1:string auth, 2:i64 serial),
-
-       i32 getStock(1:i64 upc),
-       i64 doStockChange(1:string auth, 2:i64 upc, 3:i32 delta),
-
-       list<string> toString()
-*/
-
diff --git a/database/posbookie.thrift b/database/posbookie.thrift
new file mode 100644 (file)
index 0000000..a985c68
--- /dev/null
@@ -0,0 +1,44 @@
+namespace cpp posbookie
+
+enum E_PURCHASE_STATUS { EPS_FAIL, EPS_NOMONEY, EPS_SUCCESS }
+
+service PosBookie {
+       void ping(),
+
+       string getSalt(),
+
+  E_PURCHASE_STATUS
+    purchaseItems(1:string auth, 2:string dataToHash, 3:list<i64> upcs),
+
+
+       i32 getUPCPrice(1:i64 upc),
+  i32 getStock(1:i64 upc)
+}
+
+/* POS interface :
+       i32 getAccountFromHash(1:string dataToHash),
+       list<string> getHashesFromAccountId(1:i32 account),
+       list<string> getHashesFromAccountName(1:string account),
+
+       i32 getHashAccountBalance(1:string dataToHash),
+       i32 getIdAccountBalance(1:i32 account),
+       i32 getNameAccountBalance(1:string account),
+
+       i64 associateHashWithId(1:string auth, 2:string dataToHash, 3:i32 account),
+       i64 associateHashWithName(1:string auth, 2:string dataToHash, 3:string account),
+       i64 deassociateHash(1:string auth, 2:string dataToHash),
+
+       i32 getUPCPrice(1:i64 upc),
+       i64 setUPCPrice(1:string auth, 2:i64 upc, 3:i32 price),
+
+       i64 doTransactionOnHash(1:string auth, 2:string dataToHash, 3:i32 delta),
+       i64 doTransactionOnId(1:string auth, 2:i32 account, 3:i32 delta),
+       i64 doTransactionOnName(1:string auth, 2:string account, 3:i32 delta),
+       i64 revertTransaction(1:string auth, 2:i64 serial),
+
+       i32 getStock(1:i64 upc),
+       i64 doStockChange(1:string auth, 2:i64 upc, 3:i32 delta),
+
+       list<string> toString()
+*/
+
index ea34d15..475e958 100644 (file)
@@ -3,12 +3,12 @@
 #include "sha1.h"
 #include <string>
 #include <list>
+#include <stdint.h>
 
 class SaltStore {
 
 public:
        SaltStore(uint64_t key_lifetime, size_t salt_size, std::string secret);
-       ~SaltStore();
        std::string getSalt();
        bool consumeKey(std::string key);