00001 #ifndef Impala_Core_IDash_Client_h
00002 #define Impala_Core_IDash_Client_h
00003
00004 #include "Basis/ILog.h"
00005 #include "Util/StringParser.h"
00006 #include "Util/ChannelProxy.h"
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace IDash
00013 {
00014
00015
00016 class Client
00017 {
00018 public:
00019
00020 Client(CString socketName, CString passwordFile)
00021 {
00022 int colonPos = socketName.find(":");
00023 if (colonPos <= 0)
00024 {
00025 ILOG_ERROR("Not a valid server address (port number is missing): "
00026 << socketName);
00027 return;
00028 }
00029 Init(socketName.substr(0, colonPos),
00030 atol(socketName.substr(colonPos + 1)), passwordFile);
00031 }
00032
00033 Client(CString serverAddress, int port, CString passwordFile)
00034 {
00035 Init(serverAddress, port, passwordFile);
00036 }
00037
00038 virtual
00039 ~Client()
00040 {
00041 Disconnect();
00042 }
00043
00044 bool
00045 IsConnected()
00046 {
00047 return (mChannelProxy && mChannelProxy->ChannelIsValid());
00048 }
00049
00050 String
00051 ScheduleJob(CString baseUri, CString job)
00052 {
00053 if (!IsConnected())
00054 return "ERROR: 500: Not connected to IDash server";
00055
00056 String request = "ScheduleJob:baseUri=" + baseUri + ";job=" + job;
00057 String response = mChannelProxy->Send(request);
00058 if (response.substr(0, 19) == "JobAccepted:jobRef=")
00059 {
00060 String res = response.substr(19);
00061 return res;
00062 }
00063 ILOG_ERROR("ScheduleJob failed: " << response);
00064
00065 return response;
00066 }
00067
00068 String
00069 GetJobs(CString baseUri)
00070 {
00071 if (!IsConnected())
00072 return "ERROR: 500: Not connected";
00073
00074 String request = "GetJobs:baseUri=" + baseUri + ";";
00075 String response = mChannelProxy->Send(request);
00076 if (response.substr(0, 5) == "Jobs:")
00077 {
00078 String res = response.substr(5);
00079 return res;
00080 }
00081 ILOG_ERROR("GetJobs failed: " << response);
00082
00083 return response;
00084 }
00085
00086 String
00087 StatusJob(CString baseUri, CString jobId)
00088 {
00089 if (!IsConnected())
00090 return "ERROR: 500: Not connected";
00091
00092 String request = "StatusJob:job=" + jobId + ";";
00093 String response = mChannelProxy->Send(request);
00094 if (response.substr(0, 14) == "JobStatus:job=")
00095 {
00096 String res = response.substr(14);
00097 return res;
00098 }
00099 ILOG_ERROR("StatusJob failed: " << response);
00100
00101 return response;
00102 }
00103
00104 String
00105 DeleteCases()
00106 {
00107 if (!IsConnected())
00108 return "ERROR: 500: Not connected";
00109
00110 String response = mChannelProxy->Send("DeleteCases");
00111 return response;
00112 }
00113
00114 private:
00115
00116 void
00117 Init(CString serverAddress, int port, CString passwordFile)
00118 {
00119 mServerAddress = serverAddress;
00120 mPort = port;
00121 mPasswordFile = passwordFile;
00122
00123 mChannelProxy = 0;
00124
00125 if (! Connect(mServerAddress, mPort, mPasswordFile))
00126 {
00127 ILOG_ERROR("Cannot connect to IDash server at " << mServerAddress
00128 << ':' << mPort);
00129 }
00130 }
00131
00132 bool
00133 Connect(CString serverAddress, int port, CString passwordFile)
00134 {
00135 ILOG_INFO("");
00136 ILOG_INFO("");
00137 ILOG_INFO("");
00138 ILOG_INFO("Connecting");
00139 mChannelProxy = new Util::ChannelProxy(serverAddress, port,
00140 passwordFile);
00141 if (!mChannelProxy->GetChannel())
00142 {
00143 ILOG_ERROR("Failed to connect to idash server");
00144 Disconnect();
00145 return false;
00146 }
00147
00148 return true;
00149 }
00150
00151 void
00152 Disconnect()
00153 {
00154 ILOG_INFO("Disconnecting");
00155 if (mChannelProxy)
00156 {
00157
00158 delete mChannelProxy;
00159 mChannelProxy = 0;
00160 }
00161 }
00162
00163 ILOG_VAR_DECL;
00164
00165 String mServerAddress;
00166 int mPort;
00167 String mPasswordFile;
00168
00169 Util::ChannelProxy* mChannelProxy;
00170
00171 };
00172
00173 ILOG_VAR_INIT(Client, Impala.Core.IDash);
00174
00175 }
00176 }
00177 }
00178
00179 #endif