diff --git a/nasfaq/connections/common/common.c b/nasfaq/connections/common/common.c deleted file mode 100644 index ddef6fc..0000000 --- a/nasfaq/connections/common/common.c +++ /dev/null @@ -1,2 +0,0 @@ -#include "common.h" - diff --git a/nasfaq/connections/common/common.cpp b/nasfaq/connections/common/common.cpp new file mode 100644 index 0000000..bdc5991 --- /dev/null +++ b/nasfaq/connections/common/common.cpp @@ -0,0 +1,14 @@ +#include "common.h" + +std::ostream& operator<< (std::ostream& stream, MSG_TYPE const & type) { + std::string ret; + switch(type) { + case COIN_PRICE_UPDATE: ret = "COIN_PRICE_UPDATE"; break; + case HISTORY_UPDATE: ret = "HISTORY_UPDATE"; break; + case BROKER_FEE_UPDATE: ret = "BROKER_FEE_UPDATE"; break; + case TODAY_PRICES_UPDATE: ret = "TODAY_PRICES_UPDATE"; break; + default: ret = "Unknown type: "; + } + stream << ret; + return stream; +} diff --git a/nasfaq/connections/common/common.h b/nasfaq/connections/common/common.h index 403b09e..df4f5ad 100644 --- a/nasfaq/connections/common/common.h +++ b/nasfaq/connections/common/common.h @@ -1,27 +1,49 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -enum MSG_TYPE = +/* + ALL INCLUDES (COMMON) SHOULD BE HERE +*/ +#include +#include +#include + +#include + +#include "../common/common.h" +#include "../safe_queue/safe_queue.h" + + +enum MSG_TYPE { COIN_PRICE_UPDATE , HISTORY_UPDATE , BROKER_FEE_UPDATE , TODAY_PRICES_UPDATE - } +}; -typedef struct raw_message_t { - enum msg_type type; +std::ostream& operator<< (std::ostream&, const MSG_TYPE&); + +typedef struct raw_msg_t { + enum MSG_TYPE type; std::string data; } raw_message_t ; +/** + Placeholder for pre-parsed outputs of the socket. +*/ +template +struct pparsed; + /** coinPriceUpdate structure */ -typedef struct cpu_hld_t { +template <> +struct pparsed { std::string coin; float price; float saleValue; uint inCirculation; -} cpu_hld_t +}; #endif diff --git a/nasfaq/connections/parser/parser.c b/nasfaq/connections/parser/parser.c deleted file mode 100644 index 915eb64..0000000 --- a/nasfaq/connections/parser/parser.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "parser.h" - -enum MSG_TYPE msg_type_detect(std::string op) { - MSG_TYPE ret; - - if (op.substr(2, 15) == "coinPriceUpdate") { - ret = COIN_PRICE_UPDATE; - } else if (op.substr(2, 13) == "historyUpdate") { - ret = HISTORY_UPDATE; - } else if (op.substr(2, 17) == "todayPricesUpdate") { - ret = TODAY_PRICES_UPDATE; - } - } else if (op.substr(2, 15) == "brokerFeeUpdate") { - ret = BROKER_FEE_UPDATE; - } - - return ret; -} - -class parser { -public: - parser (SafeQueue queue) - : m_queue(queue); - {} - - void parse() { - std::string raw_data; - - msg_type type; - std::string data; - - while(1) { - raw_data = m_queue.dequeue(); - type = msg_type_detect(raw_data); - - data = m_queue. - sleep(0.5); - } - } -private: - SafeQueue m_queue; -} - diff --git a/nasfaq/connections/parser/parser.cpp b/nasfaq/connections/parser/parser.cpp new file mode 100644 index 0000000..c822640 --- /dev/null +++ b/nasfaq/connections/parser/parser.cpp @@ -0,0 +1,59 @@ +#include "parser.h" + +MSG_TYPE msg_type_detect(std::string op) { + MSG_TYPE ret; + + if (op.substr(2, 15) == "coinPriceUpdate") { + ret = COIN_PRICE_UPDATE; + } else if (op.substr(2, 13) == "historyUpdate") { + ret = HISTORY_UPDATE; + } else if (op.substr(2, 17) == "todayPricesUpdate") { + ret = TODAY_PRICES_UPDATE; + } else if (op.substr(2, 15) == "brokerFeeUpdate") { + ret = BROKER_FEE_UPDATE; + } + return ret; +} + +template +pparsed parse(raw_msg_t rmsg) {}; + +template <> +pparsed parse(raw_msg_t rmsg) { + std::string payload = rmsg.data; + nlohmann::json jparsed = nlohmann::json::parse(payload.substr(21, payload.length()-21-1)); /* Check for errors and emptiness needed */ + + pparsed rop; + rop.coin = jparsed["coin"]; + rop.price = (jparsed["info"])["price"]; + rop.saleValue = (jparsed["info"])["saleValue"]; + rop.inCirculation = (jparsed["info"])["inCirculation"]; + + return rop; +} + + +//class parser { +//public: +// parser (SafeQueue queue) +// : m_queue(queue); +// {} +// +// void parse() { +// std::string raw_data; +// +// msg_type type; +// std::string data; +// +// while(1) { +// raw_data = m_queue.dequeue(); +// type = msg_type_detect(raw_data); +// +// data = m_queue. +// sleep(0.5); +// } +// } +//private: +// SafeQueue m_queue; +//} +// diff --git a/nasfaq/connections/parser/parser.h b/nasfaq/connections/parser/parser.h index 3886db4..c80d568 100644 --- a/nasfaq/connections/parser/parser.h +++ b/nasfaq/connections/parser/parser.h @@ -1,5 +1,5 @@ -#ifndef _MY_SSL_H_ -#define _MY_SSL_H_ +#ifndef _PARSER_H_ +#define _PARSER_H_ #include #include @@ -10,5 +10,7 @@ #include "../common/common.h" #include "../safe_queue/safe_queue.h" +MSG_TYPE msg_type_detect(std::string); + #endif diff --git a/nasfaq/connections/safe_queue/safe_queue.c b/nasfaq/connections/safe_queue/safe_queue.cpp similarity index 100% rename from nasfaq/connections/safe_queue/safe_queue.c rename to nasfaq/connections/safe_queue/safe_queue.cpp diff --git a/nasfaq/connections/test/main b/nasfaq/connections/test/main index 60d4a8e..b13c4fd 100755 Binary files a/nasfaq/connections/test/main and b/nasfaq/connections/test/main differ diff --git a/nasfaq/connections/test/main.cpp b/nasfaq/connections/test/main.cpp index d8d6720..cc14920 100644 --- a/nasfaq/connections/test/main.cpp +++ b/nasfaq/connections/test/main.cpp @@ -1,9 +1,11 @@ #include "../http/http_connector.h" #include "../ws/my_ssl.h" #include "../safe_queue/safe_queue.h" +#include "../parser/parser.h" +#include "../common/common.h" int main(void) { - std::string sid = sid(); + std::string sid = get_sid(); connect_ws(sid); } diff --git a/nasfaq/connections/test/run.sh b/nasfaq/connections/test/run.sh index c844803..04676f8 100755 --- a/nasfaq/connections/test/run.sh +++ b/nasfaq/connections/test/run.sh @@ -1,5 +1,7 @@ -clang++ ../http/http_connector.cpp \ - ../ws/my_ssl.cpp \ +clang++ ../common/common.cpp \ + ../parser/parser.cpp \ ../safe_queue/safe_queue.cpp \ + ../http/http_connector.cpp \ + ../ws/my_ssl.cpp \ ./main.cpp \ -o main -lcurl -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./main diff --git a/nasfaq/connections/ws/my_ssl.cpp b/nasfaq/connections/ws/my_ssl.cpp index 5ad64e7..1b7d536 100644 --- a/nasfaq/connections/ws/my_ssl.cpp +++ b/nasfaq/connections/ws/my_ssl.cpp @@ -60,6 +60,7 @@ public: void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) { if (msg->get_opcode() == websocketpp::frame::opcode::text) { std::string payload = msg->get_payload(); + if (payload == "2") { websocketpp::lib::error_code ec; m_endpoint->send(m_hdl, "3", websocketpp::frame::opcode::text, ec); @@ -68,9 +69,15 @@ public: return; } - } else if (payload.substr(4, 15) == "coinPriceUpdate") { - std::cout << " > payload: " << payload.substr(21, payload.length()-2) << std::endl; - nlohmann::json jres = nlohmann::json::parse(payload.substr(21, payload.length()-21-1)); + } else if (payload.substr(0, 2) == "42") { + /* Add message to a raw struct and pass to parser */ + raw_message_t rmsg; /* should be cached somewhere? */ + rmsg.type = msg_type_detect(payload.substr(2, 20)); /* Get message type, this should be out of this loop and encompassing the pongs and probes */ + rmsg.data = payload.substr(3, payload.length() - 1); /* Should use regex */ + + std::cout << "Got payload of type: " << rmsg.type << std::endl; + + //nlohmann::json jres = nlohmann::json::parse(payload.substr(21, payload.length()-21-1)); m_messages.push_back(payload); } } diff --git a/nasfaq/connections/ws/my_ssl.h b/nasfaq/connections/ws/my_ssl.h index e193d28..b40d042 100644 --- a/nasfaq/connections/ws/my_ssl.h +++ b/nasfaq/connections/ws/my_ssl.h @@ -13,6 +13,10 @@ #include +#include "../parser/parser.h" +#include "../common/common.h" +#include "../safe_queue/safe_queue.h" + int connect_ws(std::string); #endif