Commit 3111b395 authored by Gabriel Margiani's avatar Gabriel Margiani
Browse files

push events

parent ab18a48d
Loading
Loading
Loading
Loading
+78 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <iostream>
#include <random>
#include <ctime>
#include <future>

#include <boost/algorithm/string.hpp>

@@ -44,6 +45,7 @@ std::map<std::string, std::string> p3::client::command_shorthands {
	{"rr", "reregister"},
	{"rl", "reload"},
	{"st", "status"},
	{"e", "events"},
	{"q", "quit"},
	{"x", "exit"}
};
@@ -91,7 +93,7 @@ p3::client::~client() {
	delete connection;
}

std::string p3::client::get_input(std::string q, p3::client::inputType type) {
std::string p3::client::get_input(const std::string & q, p3::client::inputType type) {
	std::string in;
	if (!argv.empty()) {
		in = argv.front();
@@ -118,8 +120,19 @@ std::string p3::client::get_input(std::string q, p3::client::inputType type) {

void p3::client::run() {
	bool r = true;
	connection->send(p3::protocol::COMMAND, get_input("", COMMAND));
	while (r) {
		std::string in = get_input("", COMMAND);
		if (in == "exit" || in == "quit") {
			break;
		} else if ("events" == in) {
			if(push_event_manager()) {
				continue;
			} else {
				break;
			}
		} else {
			connection->send(p3::protocol::COMMAND, in);
		}
		p3::mQueueMessage m = connection->receive(-1);
		bool print_list = true;
		switch (m.get_command()) {
@@ -160,12 +173,71 @@ void p3::client::run() {
			default:
				std::cout << "Unknown server command. " << m.get_value() << std::endl;
		}
		std::string in = get_input("", COMMAND);
		if (in == "exit" || in == "quit") {
			break;
	}
}

bool p3::client::push_event_manager() {
	bool ointer = interactive;
	interactive = true;

	auto res = std::async(std::launch::async, &p3::client::push_event_reader, this);

	connection->send(p3::protocol::COMMAND, "startevents");
	while (res.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) {
		std::string in("");
		std::cin >> in;
		boost::trim(in);
		boost::to_lower(in);
		if (in == "exit" || in == "quit" || in == "q" || in == "x" || !std::cin) {
			connection->send(p3::protocol::COMMAND, "stopevents");
			res.wait();
			return false;
		} else if ("events" == in || "e" == in) {
			connection->send(p3::protocol::COMMAND, "stopevents");
			res.wait();
		} else {
			connection->send(p3::protocol::COMMAND, in);
			std::cout << "No commands supported in push event mode. Use 'event' to stop." << std::endl;
		}
	}
	interactive = ointer;
	return res.get();
}

bool p3::client::push_event_reader() {
	bool okexit = false;
	while (true) {
		p3::mQueueMessage m = connection->receive(-1);
		bool print_list = true;
		switch (m.get_command()) {
			case p3::protocol::BEGINTEXT:
				m = connection->receive(30);
				while (m.get_command() != p3::protocol::ENDTEXT) {
					if (print_list) {
						std::cout << m.get_value() << std::endl;
					}
					m = connection->receive(30);
				}
				std::cout << "---" << std::endl;
				break;
			case p3::protocol::OK:
				if (verbose >= 1) std::cout << m.get_value() << std::endl;
				if (okexit) {
					return true;
				} else {
					okexit = true;
				}
				break;
			case p3::protocol::ERROR:
				std::cout << "ERROR: " << m.get_value() << std::endl;
				return true;
			case p3::protocol::FATAL_ERROR:
				std::cout << "FATAL ERROR: " << m.get_value() << std::endl;
				return true;
			case p3::protocol::BYE:
				std::cout << "done." << std::endl;
				return false;
			default:
				std::cout << "Unknown server command. " << m.get_value() << std::endl;
		}
	}
}
+4 −1
Original line number Diff line number Diff line
@@ -56,7 +56,10 @@ namespace p3 {
		bool interactive;
		int verbose;

		std::string get_input(std::string question, inputType is_command);
		bool push_event_reader(); // Return false if client should exit.
		bool push_event_manager(); // Return false if client should exit.

		std::string get_input(const std::string & question, inputType is_command);

		public:
			client(int argc, char *argv[]);
+17 −0
Original line number Diff line number Diff line
@@ -91,3 +91,20 @@ bool p3::config::get_bool(const std::string& k) {
		throw;
	}
}

std::list<std::string> p3::config::get_string_list(const std::string& k) {
	try {
		std::list<std::string> res;
		boost::split(res, data.at(k), boost::is_any_of(",;"));
		if (res.size() == 1 && (res.front() == "none" || res.front() == "")) {
			return std::list<std::string>();
		}
		for (auto &n : res) {
			boost::trim(n);
		}
		return res;
	} catch (std::out_of_range& e) {
		std::cout << "!! config::get_string_list(): Invalide configuration key: " << k << std::endl;
		throw;
	}
}
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#define __P3_CONFIG_H__

#include <string>
#include <list>
#include <map>

namespace p3 {
@@ -40,6 +41,7 @@ namespace p3 {
			std::string get_string(const std::string& k);
			int get_int(const std::string& k);
			bool get_bool(const std::string& k);
			std::list <std::string> get_string_list(const std::string& k);

	};
}
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ namespace p3 {
		std::string message;

		public:
			perror(std::string c, std::string e) : context(c), error(e), message(c + ": " + e) {};
			perror(const std::string & c, const std::string & e) : context(c), error(e), message(c + ": " + e) {};
			~perror() throw () {};

			const char * getContext() { return context.c_str(); }
Loading