00001 #ifndef Impala_Application_Videolympics_VideolympicsClient_h 00002 #define Impala_Application_Videolympics_VideolympicsClient_h 00003 00004 #include <iostream> 00005 #include <queue> 00006 #include <sstream> 00007 #include "Link/PracticalSocket.h" 00008 #include "Link/PracticalSocket.cpp" 00009 00010 #include "Util/StringParser.h" 00011 00012 namespace Impala { 00013 namespace Application { 00014 namespace Videolympics { 00015 00016 class VideolympicsClient 00017 { 00018 public: 00019 VideolympicsClient(std::string server, int port, int teamnr) 00020 { 00021 mVidolServer = server; 00022 mVidolPort = port; 00023 mVidolTeamNo = teamnr; 00024 ILOG_DEBUG("VideOlypicsClient configured for " << server << ":" << port << ". We are team " << teamnr); 00025 } 00026 00027 int SendShot(std::string shotname) 00028 { 00029 std::ostringstream o; 00030 o << "GET /team=" << mVidolTeamNo << "/" << shotname << std::endl << std::endl; 00031 TCPSocket *s = 0; 00032 try 00033 { 00034 s = new TCPSocket(mVidolServer, mVidolPort); 00035 } 00036 catch (SocketException e) 00037 { 00038 ILOG_ERROR("SendShot " << shotname << " FAILED: could not connect to server."); 00039 if (s) delete s; 00040 return RET_NOCONNECTION; 00041 } 00042 if (s == 0) 00043 { 00044 ILOG_ERROR("SendShot " << shotname << " FAILED: could not initialize server object."); 00045 return RET_NOCONNECTION; 00046 } 00047 00048 std::string call = o.str(); 00049 s->send(call.c_str(), (int)call.length()); 00050 int length = s->recv(mBuffer, 2048); 00051 if (length <= 0) 00052 { 00053 ILOG_ERROR("SendShot " << shotname << " FAILED: No reply from server."); 00054 delete s; 00055 return RET_NOREPLY; 00056 } 00057 // ILOG_DEBUG("Read " << length<< " bytes from server."); 00058 mBuffer[length] = 0; 00059 Util::StringParser p = Util::StringParser(mBuffer); 00060 00061 std::string r = p.GetString('\n'); 00062 int replycode = RET_UNKNOWNREPLY; 00063 00064 if (r == "OK") replycode = RET_OK; 00065 else if (r == "ERROR INVALID") replycode = RET_INVALID; 00066 else if (r == "ERROR TOOSOON") replycode = RET_TOOSOON; 00067 else if (r == "ERROR TOOLATE") replycode = RET_TOOLATE; 00068 else if (r == "ERROR CHEATING DETECTED") replycode = RET_CHEATER; 00069 00070 if (replycode != 0) 00071 { 00072 ILOG_ERROR("SendShot " << shotname << ": FAILED with error code " << replycode << ": " << r); 00073 } else { 00074 ILOG_DEBUG("SendShot " << shotname << ": OK"); 00075 } 00076 00077 delete s; 00078 return replycode; 00079 } 00080 00081 char mBuffer[2048]; 00082 00083 public: 00084 static const int RET_OK = 0; 00085 static const int RET_INVALID = 1; 00086 static const int RET_TOOSOON = 2; 00087 static const int RET_TOOLATE = 3; 00088 static const int RET_NOREPLY = 4; 00089 static const int RET_UNKNOWNREPLY = 5; 00090 static const int RET_NOCONNECTION = 6; 00091 static const int RET_CHEATER = 71775; 00092 private: 00093 std::string mVidolServer; 00094 int mVidolPort; 00095 int mVidolTeamNo; 00096 00097 00098 00099 ILOG_VAR_DEC; 00100 }; 00101 00102 ILOG_VAR_INIT(VideolympicsClient, Application.Videolympics); 00103 00104 00105 } 00106 } 00107 } 00108 00109 #endif