+Payload type overloading, parser template
This commit is contained in:
parent
163165c1f9
commit
a464435f9b
@ -1,2 +0,0 @@
|
|||||||
#include "common.h"
|
|
||||||
|
|
14
nasfaq/connections/common/common.cpp
Normal file
14
nasfaq/connections/common/common.cpp
Normal file
@ -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;
|
||||||
|
}
|
@ -1,27 +1,49 @@
|
|||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
enum MSG_TYPE =
|
/*
|
||||||
|
ALL INCLUDES (COMMON) SHOULD BE HERE
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "../common/common.h"
|
||||||
|
#include "../safe_queue/safe_queue.h"
|
||||||
|
|
||||||
|
|
||||||
|
enum MSG_TYPE
|
||||||
{ COIN_PRICE_UPDATE
|
{ COIN_PRICE_UPDATE
|
||||||
, HISTORY_UPDATE
|
, HISTORY_UPDATE
|
||||||
, BROKER_FEE_UPDATE
|
, BROKER_FEE_UPDATE
|
||||||
, TODAY_PRICES_UPDATE
|
, TODAY_PRICES_UPDATE
|
||||||
}
|
};
|
||||||
|
|
||||||
typedef struct raw_message_t {
|
std::ostream& operator<< (std::ostream&, const MSG_TYPE&);
|
||||||
enum msg_type type;
|
|
||||||
|
typedef struct raw_msg_t {
|
||||||
|
enum MSG_TYPE type;
|
||||||
std::string data;
|
std::string data;
|
||||||
} raw_message_t ;
|
} raw_message_t ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Placeholder for pre-parsed outputs of the socket.
|
||||||
|
*/
|
||||||
|
template <MSG_TYPE E>
|
||||||
|
struct pparsed;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
coinPriceUpdate structure
|
coinPriceUpdate structure
|
||||||
*/
|
*/
|
||||||
typedef struct cpu_hld_t {
|
template <>
|
||||||
|
struct pparsed<COIN_PRICE_UPDATE> {
|
||||||
std::string coin;
|
std::string coin;
|
||||||
float price;
|
float price;
|
||||||
float saleValue;
|
float saleValue;
|
||||||
uint inCirculation;
|
uint inCirculation;
|
||||||
} cpu_hld_t
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -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<raw_msg_t> 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<raw_msg_t> m_queue;
|
|
||||||
}
|
|
||||||
|
|
59
nasfaq/connections/parser/parser.cpp
Normal file
59
nasfaq/connections/parser/parser.cpp
Normal file
@ -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 <MSG_TYPE E>
|
||||||
|
pparsed<E> parse(raw_msg_t rmsg) {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
pparsed<COIN_PRICE_UPDATE> parse<COIN_PRICE_UPDATE>(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<COIN_PRICE_UPDATE> 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<raw_msg_t> 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<raw_msg_t> m_queue;
|
||||||
|
//}
|
||||||
|
//
|
@ -1,5 +1,5 @@
|
|||||||
#ifndef _MY_SSL_H_
|
#ifndef _PARSER_H_
|
||||||
#define _MY_SSL_H_
|
#define _PARSER_H_
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -10,5 +10,7 @@
|
|||||||
#include "../common/common.h"
|
#include "../common/common.h"
|
||||||
#include "../safe_queue/safe_queue.h"
|
#include "../safe_queue/safe_queue.h"
|
||||||
|
|
||||||
|
MSG_TYPE msg_type_detect(std::string);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Binary file not shown.
@ -1,9 +1,11 @@
|
|||||||
#include "../http/http_connector.h"
|
#include "../http/http_connector.h"
|
||||||
#include "../ws/my_ssl.h"
|
#include "../ws/my_ssl.h"
|
||||||
#include "../safe_queue/safe_queue.h"
|
#include "../safe_queue/safe_queue.h"
|
||||||
|
#include "../parser/parser.h"
|
||||||
|
#include "../common/common.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
std::string sid = sid();
|
std::string sid = get_sid();
|
||||||
connect_ws(sid);
|
connect_ws(sid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
clang++ ../http/http_connector.cpp \
|
clang++ ../common/common.cpp \
|
||||||
../ws/my_ssl.cpp \
|
../parser/parser.cpp \
|
||||||
../safe_queue/safe_queue.cpp \
|
../safe_queue/safe_queue.cpp \
|
||||||
|
../http/http_connector.cpp \
|
||||||
|
../ws/my_ssl.cpp \
|
||||||
./main.cpp \
|
./main.cpp \
|
||||||
-o main -lcurl -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./main
|
-o main -lcurl -lcrypto -lssl -lboost_system -lboost_random -lboost_thread -lpthread && ./main
|
||||||
|
@ -60,6 +60,7 @@ public:
|
|||||||
void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
|
void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
|
||||||
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
|
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
|
||||||
std::string payload = msg->get_payload();
|
std::string payload = msg->get_payload();
|
||||||
|
|
||||||
if (payload == "2") {
|
if (payload == "2") {
|
||||||
websocketpp::lib::error_code ec;
|
websocketpp::lib::error_code ec;
|
||||||
m_endpoint->send(m_hdl, "3", websocketpp::frame::opcode::text, ec);
|
m_endpoint->send(m_hdl, "3", websocketpp::frame::opcode::text, ec);
|
||||||
@ -68,9 +69,15 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (payload.substr(4, 15) == "coinPriceUpdate") {
|
} else if (payload.substr(0, 2) == "42") {
|
||||||
std::cout << " > payload: " << payload.substr(21, payload.length()-2) << std::endl;
|
/* Add message to a raw struct and pass to parser */
|
||||||
nlohmann::json jres = nlohmann::json::parse(payload.substr(21, payload.length()-21-1));
|
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);
|
m_messages.push_back(payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,10 @@
|
|||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "../parser/parser.h"
|
||||||
|
#include "../common/common.h"
|
||||||
|
#include "../safe_queue/safe_queue.h"
|
||||||
|
|
||||||
int connect_ws(std::string);
|
int connect_ws(std::string);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user