+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_
|
||||
#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
|
||||
, 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 <MSG_TYPE E>
|
||||
struct pparsed;
|
||||
|
||||
/**
|
||||
coinPriceUpdate structure
|
||||
*/
|
||||
typedef struct cpu_hld_t {
|
||||
template <>
|
||||
struct pparsed<COIN_PRICE_UPDATE> {
|
||||
std::string coin;
|
||||
float price;
|
||||
float saleValue;
|
||||
uint inCirculation;
|
||||
} cpu_hld_t
|
||||
};
|
||||
|
||||
#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_
|
||||
#define _MY_SSL_H_
|
||||
#ifndef _PARSER_H_
|
||||
#define _PARSER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
@ -10,5 +10,7 @@
|
||||
#include "../common/common.h"
|
||||
#include "../safe_queue/safe_queue.h"
|
||||
|
||||
MSG_TYPE msg_type_detect(std::string);
|
||||
|
||||
#endif
|
||||
|
||||
|
Binary file not shown.
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,10 @@
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../parser/parser.h"
|
||||
#include "../common/common.h"
|
||||
#include "../safe_queue/safe_queue.h"
|
||||
|
||||
int connect_ws(std::string);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user