00001 #ifndef Impala_Util_ChannelProxy_h
00002 #define Impala_Util_ChannelProxy_h
00003
00004 #include "Util/ChannelPool.h"
00005 #include "Util/Channel.h"
00006
00007 namespace Impala
00008 {
00009 namespace Util
00010 {
00011
00012
00013
00014
00015
00016
00017 class ChannelProxy
00018 {
00019
00020 public:
00021
00022 ChannelProxy(CString serverName, int port, CString passwordFile) :
00023 mServer(serverName), mPort(port), mPasswordFile(passwordFile),
00024 mChannel(0)
00025 {
00026 }
00027
00028 virtual
00029 ~ChannelProxy()
00030 {
00031 }
00032
00033 bool
00034 GetChannel()
00035 {
00036 ILOG_INFO("Need a channel to " << mServer << ":" << mPort);
00037 mChannel = ChannelPool::Instance().Get(mServer, mPort, mPasswordFile);
00038 if (ChannelIsValid())
00039 {
00040 ILOG_INFO("Open channel to " << mServer << " available at port " <<
00041 mChannel->GetPortNumber(0));
00042 return true;
00043 }
00044 else
00045 {
00046 ILOG_ERROR("No valid channel to " << mServer << ":" << mPort);
00047 return false;
00048 }
00049 }
00050
00051 bool
00052 ChannelIsValid()
00053 {
00054 return (mChannel && mChannel->Valid());
00055 }
00056
00057 String
00058 Send(CString msg)
00059 {
00060 if (!ChannelIsValid())
00061 {
00062 ILOG_ERROR("Invalid channel, cannot send");
00063 return "ERROR";
00064 }
00065
00066 ILOG_DEBUG("Sending: " << msg);
00067
00068 char* buf = mChannel->Buffer();
00069 sprintf(buf, "%s\0", msg.c_str());
00070 int len = mChannel->SendRequest(strlen(buf) + 1);
00071
00072 if (len < 0)
00073 {
00074 ILOG_ERROR("Send failed (" << len << "); closing channel");
00075 ChannelPool::Instance().Remove(mServer, mPort);
00076 mChannel = 0;
00077 return "ERROR: 503: Send failed; closing channel";
00078 }
00079
00080 buf[len] = 0;
00081 ILOG_DEBUG("Response: " << buf);
00082 return String(buf);
00083 }
00084
00085 private:
00086
00087 String mServer;
00088 int mPort;
00089 String mPasswordFile;
00090 Channel* mChannel;
00091
00092 ILOG_VAR_DECL;
00093
00094 };
00095
00096 ILOG_VAR_INIT(ChannelProxy, Impala.Util);
00097
00098 }
00099 }
00100
00101 #endif