From 38c6e9b9fc245ca5e6e6cee2806cb64dcbd34e35 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Wed, 18 Jan 2012 21:36:47 +0800 Subject: [PATCH] pause AI in opponent's turn (Temporary) This is a temporary and not entirely effective solution, to prevent AI from spamming CPU useage to make their opponents time out. Yes I could use renice instead, but its just as easy for an AI to get around. I will have to replace this with a better solution. But I don't have one at the moment. --- judge/manager/ai_controller.h | 2 ++ judge/manager/controller.cpp | 2 ++ judge/manager/controller.h | 4 ++++ judge/manager/game.cpp | 6 ++++-- judge/manager/program.cpp | 36 ++++++++++++++++++++++++++++++++++- judge/manager/program.h | 5 ++++- 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/judge/manager/ai_controller.h b/judge/manager/ai_controller.h index ee053e6..f4682eb 100644 --- a/judge/manager/ai_controller.h +++ b/judge/manager/ai_controller.h @@ -20,6 +20,8 @@ class AI_Controller : public Controller, private Program virtual MovementResult QueryMove(std::string & buffer); virtual void Message(const char * message) {Program::SendMessage(message);} + virtual void Pause() {Program::Pause();} //Hack wrapper + virtual void Continue() {Program::Continue();} //Hack wrapper virtual bool Valid() const {return Program::Running();} diff --git a/judge/manager/controller.cpp b/judge/manager/controller.cpp index 5691406..3a78e9b 100644 --- a/judge/manager/controller.cpp +++ b/judge/manager/controller.cpp @@ -188,3 +188,5 @@ MovementResult Controller::MakeMove(string & buffer) return moveResult; } + + diff --git a/judge/manager/controller.h b/judge/manager/controller.h index 55c233d..5aa6364 100644 --- a/judge/manager/controller.h +++ b/judge/manager/controller.h @@ -28,6 +28,10 @@ class Controller virtual MovementResult QueryMove(std::string & buffer) = 0; virtual bool Valid() const {return true;} + + virtual void Pause() {} // Hack function (AI_Controller ONLY will overwrite with wrapper to Program::Pause) + virtual void Continue() {} // Hack function (AI_Controller '' '' wrapper to Program::Continue) + const Piece::Colour colour; std::string name; diff --git a/judge/manager/game.cpp b/judge/manager/game.cpp index 486920d..8d3eef0 100644 --- a/judge/manager/game.cpp +++ b/judge/manager/game.cpp @@ -498,7 +498,8 @@ MovementResult Game::Play() #endif //BUILD_GRAPHICS turn = Piece::RED; - + blue->Pause(); + red->Continue(); if (!Board::HaltResult(result)) { result = CheckVictoryAttrition(); @@ -542,7 +543,8 @@ MovementResult Game::Play() turn = Piece::BLUE; - + red->Pause(); + blue->Continue(); if (!Board::HaltResult(result)) { result = CheckVictoryAttrition(); diff --git a/judge/manager/program.cpp b/judge/manager/program.cpp index d5463f4..031d411 100644 --- a/judge/manager/program.cpp +++ b/judge/manager/program.cpp @@ -20,7 +20,7 @@ using namespace std; * The child process closes unused sides of the pipe, and then calls exec to replace itself with the AI program * The parent process closes unused sides of the pipe, and sets up member variables - associates streams with the pipe fd's for convenience. */ -Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0) +Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0), paused(false) { //See if file exists and is executable... if (access(executablePath, X_OK) != 0) @@ -100,8 +100,42 @@ Program::~Program() } +/** + * Forces the program to pause by sending SIGSTOP + * Program can be resumed by calling Continue() (which sends SIGCONT) + * @returns true if the program could be paused, false if it couldn't (probably because it wasn't running) + */ +bool Program::Pause() +{ + if (pid > 0 && kill(pid,SIGSTOP) == 0) + { + paused = true; + return true; + } + return false; +} +/** + * Causes a paused program to continue + * @returns true if the program could be continued, false if it couldn't (probably because it wasn't running) + */ +bool Program::Continue() +{ + if (pid > 0 && kill(pid,SIGCONT) == 0) + { + paused = false; + return true; + } + return false; +} +/** + * @returns true iff the program is paused + */ +bool Program::Paused() const +{ + return paused; +} /** diff --git a/judge/manager/program.h b/judge/manager/program.h index 01e00bc..b01ce00 100644 --- a/judge/manager/program.h +++ b/judge/manager/program.h @@ -24,7 +24,9 @@ class Program bool GetMessage(std::string & buffer, double timeout=-1); //Retrieves a message, or waits for a timeout (if positive) bool Running() const; - + bool Paused() const; + bool Pause(); + bool Continue(); protected: @@ -33,6 +35,7 @@ class Program private: pid_t pid; //Process ID of the program wrapped + bool paused; }; -- 2.20.1