Joel Grunbaum
2022-01-09 6926be56fe58e199673db158c4fee7ca7b140c8d
protocol.cpp
@@ -30,7 +30,7 @@
std::unordered_map<std::string, book::Book> recoverBook()
{
    std::unordered_map<std::string, book::Book> bs;
    std::ifstream sampleFile("../rec.log");
    std::ifstream sampleFile("../data.test");
    std::stringstream ss;
    ss << sampleFile.rdbuf();
    // httplib::Client cli("http://" + server);
@@ -46,38 +46,32 @@
    return bs;
}
void addOrder(json::AddMessage& order)
json::Message* addOrder(json::AddMessage& order)
{
    std::string message = "{\"message\": " + order.as_string() + ", " +
                          "\"username\": \"" + std::string(USER) +
                          "\", \"password\": \"" + std::string(PASS) + "\"}";
    send(message);
    return send(message);
}
void deleteOrder(json::DeleteMessage& order)
json::Message* deleteOrder(json::DeleteMessage& order)
{
    std::string message = "{\"message\": " + order.as_string() +
                          ", \"username\": \"" + std::string(USER) +
                          "\", \"password\": \"" + std::string(PASS) + "\"}";
    send(message);
    return send(message);
}
void handleMessage(std::unordered_map<std::string, book::Book>& bs,
                   json::Message* message)
{
    if (mapAnnounce.empty()) initialise();
    json::AnnounceMessage* a = dynamic_cast<json::AnnounceMessage*>(message);
    json::SettleMessage* b = dynamic_cast<json::SettleMessage*>(message);
    json::AddedMessage* c = dynamic_cast<json::AddedMessage*>(message);
    json::DeletedMessage* d = dynamic_cast<json::DeletedMessage*>(message);
    json::TradeMessage* e = dynamic_cast<json::TradeMessage*>(message);
    json::Broker* f = dynamic_cast<json::Broker*>(message);
    switch (message->type) {
    case json::FUTURE_TYPE:
    case json::SPREAD_TYPE:
    case json::CALL_TYPE:
    case json::PUT_TYPE:
   announce(bs, a);
   announce(bs, dynamic_cast<json::AnnounceMessage*>(message));
   break;
    case json::SETTLEMENT:
   settle(bs, dynamic_cast<json::SettleMessage*>(message));
@@ -130,20 +124,20 @@
                  json::DeletedMessage* message)
{
    if (message->side == book::Buy) {
   for (auto i = bs[message->product].bidSide.begin();
        i != bs[message->product].bidSide.end(); i++) {
       if (i->id == message->id) {
      bs[message->product].bidSide.erase(i);
      std::make_heap(bs[message->product].bidSide.begin(),
                     bs[message->product].bidSide.end(),
                     std::less<book::Level>());
        for (std::size_t i = 0; i < bs[message->product].bidSide.size(); i++) {
       if (bs[message->product].bidSide[i].id == message->id) {
            bs[message->product].bidSide[i] = bs[message->product].bidSide.back();
            bs[message->product].bidSide.erase(bs[message->product].bidSide.end());
            std::make_heap(bs[message->product].bidSide.begin(),
                           bs[message->product].bidSide.end(),
                           std::less<book::Level>());
       }
   }
    } else {
   for (auto i = bs[message->product].askSide.begin();
        i != bs[message->product].askSide.end(); i++) {
       if (i->id == message->id) {
      bs[message->product].askSide.erase(i);
        for (std::size_t i = 0; i < bs[message->product].askSide.size(); i++) {
       if (bs[message->product].askSide[i].id == message->id) {
            bs[message->product].askSide[i] = bs[message->product].askSide.back();
            bs[message->product].askSide.erase(bs[message->product].askSide.end());
      std::make_heap(bs[message->product].askSide.begin(),
                     bs[message->product].askSide.end(),
                     std::greater<book::Level>());
@@ -169,9 +163,10 @@
{
}
void send(std::string& message)
json::Message* send(std::string& message)
{
    cli.Post("/execution", message, "text/plain");
    auto res = cli.Post("/execution", message, "text/plain");
    return json::parseSingle(res->body);
}
} // namespace protocol