summaryrefslogtreecommitdiffstats
path: root/src/madc.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-05-20 17:00:33 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-05-20 17:00:33 +0200
commit96767ff85614974ff8c31686bce19001ec5cccd2 (patch)
treee9a66ebde9e6bfd2db81011e63d56834a8b4e943 /src/madc.cpp
parent776377bb21ee1cfe0bcdbc000f7c6fa0be227226 (diff)
downloadmad-96767ff85614974ff8c31686bce19001ec5cccd2.tar
mad-96767ff85614974ff8c31686bce19001ec5cccd2.zip
waitWhile-Methoden f?r Connection hinzugef?gt
Diffstat (limited to 'src/madc.cpp')
-rw-r--r--src/madc.cpp37
1 files changed, 26 insertions, 11 deletions
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 <readline/readline.h>
#include <readline/history.h>
+#include <boost/thread/condition_variable.hpp>
+
+
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<boost::mutex> 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<Common::Requests::IdentifyRequest>(connection, boost::bind(&commandFinished));
- while(commandRunning) {
- usleep(100000);
+ {
+ boost::unique_lock<boost::mutex> lock(commandMutex);
+ commandRunning = true;
+ Common::RequestManager::get()->sendRequest<Common::Requests::IdentifyRequest>(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<boost::mutex> 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) {