xrootd
XrdClMessageUtils.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef __XRD_CL_MESSAGE_UTILS_HH__
00020 #define __XRD_CL_MESSAGE_UTILS_HH__
00021 
00022 #include "XrdCl/XrdClXRootDResponses.hh"
00023 #include "XrdCl/XrdClURL.hh"
00024 #include "XrdCl/XrdClMessage.hh"
00025 #include "XrdSys/XrdSysPthread.hh"
00026 #include <memory>
00027 
00028 namespace XrdCl
00029 {
00030   //----------------------------------------------------------------------------
00032   //----------------------------------------------------------------------------
00033   class SyncResponseHandler: public ResponseHandler
00034   {
00035     public:
00036       //------------------------------------------------------------------------
00038       //------------------------------------------------------------------------
00039       SyncResponseHandler():
00040         pStatus(0),
00041         pResponse(0),
00042         pSem( new XrdSysSemaphore(0) ) {}
00043 
00044       //------------------------------------------------------------------------
00046       //------------------------------------------------------------------------
00047       virtual ~SyncResponseHandler()
00048       {
00049         delete pSem;
00050       }
00051 
00052 
00053       //------------------------------------------------------------------------
00055       //------------------------------------------------------------------------
00056       virtual void HandleResponse( XRootDStatus *status,
00057                                    AnyObject    *response )
00058       {
00059         pStatus = status;
00060         pResponse = response;
00061         pSem->Post();
00062       }
00063 
00064       //------------------------------------------------------------------------
00066       //------------------------------------------------------------------------
00067       XRootDStatus *GetStatus()
00068       {
00069         return pStatus;
00070       }
00071 
00072       //------------------------------------------------------------------------
00074       //------------------------------------------------------------------------
00075       AnyObject *GetResponse()
00076       {
00077         return pResponse;
00078       }
00079 
00080       //------------------------------------------------------------------------
00082       //------------------------------------------------------------------------
00083       void WaitForResponse()
00084       {
00085         pSem->Wait();
00086       }
00087 
00088     private:
00089       XRootDStatus    *pStatus;
00090       AnyObject       *pResponse;
00091       XrdSysSemaphore *pSem;
00092   };
00093 
00094   //----------------------------------------------------------------------------
00095   // Sending parameters
00096   //----------------------------------------------------------------------------
00097   struct MessageSendParams
00098   {
00099     MessageSendParams():
00100       timeout(0), expires(0), followRedirects(true), stateful(true),
00101       hostList(0), chunkList(0), redirectLimit(0) {}
00102     uint16_t         timeout;
00103     time_t           expires;
00104     const HostInfo   loadBalancer;
00105     bool             followRedirects;
00106     bool             stateful;
00107     HostList        *hostList;
00108     ChunkList       *chunkList;
00109     uint16_t         redirectLimit;
00110   };
00111 
00112   class MessageUtils
00113   {
00114     public:
00115       //------------------------------------------------------------------------
00117       //------------------------------------------------------------------------
00118       static XRootDStatus WaitForStatus( SyncResponseHandler *handler )
00119       {
00120         handler->WaitForResponse();
00121         XRootDStatus *status = handler->GetStatus();
00122         XRootDStatus ret( *status );
00123         delete status;
00124         return ret;
00125       }
00126 
00127       //------------------------------------------------------------------------
00129       //------------------------------------------------------------------------
00130       template<class Type>
00131       static XrdCl::XRootDStatus WaitForResponse(
00132                             SyncResponseHandler  *handler,
00133                             Type                *&response )
00134       {
00135         handler->WaitForResponse();
00136 
00137         std::auto_ptr<AnyObject> resp( handler->GetResponse() );
00138         XRootDStatus *status = handler->GetStatus();
00139         XRootDStatus ret( *status );
00140         delete status;
00141 
00142         if( ret.IsOK() )
00143         {
00144           if( !resp.get() )
00145             return XRootDStatus( stError, errInternal );
00146           resp->Get( response );
00147           resp->Set( (int *)0 );
00148           if( !response )
00149             return XRootDStatus( stError, errInternal );
00150         }
00151 
00152         return ret;
00153       }
00154 
00155       //------------------------------------------------------------------------
00157       //------------------------------------------------------------------------
00158       template<class Type>
00159       static void CreateRequest( Message  *&msg,
00160                                  Type     *&req,
00161                                  uint32_t  payloadSize = 0 )
00162       {
00163         msg = new Message( sizeof(Type)+payloadSize );
00164         req = (Type*)msg->GetBuffer();
00165         msg->Zero();
00166       }
00167 
00168       //------------------------------------------------------------------------
00170       //------------------------------------------------------------------------
00171       static Status SendMessage( const URL               &url,
00172                                  Message                 *msg,
00173                                  ResponseHandler         *handler,
00174                                  const MessageSendParams &sendParams );
00175 
00176       //------------------------------------------------------------------------
00178       //------------------------------------------------------------------------
00179       static void ProcessSendParams( MessageSendParams &sendParams );
00180 
00181       //------------------------------------------------------------------------
00189       //------------------------------------------------------------------------
00190       static void AppendCGI( Message              *msg,
00191                              const URL::ParamsMap &newCgi,
00192                              bool                  replace );
00193 
00194       //------------------------------------------------------------------------
00202       //------------------------------------------------------------------------
00203       static void MergeCGI( URL::ParamsMap       &cgi1,
00204                             const URL::ParamsMap &cgi2,
00205                             bool                  replace );                             
00206   };
00207 }
00208 
00209 #endif // __XRD_CL_MESSAGE_UTILS_HH__