pos/database/gen-cpp/PosBookie_server.skeleton.cpp

137 lines
3.7 KiB
C++

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "PosBookie.h"
#include <thrift/protocol/TBinaryProtocol.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 "salt_store.h"
#include "sha1.h"
#include <stdio.h>
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
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;
SaltStore * saltStore;
std::string getAuthString() {
std::string 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)
{
this->saltStore = saltStore;
try{
this->client.ping();
} catch {
fprintf(stderr, "Failed to contact server '%s:%d'!\n", serverHost, serverPort);
abort();
}
}
void ping() { }
void getSalt(std::string& _return) {
_return = this->saltStore->getSalt();
}
E_PURCHASE_STATUS::type purchaseItems(const std::string& auth, const std::string& dataToHash, const std::vector<int64_t> & upcs) {
if(saltStore->consumeKey(auth)) {
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(this->getAuthString(), 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;
}
}
return E_PURCHASE_STATUS::type::EPS_FAIL;
}
int32_t getUPCPrice(const int64_t upc) {
return this->client.getUPCPrice(upc);
}
int32_t getStock(const int64_t upc) {
return this->client.getStock(upc);
}
};
int main(int argc, char **argv) {
if(argc != 2) {
printf("usage: posbookie <configFile>\n");
exit(1);
}
ConfigFile conf(argv[1]);
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);
server.serve();
return 0;
}