00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00013
00014
00015
00016 #ifndef XRD_CONN_H
00017 #define XRD_CONN_H
00018
00019
00020 #include "XrdClient/XrdClientConst.hh"
00021
00022 #include "time.h"
00023 #include "XrdClient/XrdClientConnMgr.hh"
00024 #include "XrdClient/XrdClientMessage.hh"
00025 #include "XrdClient/XrdClientUrlInfo.hh"
00026 #include "XrdClient/XrdClientReadCache.hh"
00027 #include "XrdOuc/XrdOucHash.hh"
00028
00029 #define ConnectionManager XrdClientConn::GetConnectionMgr()
00030 #define SessionIDRepo XrdClientConn::GetSessionIDRepo()
00031
00032 class XrdClientAbs;
00033 class XrdSecProtocol;
00034
00035 class XrdClientConn {
00036
00037 public:
00038
00039 enum ESrvErrorHandlerRetval {
00040 kSEHRReturnMsgToCaller = 0,
00041 kSEHRBreakLoop = 1,
00042 kSEHRContinue = 2,
00043 kSEHRReturnNoMsgToCaller = 3,
00044 kSEHRRedirLimitReached = 4
00045 };
00046 enum EThreeStateReadHandler {
00047 kTSRHReturnMex = 0,
00048 kTSRHReturnNullMex = 1,
00049 kTSRHContinue = 2
00050 };
00051
00052
00053 struct SessionIDInfo {
00054 char id[16];
00055 };
00056
00057 int fLastDataBytesRecv;
00058 int fLastDataBytesSent;
00059 XErrorCode fOpenError;
00060
00061 XrdOucString fRedirOpaque;
00062
00063
00064 XrdClientConn();
00065 virtual ~XrdClientConn();
00066
00067 inline bool CacheWillFit(long long bytes) {
00068 if (!fMainReadCache)
00069 return FALSE;
00070 return fMainReadCache->WillFit(bytes);
00071 }
00072
00073 bool CheckHostDomain(XrdOucString hostToCheck);
00074 short Connect(XrdClientUrlInfo Host2Conn,
00075 XrdClientAbsUnsolMsgHandler *unsolhandler);
00076 void Disconnect(bool ForcePhysicalDisc);
00077 virtual bool GetAccessToSrv();
00078 XReqErrorType GoBackToRedirector();
00079
00080 XrdOucString GetClientHostDomain() { return fgClientHostDomain; }
00081
00082
00083 static XrdClientPhyConnection *GetPhyConn(int LogConnID);
00084
00085
00086
00087
00088 long GetDataFromCache(const void *buffer,
00089 long long begin_offs,
00090 long long end_offs,
00091 bool PerfCalc,
00092 XrdClientIntvList &missingblks,
00093 long &outstandingblks );
00094
00095 bool SubmitDataToCache(XrdClientMessage *xmsg,
00096 long long begin_offs,
00097 long long end_offs);
00098
00099 bool SubmitRawDataToCache(const void *buffer,
00100 long long begin_offs,
00101 long long end_offs);
00102
00103 void SubmitPlaceholderToCache(long long begin_offs,
00104 long long end_offs) {
00105 if (fMainReadCache)
00106 fMainReadCache->PutPlaceholder(begin_offs, end_offs);
00107 }
00108
00109
00110 void RemoveAllDataFromCache(bool keepwriteblocks=true) {
00111 if (fMainReadCache)
00112 fMainReadCache->RemoveItems(keepwriteblocks);
00113 }
00114
00115 void RemoveDataFromCache(long long begin_offs,
00116 long long end_offs, bool remove_overlapped = false) {
00117 if (fMainReadCache)
00118 fMainReadCache->RemoveItems(begin_offs, end_offs, remove_overlapped);
00119 }
00120
00121 void RemovePlaceholdersFromCache() {
00122 if (fMainReadCache)
00123 fMainReadCache->RemovePlaceholders();
00124 }
00125
00126 void PrintCache() {
00127 if (fMainReadCache)
00128 fMainReadCache->PrintCache();
00129 }
00130
00131
00132 bool GetCacheInfo(
00133
00134 int &size,
00135
00136
00137 long long &bytessubmitted,
00138
00139
00140 long long &byteshit,
00141
00142
00143
00144 long long &misscount,
00145
00146
00147 float &missrate,
00148
00149
00150 long long &readreqcnt,
00151
00152
00153 float &bytesusefulness
00154 ) {
00155 if (!fMainReadCache) return false;
00156
00157 fMainReadCache->GetInfo(size,
00158 bytessubmitted,
00159 byteshit,
00160 misscount,
00161 missrate,
00162 readreqcnt,
00163 bytesusefulness);
00164 return true;
00165 }
00166
00167
00168 void SetCacheSize(int CacheSize) {
00169 if (!fMainReadCache && CacheSize)
00170 fMainReadCache = new XrdClientReadCache();
00171
00172 if (fMainReadCache)
00173 fMainReadCache->SetSize(CacheSize);
00174 }
00175
00176 void SetCacheRmPolicy(int RmPolicy) {
00177 if (fMainReadCache)
00178 fMainReadCache->SetBlkRemovalPolicy(RmPolicy);
00179 }
00180
00181 void UnPinCacheBlk(long long begin_offs, long long end_offs) {
00182 fMainReadCache->UnPinCacheBlk(begin_offs, end_offs);
00183
00184 fWriteWaitAck->Broadcast();
00185 }
00186
00187
00188
00189
00190
00191 int GetLogConnID() const { return fLogConnID; }
00192
00193 ERemoteServerType GetServerType() const { return fServerType; }
00194
00195 kXR_unt16 GetStreamID() const { return fPrimaryStreamid; }
00196
00197 inline XrdClientUrlInfo *GetLBSUrl() { return fLBSUrl; }
00198 inline XrdClientUrlInfo GetCurrentUrl() { return fUrl; }
00199 inline XrdClientUrlInfo GetRedirUrl() { return fREQUrl; }
00200
00201 XErrorCode GetOpenError() const { return fOpenError; }
00202 virtual XReqErrorType GoToAnotherServer(XrdClientUrlInfo &newdest);
00203 bool IsConnected() const { return fConnected; }
00204 bool IsPhyConnConnected();
00205
00206 struct ServerResponseHeader
00207 LastServerResp;
00208
00209 struct ServerResponseBody_Error
00210 LastServerError;
00211
00212 void ClearLastServerError() {
00213 memset(&LastServerError, 0, sizeof(LastServerError));
00214 LastServerError.errnum = kXR_noErrorYet;
00215 }
00216
00217 UnsolRespProcResult ProcessAsynResp(XrdClientMessage *unsolmsg);
00218
00219 virtual bool SendGenCommand(ClientRequest *req,
00220 const void *reqMoreData,
00221 void **answMoreDataAllocated,
00222 void *answMoreData, bool HasToAlloc,
00223 char *CmdName, int substreamid = 0);
00224
00225 int GetOpenSockFD() const { return fOpenSockFD; }
00226
00227 void SetClientHostDomain(const char *src) { fgClientHostDomain = src; }
00228 void SetConnected(bool conn) { fConnected = conn; }
00229
00230 void SetOpenError(XErrorCode err) { fOpenError = err; }
00231
00232
00233 int GetParallelStreamToUse(int reqsperstream);
00234 int GetParallelStreamCount();
00235
00236 void SetRedirHandler(XrdClientAbs *rh) { fRedirHandler = rh; }
00237
00238 void SetRequestedDestHost(char *newh, kXR_int32 port) {
00239 fREQUrl = fUrl;
00240 fREQUrl.Host = newh;
00241 fREQUrl.Port = port;
00242 fREQUrl.SetAddrFromHost();
00243 }
00244
00245
00246
00247 void SetREQPauseState(kXR_int32 wsec) {
00248
00249 fREQWait->Lock();
00250
00251 if (wsec > 0)
00252 fREQWaitTimeLimit = time(0) + wsec;
00253 else {
00254 fREQWaitTimeLimit = 0;
00255 fREQWait->Broadcast();
00256 }
00257
00258
00259 fREQWait->UnLock();
00260 }
00261
00262
00263
00264
00265 void SetREQDelayedConnectState(kXR_int32 wsec) {
00266
00267 fREQConnectWait->Lock();
00268
00269 if (wsec > 0)
00270 fREQConnectWaitTimeLimit = time(0) + wsec;
00271 else {
00272 fREQConnectWaitTimeLimit = 0;
00273 fREQConnectWait->Broadcast();
00274 }
00275
00276
00277 fREQConnectWait->UnLock();
00278 }
00279
00280 void SetSID(kXR_char *sid);
00281 inline void SetUrl(XrdClientUrlInfo thisUrl) { fUrl = thisUrl; }
00282
00283
00284
00285
00286 XReqErrorType WriteToServer_Async(ClientRequest *req,
00287 const void* reqMoreData,
00288 int substreamid = 0);
00289
00290 static XrdClientConnectionMgr *GetConnectionMgr()
00291 { return fgConnectionMgr;}
00292
00293 static XrdOucHash<SessionIDInfo> &GetSessionIDRepo()
00294 { return fSessionIDRepo; }
00295
00296 void GetSessionID(SessionIDInfo &sess) {
00297 XrdOucString sessname;
00298 char buf[20];
00299
00300 snprintf(buf, 20, "%d", fUrl.Port);
00301
00302 sessname = fUrl.HostAddr;
00303 if (sessname.length() <= 0)
00304 sessname = fUrl.Host;
00305
00306 sessname += ":";
00307 sessname += buf;
00308
00309 sess = *( fSessionIDRepo.Find(sessname.c_str()) );
00310 }
00311
00312 long GetServerProtocol() { return fServerProto; }
00313
00314 short GetMaxRedirCnt() const { return fMaxGlobalRedirCnt; }
00315 void SetMaxRedirCnt(short mx) {fMaxGlobalRedirCnt = mx; }
00316 short GetRedirCnt() const { return fGlobalRedirCnt; }
00317
00318 bool DoWriteSoftCheckPoint();
00319 bool DoWriteHardCheckPoint();
00320 void UnPinCacheBlk();
00321
00322
00323
00324
00325 void SetOpTimeLimit(int delta_secs);
00326 bool IsOpTimeLimitElapsed(time_t timenow);
00327
00328
00329 protected:
00330 void SetLogConnID(int cid) { fLogConnID = cid; }
00331 void SetStreamID(kXR_unt16 sid) { fPrimaryStreamid = sid; }
00332
00333
00334
00335
00336 XrdClientAbsUnsolMsgHandler *fUnsolMsgHandler;
00337
00338 XrdClientUrlInfo fUrl;
00339 XrdClientUrlInfo *fLBSUrl;
00340 XrdClientUrlInfo fREQUrl;
00341
00342 short fGlobalRedirCnt;
00343
00344 private:
00345
00346 static XrdOucString fgClientHostDomain;
00347 bool fConnected;
00348 bool fGettingAccessToSrv;
00349 time_t fGlobalRedirLastUpdateTimestamp;
00350
00351 int fLogConnID;
00352 kXR_unt16 fPrimaryStreamid;
00353
00354
00355
00356 short fMaxGlobalRedirCnt;
00357 XrdClientReadCache *fMainReadCache;
00358
00359
00360 time_t fOpTimeLimit;
00361
00362 XrdClientAbs *fRedirHandler;
00363
00364
00365
00366 XrdOucString fRedirInternalToken;
00367
00368
00369 XrdSysCondVar *fREQWaitResp;
00370 ServerResponseBody_Attn_asynresp *
00371 fREQWaitRespData;
00372
00373 time_t fREQWaitTimeLimit;
00374 XrdSysCondVar *fREQWait;
00375 time_t fREQConnectWaitTimeLimit;
00376 XrdSysCondVar *fREQConnectWait;
00377
00378 long fServerProto;
00379 ERemoteServerType fServerType;
00380
00381 static XrdOucHash<SessionIDInfo>
00382 fSessionIDRepo;
00383
00384
00385
00386 int fOpenSockFD;
00387 static XrdClientConnectionMgr *fgConnectionMgr;
00388
00389 XrdSysCondVar *fWriteWaitAck;
00390 XrdClientVector<ClientRequest> fWriteReqsToRetry;
00391
00392 bool CheckErrorStatus(XrdClientMessage *, short &, char *);
00393 void CheckPort(int &port);
00394 void CheckREQPauseState();
00395 void CheckREQConnectWaitState();
00396 bool CheckResp(struct ServerResponseHeader *resp, const char *method);
00397 XrdClientMessage *ClientServerCmd(ClientRequest *req,
00398 const void *reqMoreData,
00399 void **answMoreDataAllocated,
00400 void *answMoreData,
00401 bool HasToAlloc,
00402 int substreamid = 0);
00403 XrdSecProtocol *DoAuthentication(char *plist, int plsiz);
00404
00405 ERemoteServerType DoHandShake(short log);
00406
00407 bool DoLogin();
00408 bool DomainMatcher(XrdOucString dom, XrdOucString domlist);
00409
00410 XrdOucString GetDomainToMatch(XrdOucString hostname);
00411
00412 ESrvErrorHandlerRetval HandleServerError(XReqErrorType &, XrdClientMessage *,
00413 ClientRequest *);
00414 bool MatchStreamid(struct ServerResponseHeader *ServerResponse);
00415
00416
00417
00418 bool PanicClose();
00419
00420 XrdOucString ParseDomainFromHostname(XrdOucString hostname);
00421
00422 XrdClientMessage *ReadPartialAnswer(XReqErrorType &, size_t &,
00423 ClientRequest *, bool, void**,
00424 EThreeStateReadHandler &);
00425
00426 void ClearSessionID();
00427
00428 XReqErrorType WriteToServer(ClientRequest *req,
00429 const void* reqMoreData,
00430 short LogConnID,
00431 int substreamid = 0);
00432
00433 bool WaitResp(int secsmax);
00434 };
00435
00436
00437
00438 #endif