From 96767ff85614974ff8c31686bce19001ec5cccd2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 20 May 2009 17:00:33 +0200 Subject: waitWhile-Methoden f?r Connection hinzugef?gt --- src/madc.cpp | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/madc.cpp') diff --git a/src/madc.cpp b/src/madc.cpp index a902144..8499232 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -31,17 +31,25 @@ #include #include +#include + + using namespace Mad; static bool commandRunning = false; +static boost::mutex commandMutex; +static boost::condition_variable commandNotify; static void usage(const std::string &cmd) { std::cerr << "Usage: " << cmd << " address" << std::endl; } static void commandFinished() { + boost::lock_guard lock(commandMutex); commandRunning = false; + + commandNotify.notify_one(); } int main(int argc, char *argv[]) { @@ -62,15 +70,17 @@ int main(int argc, char *argv[]) { std::cerr << "Connecting to " << argv[1] << "..." << std::flush; - while(connection->isConnecting()) - usleep(100000); + connection->waitWhileConnecting(); Common::RequestManager::get()->registerConnection(connection); - commandRunning = true; - Common::RequestManager::get()->sendRequest(connection, boost::bind(&commandFinished)); - while(commandRunning) { - usleep(100000); + { + boost::unique_lock lock(commandMutex); + commandRunning = true; + Common::RequestManager::get()->sendRequest(connection, boost::bind(&commandFinished)); + while(commandRunning) { + commandNotify.wait(lock); + } } std::cerr << " connected." << std::endl; @@ -86,23 +96,28 @@ int main(int argc, char *argv[]) { Client::CommandParser::get()->setConnection(connection); Client::CommandManager::get()->signalFinished().connect(&commandFinished); - while(connection->isConnected()) { + while(connection->isConnected() && !Client::CommandManager::get()->willDisconnect()) { char *cmd = readline("mad> "); + boost::unique_lock lock(commandMutex); + if(!cmd) { - commandRunning = true; Client::CommandParser::get()->requestDisconnect(); } else if(*cmd) { - commandRunning = true; Client::CommandParser::get()->parse(cmd); add_history(cmd); } + else continue; - while(Client::CommandManager::get()->requestsActive()) - usleep(100000); + commandRunning = Client::CommandManager::get()->requestsActive(); + + while(commandRunning) + commandNotify.wait(lock); } + connection->waitWhileConnected(); + Common::RequestManager::get()->unregisterConnection(connection); } catch(Net::Exception &e) { -- cgit v1.2.3