diff --git a/include/protocol/protocol.hpp b/include/protocol/protocol.hpp index c13fc8c..359ab40 100644 --- a/include/protocol/protocol.hpp +++ b/include/protocol/protocol.hpp @@ -18,18 +18,33 @@ namespace protocol { using handler_t = std::function; using handler_map_t = std::map; + struct session_key_t { + std::string session; + std::string token; + }; + + bool operator==(const session_key_t& l, const session_key_t& r); + + session_key_t session_key_from(const token::list_t& tokens); + session_key_t session_key_from(const message_t& message); + class protocol_t { public: void set_handlers(const handler_map_t handlers); + void absorb(const std::string& input); void send(const token::list_t& tokens); + void submit_message(const session_key_t& session_key, const std::vector& header, const std::vector& body); + protected: virtual void emit(const std::string& output) { }; private: handler_map_t handlers; size_t max_pattern_length; + + void send_data_line(const session_key_t& session_key, const std::string& data_line); }; class ios_protocol_t : public protocol_t { @@ -45,20 +60,11 @@ namespace protocol { std::ostream& out; }; - struct session_t { - std::string session; - std::string token; - }; - - bool operator==(const session_t& l, const session_t& r); - - session_t session_from(token::list_t tokens); - session_t session_from(message_t message); } namespace std { - template<> struct hash { - std::size_t operator()(protocol::session_t const& session) const noexcept { + template<> struct hash { + std::size_t operator()(protocol::session_key_t const& session) const noexcept { auto session_hash = std::hash{}(session.session); auto token_hash = std::hash{}(session.token); return session_hash ^ (token_hash << 1); diff --git a/src/protocol/protocol.cpp b/src/protocol/protocol.cpp index e017748..00e593a 100644 --- a/src/protocol/protocol.cpp +++ b/src/protocol/protocol.cpp @@ -45,6 +45,29 @@ namespace protocol { } } + void protocol_t::send_data_line(const session_key_t& session_key, const string& data_line) { + send({"filter-dataline", session_key.session, session_key.token, session_key.token, data_line}); + } + + void protocol_t::submit_message(const session_key_t& session_key, const vector& header, const vector& body) { + for(auto& data_line : header) { + send_data_line(session_key, data_line); + } + + send_data_line(session_key, ""); + + for(auto& data_line : body) { + if(data_line == ".") { + send_data_line(session_key, ".."); + continue; + } + + send_data_line(session_key, data_line); + } + + send_data_line(session_key, "."); + } + ios_protocol_t::ios_protocol_t(std::istream& in, std::ostream& out) : in(in), out(out) { } @@ -60,18 +83,18 @@ namespace protocol { } } - bool operator==(const session_t& l, const session_t& r) { + bool operator==(const session_key_t& l, const session_key_t& r) { return l.session == r.session && l.token == r.token; } - session_t session_from(token::list_t tokens) { + session_key_t session_key_from(const token::list_t& tokens) { auto session_id = tokens.at(5); auto session_token = tokens.at(6); return {session_id, session_token}; } - session_t session_from(message_t message) { + session_key_t session_key_from(const message_t& message) { auto tokens = token::decompose(message.input); - return session_from(tokens); + return session_key_from(tokens); } }