Commit 54ad7c18 authored by Gabriel Margiani's avatar Gabriel Margiani

added restart command.

parent 860fd9f8
......@@ -37,6 +37,7 @@ void print_usage() {
std::cout << "3phone - p3 usage: " << std::endl
<< "p3 s erve - start the server" << std::endl
<< "p3 quit, stop - terminate server" << std::endl
<< "p3 restart - restart server" << std::endl
<< "p3 reload - reload phonebook and some parts of the configuration." << std::endl
<< "p3 c all [nr] - call someone" << std::endl
<< "p3 a nswer - answer a currently ringing call" << std::endl
......@@ -58,9 +59,12 @@ void print_usage() {
int run_server() {
try {
p3::server srv;
std::cout << "running server" << std::endl;
srv.run();
bool restart = true;
while (restart) {
p3::server srv(restart);
std::cout << "running server" << std::endl;
srv.run();
}
} catch (p3::perror const & e) {
std::cout << "Server Error: " << e.what() << std::endl;
return 1;
......@@ -79,13 +83,13 @@ int run_client(int argc, char *argv[]) {
return 0;
}
// Tell the server to quit, see protocol.h/server.h/client.h
int send_quit() {
// Tell the server to quit or restart, see protocol.h/server.h/client.h
int send_command(p3::protocol command, const std::string& text) {
try {
p3::mQueue q(p3::mQueue::DefaultId);
q.send(p3::protocol::QUIT, "Terminate server");
q.send(command, text);
} catch (p3::perror const & e) {
std::cout << "Termination Error: " << e.what() << std::endl;
std::cout << text << " Error: " << e.what() << std::endl;
return 1;
}
return 0;
......@@ -100,7 +104,9 @@ int main(int argc, char *argv[]) {
} else if (a == "s" || a == "serve" || a == "server") {
return run_server();
} else if (a == "quit" || a == "stop") { // Quit has to be send without client connection.
return send_quit();
return send_command(p3::protocol::QUIT, "Terminate server");
} else if (a == "restart") { // Restart has to be send without client connection.
return send_command(p3::protocol::RESTART, "Restart server");
} else {
return run_client(argc, argv);
}
......
......@@ -59,6 +59,10 @@ namespace p3 {
// MAIN CHANNEL.
// Tells the server to exit.
RESTART, // Data: *, Answer: none.
// MAIN CHANNEL.
// Tells the server to restart.
SET_TIMEOUT, // Data: [timeout], ANSWER: OK/ERROR
// Set the listen timeout on the server for the
// current connection. (see msgq for valid values)
......
......@@ -45,13 +45,15 @@ std::map<std::string, std::string> p3::server::default_config {
};
p3::server::server() :
p3::server::server(bool& r) :
restart_flag(r),
conf(std::string(std::getenv("HOME")) + "/.3phonerc", p3::server::default_config),
connection(p3::mQueue::DefaultId, true),
book(),
hook(conf, book),
phone(conf, hook, book)
{
restart_flag = false;
p3::phonebook::set_config(conf);
book.load();
}
......@@ -77,9 +79,14 @@ void p3::server::run() {
break;
case p3::protocol::QUIT:
r = false;
restart_flag = false;
break;
case p3::protocol::RESTART:
r = false;
restart_flag = true;
break;
default:
std::cout << "Protokol Error, invalid command in MAIN: " << m.get_value() << std::endl;
std::cout << "Protocol Error, invalid command in MAIN: " << m.get_value() << std::endl;
}
}
} catch (p3::perror & e) {
......
......@@ -66,6 +66,8 @@ namespace p3 {
class server {
bool & restart_flag;
static std::map<std::string, std::string> default_config;
config conf;
......@@ -85,7 +87,8 @@ namespace p3 {
void server_thread(const std::string& id);
public:
server();
// if restart is true after ther server dies, the user asked to restart it.
explicit server(bool& restart);
~server();
void run();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment