read from config file probably maybe
authorOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 04:20:35 +0000 (23:20 -0500)
committerOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 04:20:35 +0000 (23:20 -0500)
database/gen-cpp/PosBookie_server.skeleton.cpp

index 79bdc71..11761b1 100644 (file)
@@ -40,20 +40,20 @@ class PosBookieHandler : virtual public PosBookieIf {
   }
 
  public:
-  PosBookieHandler(char* serverHost, int serverPort)
+  PosBookieHandler(char* serverHost, int serverPort, SaltStore* saltStore)
     : socket(new TSocket(serverHost, serverPort))
     , transport(new TFramedTransport(socket))
     , protocol(new TBinaryProtocol(transport))
     , client(protocol)
   {
+       this->saltStore = saltStore;
+
     try{
       this->client.ping();
     } catch {
-      printf("failed to contact server");
-      exit(1);
+      fprintf(stderr, "Failed to contact server '%s:%d'!\n", serverHost, serverPort);
+      abort();
     }
-
-    saltStore = new SaltStore(120, 256, "SEKRET!");
   }
 
   void ping() { }
@@ -97,15 +97,36 @@ class PosBookieHandler : virtual public PosBookieIf {
 };
 
 int main(int argc, char **argv) {
-  if(argc != 4) {
-    printf("usage: posbookie <serverhost> <serverport> <listenport>\n");
+  if(argc != 2) {
+    printf("usage: posbookie <configFile>\n");
     exit(1);
   }
 
-  int server_port = STRTOINT(argv[2]);
-  int listen_port = STRTOINT(argv[3]);
+  ConfigFile conf(argv[1]);
 
-  shared_ptr<PosBookieHandler> handler(new PosBookieHandler(argv[1], server_port));
+  char* server_host;
+  int server_port;
+  int listen_port;
+  SaltStore * saltStore;
+
+  try {
+    server_host = conf.read<char*>("server_host", "localhost");
+    server_port = conf.read<int>("server_port", 9090);
+    listen_port = conf.read<int>("listen_port", 9091);
+    saltStore = new SaltStore(
+      conf.read<uint64_t>("key_lifetime", 120),
+      conf.read<size_t>("salt_size", 256),
+      conf.read<std::string>("secret")
+      );
+  } catch(ConfigFile::file_not_found & e) {
+    fprintf(stderr, "Could not open configuration file '%s'!\n", e.filename.c_str());
+    abort();
+  } catch(ConfigFile::key_not_found & e) {
+    fprintf(stderr, "Could not find required configuration key '%s'!\n", e.key.c_str());
+    abort();
+  }
+  
+  shared_ptr<PosBookieHandler> handler(new PosBookieHandler(server_host, server_port, saltStore));
   shared_ptr<TProcessor> processor(new PosBookieProcessor(handler));
 
   TNonblockingServer server(processor, listen_port);