xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
XrdClMessageUtils.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef __XRD_CL_MESSAGE_UTILS_HH__
26 #define __XRD_CL_MESSAGE_UTILS_HH__
27 
29 #include "XrdCl/XrdClURL.hh"
30 #include "XrdCl/XrdClMessage.hh"
31 #include "XrdCl/XrdClUglyHacks.hh"
32 
33 namespace XrdCl
34 {
35  class LocalFileHandler;
36 
37  class XAttr;
38 
39  //----------------------------------------------------------------------------
41  //----------------------------------------------------------------------------
43  {
44  public:
45  //------------------------------------------------------------------------
47  //------------------------------------------------------------------------
49  pStatus(0),
50  pResponse(0),
51  pCondVar(0) {}
52 
53  //------------------------------------------------------------------------
55  //------------------------------------------------------------------------
57  {
58  }
59 
60 
61  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
64  virtual void HandleResponse( XRootDStatus *status,
65  AnyObject *response )
66  {
67  XrdSysCondVarHelper scopedLock(pCondVar);
68  pStatus = status;
69  pResponse = response;
71  }
72 
73  //------------------------------------------------------------------------
75  //------------------------------------------------------------------------
77  {
78  return pStatus;
79  }
80 
81  //------------------------------------------------------------------------
83  //------------------------------------------------------------------------
85  {
86  return pResponse;
87  }
88 
89  //------------------------------------------------------------------------
91  //------------------------------------------------------------------------
93  {
94  XrdSysCondVarHelper scopedLock(pCondVar);
95  while (pStatus == 0) {
96  pCondVar.Wait();
97  }
98  }
99 
100  private:
103 
107  };
108 
109 
110  //----------------------------------------------------------------------------
111  // We're not interested in the response just commit suicide
112  //----------------------------------------------------------------------------
114  {
115  public:
116  //------------------------------------------------------------------------
117  // Handle the response
118  //------------------------------------------------------------------------
120  XrdCl::AnyObject *response,
121  XrdCl::HostList *hostList )
122  {
123  delete this;
124  }
125  };
126 
127  //----------------------------------------------------------------------------
128  // Sending parameters
129  //----------------------------------------------------------------------------
131  {
133  timeout(0), expires(0), followRedirects(true), chunkedResponse(false),
134  stateful(true), hostList(0), chunkList(0), redirectLimit(0) {}
135  uint16_t timeout;
136  time_t expires;
140  bool stateful;
143  uint16_t redirectLimit;
144  };
145 
147  {
148  public:
149  //------------------------------------------------------------------------
151  //------------------------------------------------------------------------
153  {
154  handler->WaitForResponse();
155  XRootDStatus *status = handler->GetStatus();
156  XRootDStatus ret( *status );
157  delete status;
158  return ret;
159  }
160 
161  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  template<class Type>
166  SyncResponseHandler *handler,
167  Type *&response )
168  {
169  handler->WaitForResponse();
170 
171  AnyObject *resp = handler->GetResponse();
172  XRootDStatus *status = handler->GetStatus();
173  XRootDStatus ret( *status );
174  delete status;
175 
176  if( ret.IsOK() )
177  {
178  if( !resp )
179  return XRootDStatus( stError, errInternal );
180  resp->Get( response );
181  resp->Set( (int *)0 );
182  delete resp;
183 
184  if( !response )
185  return XRootDStatus( stError, errInternal );
186  }
187 
188  return ret;
189  }
190 
191  //------------------------------------------------------------------------
193  //------------------------------------------------------------------------
194  template<class Request>
195  static void CreateRequest( Message *&msg,
196  Request *&req,
197  uint32_t payloadSize = 0 )
198  {
199  msg = new Message( sizeof(Request) + payloadSize );
200  req = (Request*)msg->GetBuffer();
201  msg->Zero();
202  }
203 
204  //------------------------------------------------------------------------
206  //------------------------------------------------------------------------
207  static XRootDStatus SendMessage( const URL &url,
208  Message *msg,
209  ResponseHandler *handler,
210  const MessageSendParams &sendParams,
211  LocalFileHandler *lFileHandler );
212 
213  //------------------------------------------------------------------------
215  //------------------------------------------------------------------------
216  static Status RedirectMessage( const URL &url,
217  Message *msg,
218  ResponseHandler *handler,
219  MessageSendParams &sendParams,
220  LocalFileHandler *lFileHandler );
221 
222  //------------------------------------------------------------------------
224  //------------------------------------------------------------------------
225  static void ProcessSendParams( MessageSendParams &sendParams );
226 
227  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
238  static void RewriteCGIAndPath( Message *msg,
239  const URL::ParamsMap &newCgi,
240  bool replace,
241  const std::string &newPath );
242 
243  //------------------------------------------------------------------------
251  //------------------------------------------------------------------------
252  static void MergeCGI( URL::ParamsMap &cgi1,
253  const URL::ParamsMap &cgi2,
254  bool replace );
255 
256  //------------------------------------------------------------------------
262  //------------------------------------------------------------------------
263  static Status CreateXAttrVec( const std::vector<xattr_t> &attrs,
264  std::vector<char> &avec );
265 
266  //------------------------------------------------------------------------
271  //------------------------------------------------------------------------
272  static Status CreateXAttrVec( const std::vector<std::string> &attrs,
273  std::vector<char> &nvec );
274 
275  //------------------------------------------------------------------------
281  //------------------------------------------------------------------------
282  template<typename T>
284  const std::vector<T> &vec,
285  const std::string &path = "" )
286  {
287  ClientRequestHdr *hdr = reinterpret_cast<ClientRequestHdr*>( msg->GetBuffer() );
288 
289  std::vector<char> xattrvec;
290  Status st = MessageUtils::CreateXAttrVec( vec, xattrvec );
291  if( !st.IsOK() )
292  return st;
293 
294  // update body size in the header
295  hdr->dlen = path.size() + 1;
296  hdr->dlen += xattrvec.size();
297 
298  // append the body
299  size_t offset = sizeof( ClientRequestHdr );
300  msg->Append( path.c_str(), path.size() + 1, offset );
301  offset += path.size() + 1;
302  msg->Append( xattrvec.data(), xattrvec.size(), offset );
303 
304  return Status();
305  }
306  };
307 }
308 
309 #endif // __XRD_CL_MESSAGE_UTILS_HH__
Definition: XrdClMessageUtils.hh:113
Definition: XrdClAnyObject.hh:32
static XRootDStatus SendMessage(const URL &url, Message *msg, ResponseHandler *handler, const MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Send message.
std::map< std::string, std::string > ParamsMap
Definition: XrdClURL.hh:33
Definition: XrdClMessageUtils.hh:130
uint16_t redirectLimit
Definition: XrdClMessageUtils.hh:143
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition: XrdClXRootDResponses.hh:904
XRootDStatus * GetStatus()
Get the status.
Definition: XrdClMessageUtils.hh:76
void Get(Type &object)
Retrieve the object being held.
Definition: XrdClAnyObject.hh:78
The message representation used throughout the system.
Definition: XrdClMessage.hh:29
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
Definition: XrdClMessageUtils.hh:152
virtual void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList)
Definition: XrdClMessageUtils.hh:119
bool followRedirects
Definition: XrdClMessageUtils.hh:138
bool chunkedResponse
Definition: XrdClMessageUtils.hh:139
uint16_t timeout
Definition: XrdClMessageUtils.hh:135
Synchronize the response.
Definition: XrdClMessageUtils.hh:42
SyncResponseHandler()
Constructor.
Definition: XrdClMessageUtils.hh:48
static XrdCl::XRootDStatus WaitForResponse(SyncResponseHandler *handler, Type *&response)
Wait for the response.
Definition: XrdClMessageUtils.hh:165
static Status CreateXAttrVec(const std::vector< xattr_t > &attrs, std::vector< char > &avec)
static void MergeCGI(URL::ParamsMap &cgi1, const URL::ParamsMap &cgi2, bool replace)
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
Procedure execution status.
Definition: XrdClStatus.hh:110
SyncResponseHandler & operator=(const SyncResponseHandler &other)
Definition: XrdClMessageUtils.hh:146
virtual ~SyncResponseHandler()
Destructor.
Definition: XrdClMessageUtils.hh:56
AnyObject * GetResponse()
Get the response.
Definition: XrdClMessageUtils.hh:84
static void RewriteCGIAndPath(Message *msg, const URL::ParamsMap &newCgi, bool replace, const std::string &newPath)
Definition: XrdClXRootDResponses.hh:957
std::vector< HostInfo > HostList
Definition: XrdClXRootDResponses.hh:969
Definition: XrdSysPthread.hh:78
void Set(Type object, bool own=true)
Definition: XrdClAnyObject.hh:59
kXR_int32 dlen
Definition: XProtocol.hh:152
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
Request status.
Definition: XrdClXRootDResponses.hh:214
ChunkList * chunkList
Definition: XrdClMessageUtils.hh:142
HostList * hostList
Definition: XrdClMessageUtils.hh:141
void WaitForResponse()
Wait for the arrival of the response.
Definition: XrdClMessageUtils.hh:92
HostInfo loadBalancer
Definition: XrdClMessageUtils.hh:137
Definition: XProtocol.hh:148
void Broadcast()
Definition: XrdSysPthread.hh:89
Definition: XrdSysPthread.hh:129
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition: XrdClBuffer.hh:164
MessageSendParams()
Definition: XrdClMessageUtils.hh:132
bool stateful
Definition: XrdClMessageUtils.hh:140
Handle an async response.
Definition: XrdClXRootDResponses.hh:974
Definition: XrdClLocalFileHandler.hh:32
URL representation.
Definition: XrdClURL.hh:30
time_t expires
Definition: XrdClMessageUtils.hh:136
const uint16_t errInternal
Internal error.
Definition: XrdClStatus.hh:55
static Status CreateXAttrBody(Message *msg, const std::vector< T > &vec, const std::string &path="")
Definition: XrdClMessageUtils.hh:283
AnyObject * pResponse
Definition: XrdClMessageUtils.hh:105
bool IsOK() const
We&#39;re fine.
Definition: XrdClStatus.hh:120
XrdSysCondVar pCondVar
Definition: XrdClMessageUtils.hh:106
XRootDStatus * pStatus
Definition: XrdClMessageUtils.hh:104
static void ProcessSendParams(MessageSendParams &sendParams)
Process sending params.
void Zero()
Zero.
Definition: XrdClBuffer.hh:124
static Status RedirectMessage(const URL &url, Message *msg, ResponseHandler *handler, MessageSendParams &sendParams, LocalFileHandler *lFileHandler)
Redirect message.
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Handle the response.
Definition: XrdClMessageUtils.hh:64
static void CreateRequest(Message *&msg, Request *&req, uint32_t payloadSize=0)
Create a message.
Definition: XrdClMessageUtils.hh:195