xrootd
XrdClientPhyConnection.hh
Go to the documentation of this file.
00001 #ifndef _XrdClientPhyConnection
00002 #define _XrdClientPhyConnection
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*           X r d C l i e n t P h y C o n n e c t i o n . h h                */
00006 /*                                                                            */
00007 /* Author: Fabrizio Furano (INFN Padova, 2004)                                */
00008 /* Adapted from TXNetFile (root.cern.ch) originally done by                   */
00009 /*  Alvise Dorigo, Fabrizio Furano                                            */
00010 /*          INFN Padova, 2003                                                 */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00034 //                                                                      //
00035 // Class handling physical connections to xrootd servers                //
00036 //                                                                      //
00038 
00039 
00040 #include "XrdClient/XrdClientSock.hh"
00041 #include "XrdClient/XrdClientMessage.hh"
00042 #include "XrdClient/XrdClientUnsolMsg.hh"
00043 #include "XrdClient/XrdClientInputBuffer.hh"
00044 #include "XrdClient/XrdClientUrlInfo.hh"
00045 #include "XrdSys/XrdSysPthread.hh"
00046 #include "XrdSys/XrdSysSemWait.hh"
00047 
00048 #include <time.h> // for time_t data type
00049 
00050 enum ELoginState {
00051     kNo      = 0,
00052     kYes     = 1, 
00053     kPending = 2
00054 };
00055 
00056 enum ERemoteServerType {
00057     kSTError      = -1,  // Some error occurred: server type undetermined 
00058     kSTNone       = 0,   // Remote server type un-recognized
00059     kSTRootd      = 1,   // Remote server type: old rootd server
00060     kSTBaseXrootd = 2,   // Remote server type: xrootd dynamic load balancer
00061     kSTDataXrootd = 3,   // Remote server type: xrootd data server
00062     kSTMetaXrootd = 4    // Remote server type: xrootd meta manager
00063 }; 
00064 
00065 class XrdClientSid;
00066 class XrdClientThread;
00067 class XrdSecProtocol;
00068 
00069 class XrdClientPhyConnection: public XrdClientUnsolMsgSender {
00070 
00071 private:
00072     time_t              fLastUseTimestamp;
00073     enum ELoginState    fLogged;       // only 1 login/auth is needed for physical  
00074     XrdSecProtocol     *fSecProtocol;  // authentication protocol
00075 
00076     XrdClientInputBuffer
00077     fMsgQ;         // The queue used to hold incoming messages
00078 
00079     int                 fRequestTimeout;
00080     bool                fMStreamsGoing;
00081     XrdSysRecMutex         fRwMutex;     // Lock before using the physical channel 
00082     // (for reading and/or writing)
00083 
00084     XrdSysRecMutex         fMutex;
00085     XrdSysRecMutex         fMultireadMutex; // Used to arbitrate between multiple
00086                                             // threads reading msgs from the same conn
00087 
00088     XrdClientThread     *fReaderthreadhandler[64]; // The thread which is going to pump
00089     // out the data from the socket
00090 
00091     int                  fReaderthreadrunning;
00092 
00093     XrdClientUrlInfo          fServer;
00094 
00095     XrdClientSock       *fSocket;
00096 
00097     UnsolRespProcResult HandleUnsolicited(XrdClientMessage *m);
00098 
00099     XrdSysSemWait       fReaderCV;
00100 
00101     short               fLogConnCnt; // Number of logical connections using this phyconn
00102 
00103     XrdClientSid       *fSidManager;
00104 
00105 public:
00106     long                fServerProto;        // The server protocol
00107     ERemoteServerType   fServerType;
00108     long                fTTLsec;
00109 
00110     XrdClientPhyConnection(XrdClientAbsUnsolMsgHandler *h, XrdClientSid *sid);
00111     ~XrdClientPhyConnection();
00112 
00113     XrdClientMessage     *BuildMessage(bool IgnoreTimeouts, bool Enqueue);
00114     bool                  CheckAutoTerm();
00115 
00116     bool           Connect(XrdClientUrlInfo RemoteHost, bool isUnix = 0);
00117 
00118     //--------------------------------------------------------------------------
00126     //--------------------------------------------------------------------------
00127     bool Connect( XrdClientUrlInfo RemoteHost, bool isUnix , int fd );
00128 
00129     void           CountLogConn(int d = 1);
00130     void           Disconnect();
00131 
00132     ERemoteServerType
00133     DoHandShake(ServerInitHandShake &xbody,
00134                 int substreamid = 0);
00135 
00136     bool           ExpiredTTL();
00137     short          GetLogConnCnt() const { return fLogConnCnt; }
00138     int            GetReaderThreadsCnt() { XrdSysMutexHelper l(fMutex); return fReaderthreadrunning; }
00139 
00140     long           GetTTL() { return fTTLsec; }
00141 
00142     XrdSecProtocol *GetSecProtocol() const { return fSecProtocol; }
00143     int            GetSocket() { return fSocket ? fSocket->fSocket : -1; }
00144 
00145     // Tells to the sock to rebuild the list of interesting selectors
00146     void           ReinitFDTable() { if (fSocket) fSocket->ReinitFDTable(); }
00147 
00148     int            SaveSocket() { fTTLsec = 0; return fSocket ? (fSocket->SaveSocket()) : -1; }
00149     void           SetInterrupt() { if (fSocket) fSocket->SetInterrupt(); }
00150     void           SetSecProtocol(XrdSecProtocol *sp) { fSecProtocol = sp; }
00151 
00152     void           StartedReader();
00153 
00154     bool           IsAddress(const XrdOucString &addr) {
00155         return ( (fServer.Host == addr) ||
00156                  (fServer.HostAddr == addr) );
00157     }
00158 
00159     ELoginState    IsLogged();
00160 
00161     bool           IsPort(int port) { return (fServer.Port == port); };
00162     bool           IsUser(const XrdOucString &usr) { return (fServer.User == usr); };
00163     bool           IsValid();
00164     
00165 
00166     void           LockChannel();
00167 
00168     // see XrdClientSock for the meaning of the parameters
00169     int            ReadRaw(void *buffer, int BufferLength, int substreamid = -1,
00170                            int *usedsubstreamid = 0);
00171 
00172     XrdClientMessage     *ReadMessage(int streamid);
00173     bool           ReConnect(XrdClientUrlInfo RemoteHost);
00174     void           SetLogged(ELoginState status) { fLogged = status; }
00175     inline void    SetTTL(long ttl) { fTTLsec = ttl; }
00176     void           StartReader();
00177     void           Touch();
00178     void           UnlockChannel();
00179     int            WriteRaw(const void *buffer, int BufferLength, int substreamid = 0);
00180 
00181     int TryConnectParallelStream(int port, int windowsz, int sockid) { return ( fSocket ? fSocket->TryConnectParallelSock(port, windowsz, sockid) : -1); }
00182     int EstablishPendingParallelStream(int tmpid, int newid) { return ( fSocket ? fSocket->EstablishParallelSock(tmpid, newid) : -1); }
00183     void RemoveParallelStream(int substreamid) { if (fSocket) fSocket->RemoveParallelSock(substreamid); }
00184     // Tells if the attempt to establish the parallel streams is ongoing or was done
00185     //  and mark it as ongoing or done
00186     bool TestAndSetMStreamsGoing();
00187 
00188     int GetSockIdHint(int reqsperstream) { return ( fSocket ? fSocket->GetSockIdHint(reqsperstream) : 0); }
00189     int GetSockIdCount() {return ( fSocket ? fSocket->GetSockIdCount() : 0); }
00190     void PauseSelectOnSubstream(int substreamid) { if (fSocket) fSocket->PauseSelectOnSubstream(substreamid); }
00191     void RestartSelectOnSubstream(int substreamid) { if (fSocket) fSocket->RestartSelectOnSubstream(substreamid); }
00192 
00193     // To prohibit/re-enable a socket descriptor from being looked at by the reader threads
00194     virtual void BanSockDescr(int sockdescr, int sockid) { if (fSocket) fSocket->BanSockDescr(sockdescr, sockid); }
00195     virtual void UnBanSockDescr(int sockdescr) { if (fSocket) fSocket->UnBanSockDescr(sockdescr); }
00196 
00197     void ReadLock() { fMultireadMutex.Lock(); }
00198     void ReadUnLock() { fMultireadMutex.UnLock(); }
00199 
00200    int WipeStreamid(int streamid) { return fMsgQ.WipeStreamid(streamid); }
00201 };
00202 
00203 
00204 
00205 
00206 //
00207 // Class implementing a trick to automatically unlock an XrdClientPhyConnection
00208 //
00209 class XrdClientPhyConnLocker {
00210 private:
00211     XrdClientPhyConnection *phyconn;
00212 
00213 public:
00214     XrdClientPhyConnLocker(XrdClientPhyConnection *phyc) {
00215         // Constructor
00216         phyconn = phyc;
00217         phyconn->LockChannel();
00218     }
00219 
00220     ~XrdClientPhyConnLocker(){
00221         // Destructor. 
00222         phyconn->UnlockChannel();
00223     }
00224 
00225 };
00226 #endif