From d2cfd3eeeb8b6af3b7ccda01e6c1ac581a2df398 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Thu, 13 Jan 2022 06:54:09 +0000
Subject: [PATCH] Added time parsing

---
 json.cpp |   79 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 73 insertions(+), 6 deletions(-)

diff --git a/json.cpp b/json.cpp
index 5d8e340..a1b0a7a 100644
--- a/json.cpp
+++ b/json.cpp
@@ -48,7 +48,8 @@
 	mapOrder = {{"BUY", book::Buy}, {"SELL", book::Sell}};
 
 	mapTrade = {{"BUY_AGGRESSOR", BUY_AGGRESSOR},
-	            {"SELL_AGGRESSOR", SELL_AGGRESSOR}};
+	            {"SELL_AGGRESSOR", SELL_AGGRESSOR},
+	            {"BROKER_TRADE", BROKER_TRADE}};
 
 	mapOrderSide = {{book::Buy, "BUY"}, {book::Sell, "SELL"}};
 }
@@ -58,10 +59,14 @@
 SettleMessage* settle(rapidjson::Value& d);
 AddedMessage* added(rapidjson::Value& d);
 DeletedMessage* deleted(rapidjson::Value& d);
+RejectMessage* reject(rapidjson::Value& d);
 TradeMessage* trade(rapidjson::Value& d);
 BrokerRequest* brokerReq(rapidjson::Value& d);
 BrokerAck* brokerAck(rapidjson::Value& d);
 BrokerConfirm* brokerCon(rapidjson::Value& d);
+ErrorMessage* error(rapidjson::Value& d);
+
+std::chrono::seconds parseTime(std::string& s);
 
 std::queue<Message*> parse(std::string& str)
 {
@@ -83,6 +88,7 @@
 	if (mapTypes.empty()) {
 		initialise();
 	}
+	if (d.HasMember("error")) return error(d);
 	Message* out;
 	switch (mapTypes[d["type"].GetString()]) {
 	case FUTURE_TYPE:
@@ -99,6 +105,9 @@
 		break;
 	case DELETED:
 		out = deleted(d);
+		break;
+	case REJECT:
+		out = reject(d);
 		break;
 	case TRADE:
 		out = trade(d);
@@ -119,12 +128,21 @@
 	return out;
 }
 
+std::chrono::seconds parseTime(std::string& s)
+{
+	return std::chrono::hours{std::stoi(s.substr(0, 4)) * 24 * 30 * 12} +
+	       std::chrono::hours{std::stoi(s.substr(5, 2)) * 24 * 30} +
+	       std::chrono::hours{std::stoi(s.substr(8, 2)) * 24} +
+	       std::chrono::hours{std::stoi(s.substr(11, 2))} +
+	       std::chrono::minutes{std::stoi(s.substr(14, 2))} +
+	       std::chrono::hours{std::stoi(s.substr(16, 5))};
+}
+
 AnnounceMessage* announce(rapidjson::Value& d)
 {
-	// std::stringstream expiryStream(d["expiry"].GetString());
+	std::string es = d["expiry"].GetString();
 	std::chrono::nanoseconds exp_time(0);
-	// expiryStream >>
-	// date::parse("%Y-%m-%f %H:%M%z", exp_time); // Parsing is broken
+	exp_time = parseTime(es);
 	return new AnnounceMessage(
 		mapTypes[d["type"].GetString()], d["product"].GetString(),
 		d["stationId"].GetInt(), d["stationName"].GetString(),
@@ -135,9 +153,9 @@
 
 SettleMessage* settle(rapidjson::Value& d)
 {
-	// std::stringstream expiryStream(d["expiry"].GetString());
 	std::chrono::nanoseconds exp_time(0);
-	// expiryStream >> date::parse("%Y-%m-%d %H:%M%z", exp_time);
+    std::string es = d["expiry"].GetString();
+    exp_time = parseTime(es);
 	return new SettleMessage(
 		mapTypes[d["type"].GetString()], d["product"].GetString(),
 		d["stationName"].GetString(), exp_time, d["price"].GetDouble(),
@@ -159,6 +177,12 @@
 		mapTypes[d["type"].GetString()], d["product"].GetString(),
 		d["id"].GetString(), mapOrder[d["side"].GetString()],
 		d["sequence"].GetInt(), d["timestamp"].GetDouble());
+}
+
+RejectMessage* reject(rapidjson::Value& d)
+{
+	return new RejectMessage(mapTypes[d["type"].GetString()], "",
+	                         d["error"].GetString(), uint64_t(0), double(0));
 }
 
 TradeMessage* trade(rapidjson::Value& d)
@@ -197,11 +221,25 @@
 		d["id"].GetString());
 }
 
+ErrorMessage* error(rapidjson::Value& d)
+{
+	return new ErrorMessage(d["error"].GetString());
+}
+
 Message::Message() : type(NONE), product("error") {}
 
 Message::Message(MessageTypes types, std::string product)
 	: type(types), product(product)
 {
+}
+
+ErrorMessage::ErrorMessage(std::string message)
+	: Message(ERROR, ""), message(message)
+{
+}
+std::string ErrorMessage::as_string()
+{
+	return "{\"error\": \"" + this->message + "\"}";
 }
 
 FromExchange::FromExchange(MessageTypes type, std::string product,
@@ -266,6 +304,18 @@
 {
 }
 
+std::string AddedMessage::as_string()
+{
+	return "{\"type\": \"ADDED\", \"product\": \"" + this->product +
+	       "\", \"product\": \"" + this->id + "\" \"side\": \"" +
+	       mapOrderSide[this->side] +
+	       "\", \"price\": " + std::to_string(this->price) +
+	       "\"filled\": " + std::to_string(this->filled) +
+	       ", \"resting\": " + std::to_string(this->resting) +
+	       ", \"sequence\": " + std::to_string(this->sequence) +
+	       ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
+}
+
 DeleteMessage::DeleteMessage(MessageTypes type, std::string product,
                              std::string id)
 	: ToExchange(type, product), id(id)
@@ -286,6 +336,15 @@
 {
 }
 
+std::string DeletedMessage::as_string()
+{
+	return "{\"type\": \"DELETED\", \"product\": \"" + this->product +
+	       "\", \"product\": \"" + this->id + "\" \"side\": \"" +
+	       mapOrderSide[this->side] +
+	       ", \"sequence\": " + std::to_string(this->sequence) +
+	       ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
+}
+
 RejectMessage::RejectMessage(MessageTypes type, std::string product,
                              std::string error, uint64_t sequence,
                              double timestamp)
@@ -293,6 +352,14 @@
 {
 }
 
+std::string RejectMessage::as_string()
+{
+	return "{\"type\": \"REJECT\", \"product\": \"" + this->product =
+	           "\", \"error\": \"" + this->error +
+	           "\", \"sequence\": " + std::to_string(this->sequence) +
+	           ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
+}
+
 TradeMessage::TradeMessage(MessageTypes type, std::string product, double price,
                            uint64_t volume, std::string buyer,
                            std::string seller, TradeTypeEnum tradeType,

--
Gitblit v1.9.3