Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GtpEngine Class Reference

#include <GtpEngine.h>

List of all members.


Detailed Description

Base class for GTP (Go Text Protocol) engines.

Commands can be added with GtpEngine::Register(). Existing commands can be overridden by registering a new handler for the command or by overriding the command handler member function in subclasses.

See also:
GtpEngine Commands, Simulated Delays

Definition at line 524 of file GtpEngine.h.

Public Member Functions

 GtpEngine (GtpInputStream &in, GtpOutputStream &out)
 Constructor.
virtual ~GtpEngine ()
void ExecuteFile (const std::string &name, std::ostream &log=std::cerr)
 Execute commands from file.
std::string ExecuteCommand (const std::string &cmd, std::ostream &log=std::cerr)
 Execute a single command string.
void MainLoop ()
 Run the main command loop.
void Register (const std::string &name, GtpCallbackBase *callback)
 Register command handler.
template<class T>
void Register (const std::string &command, typename GtpCallback< T >::Method method, T *instance)
 Register a member function as a command handler.
bool IsRegistered (const std::string &command) const
 Returns if command registered.
void SetQuit ()
 Set flag for quitting the main command loop.
bool IsQuitSet () const
 Did the last command set the quit flag?
virtual void Ponder ()
 Ponder.
virtual void InitPonder ()
 Prepare for pondering.
virtual void StopPonder ()
 Stop pondering.
virtual void Interrupt ()
 Interrupt the current command.
Command Callbacks
virtual void CmdKnownCommand (GtpCommand &)
 Return true if command is known, false otherwise.
virtual void CmdListCommands (GtpCommand &)
 List all known commands.
virtual void CmdName (GtpCommand &)
 Return name.
virtual void CmdProtocolVersion (GtpCommand &)
 Return protocol version.
virtual void CmdQuit (GtpCommand &)
 Quit command loop.
virtual void CmdVersion (GtpCommand &)
 Return empty version string.

Protected Member Functions

virtual void BeforeHandleCommand ()
 Hook function to be executed before each command.
virtual void BeforeWritingResponse ()
 Hook function to be executed before the response of a command is written.

Private Types

typedef std::map< std::string,
GtpCallbackBase * > 
CallbackMap

Private Member Functions

 GtpEngine (const GtpEngine &engine)
 Not to be implemented.
GtpEngineoperator= (const GtpEngine &engine) const
 Not to be implemented.
bool HandleCommand (GtpCommand &cmd, GtpOutputStream &out)

Private Attributes

bool m_quit
GtpInputStreamm_in
GtpOutputStreamm_out
CallbackMap m_callbacks


Member Typedef Documentation

typedef std::map<std::string,GtpCallbackBase*> CallbackMap [private]

Definition at line 680 of file GtpEngine.h.


Constructor & Destructor Documentation

GtpEngine ( GtpInputStream in,
GtpOutputStream out 
)

Constructor.

Parameters:
in Input GTP stream
out Output GTP stream

Definition at line 657 of file GtpEngine.cpp.

References CmdKnownCommand(), CmdListCommands(), CmdName(), CmdProtocolVersion(), CmdQuit(), CmdVersion(), and Register().

~GtpEngine (  )  [virtual]

Definition at line 670 of file GtpEngine.cpp.

References m_callbacks.

GtpEngine ( const GtpEngine engine  )  [private]

Not to be implemented.


Member Function Documentation

void BeforeHandleCommand (  )  [protected, virtual]

Hook function to be executed before each command.

Default implementation does nothing.

Definition at line 682 of file GtpEngine.cpp.

Referenced by HandleCommand().

void BeforeWritingResponse (  )  [protected, virtual]

Hook function to be executed before the response of a command is written.

Default implementation does nothing.

Definition at line 687 of file GtpEngine.cpp.

Referenced by HandleCommand().

void CmdKnownCommand ( GtpCommand  )  [virtual]

Return true if command is known, false otherwise.

Definition at line 693 of file GtpEngine.cpp.

References GtpCommand::Arg(), GtpCommand::CheckNuArg(), IsRegistered(), and GtpCommand::SetResponseBool().

Referenced by GtpEngine().

void CmdListCommands ( GtpCommand  )  [virtual]

List all known commands.

Definition at line 700 of file GtpEngine.cpp.

References GtpCommand::CheckArgNone(), and m_callbacks.

Referenced by GtpEngine().

void CmdName ( GtpCommand  )  [virtual]

Return name.

Definition at line 709 of file GtpEngine.cpp.

References GtpCommand::CheckArgNone().

Referenced by GtpEngine().

void CmdProtocolVersion ( GtpCommand  )  [virtual]

Return protocol version.

Definition at line 716 of file GtpEngine.cpp.

References GtpCommand::CheckArgNone().

Referenced by GtpEngine().

void CmdQuit ( GtpCommand  )  [virtual]

Quit command loop.

Definition at line 723 of file GtpEngine.cpp.

References GtpCommand::CheckArgNone(), and SetQuit().

Referenced by GtpEngine().

void CmdVersion ( GtpCommand cmd  )  [virtual]

Return empty version string.

The GTP standard says to return empty string, if no meaningful reponse is available.

Definition at line 733 of file GtpEngine.cpp.

References GtpCommand::CheckArgNone().

Referenced by GtpEngine().

string ExecuteCommand ( const std::string &  cmd,
std::ostream &  log = std::cerr 
)

Execute a single command string.

Parameters:
cmd The command line
log Stream for logging the command and response to (default is std::cerr).
Returns:
The command response
Exceptions:
GtpFailure If the command fails

Definition at line 738 of file GtpEngine.cpp.

References HandleCommand(), GtpCommand::Init(), IsCommandLine(), GtpCommand::Line(), and GtpCommand::Response().

void ExecuteFile ( const std::string &  name,
std::ostream &  log = std::cerr 
)

Execute commands from file.

Aborts on the first command that fails.

Parameters:
name The file name
log Stream for logging the commands and responses to (default is std::cerr).
Exceptions:
GtpFailure If a command fails

Definition at line 753 of file GtpEngine.cpp.

References HandleCommand(), GtpCommand::Init(), IsCommandLine(), and GtpCommand::Line().

bool HandleCommand ( GtpCommand cmd,
GtpOutputStream out 
) [private]

Definition at line 774 of file GtpEngine.cpp.

References BeforeHandleCommand(), BeforeWritingResponse(), GtpOutputStream::Flush(), GtpCommand::ID(), m_callbacks, GtpCommand::Name(), ReplaceEmptyLines(), GtpFailure::Response(), GtpCommand::Response(), and GtpOutputStream::Write().

Referenced by ExecuteCommand(), ExecuteFile(), and MainLoop().

void InitPonder (  )  [virtual]

Prepare for pondering.

See also:
Ponder() The default implementation does nothing.

Definition at line 900 of file GtpEngine.cpp.

void Interrupt (  )  [virtual]

Interrupt the current command.

This function implements interrupt functionality as used by GoGui. It will be called from a different thread that the command thread when the special command line # interrupt is received. The default implementation does nothing.

Definition at line 913 of file GtpEngine.cpp.

bool IsQuitSet (  )  const

Did the last command set the quit flag?

Definition at line 876 of file GtpEngine.cpp.

References m_quit.

bool IsRegistered ( const std::string &  command  )  const

Returns if command registered.

Definition at line 812 of file GtpEngine.cpp.

References m_callbacks.

Referenced by CmdKnownCommand().

void MainLoop (  ) 

Run the main command loop.

Reads lines from input stream, calls the corresponding command handler and writes the response to the output stream. Empty lines in the command responses will be replaced by a line containing a single space, because empty lines are not allowed in GTP responses.

Definition at line 817 of file GtpEngine.cpp.

References HandleCommand(), m_in, m_out, m_quit, and SetQuit().

GtpEngine& operator= ( const GtpEngine engine  )  const [private]

Not to be implemented.

void Ponder (  )  [virtual]

Ponder.

This function will be called in MainLoop() while the engine is waiting for the next command. It will be called after InitPonder() from a different thread than the command thread, but only while waiting for the next command, so no concurrent execution of this function and other engine functions is possible. The function should return immediately when StopPonder() is called. InitPonder() and StopPonder() are called from the command thread. In a typical implementation, InitPonder() will clear an abort flag and StopPonder() will set it. Ponder() will poll the abort flag and return when it is set (or it has nothing to do; or some maximum time limit for pondering was exceeded). The default implementation does nothing and returns immediately.

Definition at line 884 of file GtpEngine.cpp.

void Register ( const std::string &  command,
typename GtpCallback< T >::Method  method,
T *  instance 
)

Register a member function as a command handler.

If a command was already registered with the same name, it will be replaced by the new command.

Definition at line 700 of file GtpEngine.h.

References Register().

void Register ( const std::string &  name,
GtpCallbackBase callback 
)

Register command handler.

Takes ownership of callback. If a command was already registered with the same name, it will be replaced by the new command.

Definition at line 857 of file GtpEngine.cpp.

References m_callbacks.

Referenced by GtpEngine(), and Register().

void SetQuit (  ) 

Set flag for quitting the main command loop.

Currently, this function works only for the "quit" command, if the engine is compiled with interrupt functionality (GTENGINE_INTERRUPT). Therefore, it is not possible for other commands to decide to quit (which would be necessary for instance to implement a maximal game number if playing on KGS and deciding to quit on the kgs-game_over command, if the maximum number is reached).

The reason is that the command stream is then read from a different thread using a blocking, non-interruptible read function, which is entered before the command handler is invoked in the main thread. Because of the non-interruptible read function, the implementation of GtpEngine needs to know what commands will quit to avoid entering this read function after a quit.

If a way is found to interrupt the read thread during the execution of the blocking std::getline (maybe in a future version of Boost.Thread), this function could also be called in other GTP commands.

See also:
MainLoop()

Definition at line 871 of file GtpEngine.cpp.

References m_quit.

Referenced by CmdQuit(), and MainLoop().

void StopPonder (  )  [virtual]

Stop pondering.

See also:
Ponder() The default implementation does nothing.

Definition at line 892 of file GtpEngine.cpp.


Member Data Documentation

CallbackMap m_callbacks [private]

Definition at line 688 of file GtpEngine.h.

Referenced by CmdListCommands(), HandleCommand(), IsRegistered(), Register(), and ~GtpEngine().

GtpInputStream& m_in [private]

Definition at line 684 of file GtpEngine.h.

Referenced by MainLoop().

GtpOutputStream& m_out [private]

Definition at line 686 of file GtpEngine.h.

Referenced by MainLoop().

bool m_quit [private]

Definition at line 682 of file GtpEngine.h.

Referenced by IsQuitSet(), MainLoop(), and SetQuit().


The documentation for this class was generated from the following files:


17 Jun 2010 Doxygen 1.4.7