00001 //---------------------------------------------------------------------------- 00002 /** @file SgGtpClient.h */ 00003 //---------------------------------------------------------------------------- 00004 00005 #ifndef SG_GTPCLIENT_H 00006 #define SG_GTPCLIENT_H 00007 00008 #include <iostream> 00009 #include <string> 00010 #include "SgException.h" 00011 00012 //---------------------------------------------------------------------------- 00013 00014 /** Error thrown by SgGtpClient::Send() if command fails or connection is 00015 broken. 00016 */ 00017 class SgGtpFailure 00018 : public SgException 00019 { 00020 public: 00021 /** Constructor. 00022 @param message The failure response of the command or other error 00023 message. 00024 */ 00025 SgGtpFailure(const std::string& message); 00026 }; 00027 00028 //---------------------------------------------------------------------------- 00029 00030 /** Client connection to an external GTP engine. 00031 Usage example: 00032 @code 00033 // Run GNU Go and send a GTP command 00034 try 00035 { 00036 SgProcess process("gnugo --mode gtp"); 00037 SgGtpClient gtp(process.Input(), process.Output()); 00038 string result = gtp.Send("version"); 00039 SgDebug() << "Success response: " << result << '\n'; 00040 } 00041 catch (const SgGtpFailure& e) 00042 { 00043 SgDebug() << "Error response: " << e.what() << '\n'; 00044 } 00045 catch (const SgException& e) 00046 { 00047 SgDebug() << "Error running GNU Go: " << e.what() << '\n'; 00048 } 00049 00050 @endcode 00051 */ 00052 class SgGtpClient 00053 { 00054 public: 00055 /** Constructor. 00056 @param in Input stream. 00057 @param out Output stream. 00058 @param verbose Log stream to SgDebug() 00059 */ 00060 SgGtpClient(std::istream& in, std::ostream& out, bool verbose = false); 00061 00062 virtual ~SgGtpClient(); 00063 00064 /** Send a command. 00065 @return The response if command succeeds (without status character 00066 and whitespace after status character) 00067 @throws SgGtpFailure If command fails or connection is broken. 00068 */ 00069 std::string Send(const std::string& command); 00070 00071 private: 00072 bool m_verbose; 00073 00074 std::istream& m_in; 00075 00076 std::ostream& m_out; 00077 }; 00078 00079 //---------------------------------------------------------------------------- 00080 00081 #endif // SG_GTPCLIENT_H