You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
#ifndef _PROTOCOL_
|
|
#define _PROTOCOL_
|
|
|
|
#include <protocol/token.hpp>
|
|
|
|
#include <string>
|
|
#include <map>
|
|
#include <functional>
|
|
#include <iostream>
|
|
|
|
namespace protocol {
|
|
struct message_t {
|
|
std::string input;
|
|
token::pattern_t pattern;
|
|
};
|
|
|
|
class protocol_t;
|
|
using handler_t = std::function<void(protocol_t&, const message_t&)>;
|
|
using handler_map_t = std::map<token::pattern_t, handler_t>;
|
|
|
|
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);
|
|
|
|
protected:
|
|
virtual void emit(const std::string& output) { };
|
|
|
|
private:
|
|
handler_map_t handlers;
|
|
size_t max_pattern_length;
|
|
};
|
|
|
|
class ios_protocol_t : public protocol_t {
|
|
public:
|
|
ios_protocol_t(std::istream& in, std::ostream& out);
|
|
void run();
|
|
|
|
protected:
|
|
virtual void emit(const std::string& output);
|
|
|
|
private:
|
|
std::istream& in;
|
|
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<protocol::session_t> {
|
|
std::size_t operator()(protocol::session_t const& session) const noexcept {
|
|
auto session_hash = std::hash<std::string>{}(session.session);
|
|
auto token_hash = std::hash<std::string>{}(session.token);
|
|
return session_hash ^ (token_hash << 1);
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif |