/
XrdClMessageUtils.hh
211 lines (189 loc) · 7.94 KB
/
XrdClMessageUtils.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#ifndef __XRD_CL_MESSAGE_UTILS_HH__
#define __XRD_CL_MESSAGE_UTILS_HH__
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClMessage.hh"
#include "XrdCl/XrdClUglyHacks.hh"
namespace XrdCl
{
//----------------------------------------------------------------------------
//! Synchronize the response
//----------------------------------------------------------------------------
class SyncResponseHandler: public ResponseHandler
{
public:
//------------------------------------------------------------------------
//! Constructor
//------------------------------------------------------------------------
SyncResponseHandler():
pStatus(0),
pResponse(0),
pSem( new Semaphore(0) ) {}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
virtual ~SyncResponseHandler()
{
delete pSem;
}
//------------------------------------------------------------------------
//! Handle the response
//------------------------------------------------------------------------
virtual void HandleResponse( XRootDStatus *status,
AnyObject *response )
{
pStatus = status;
pResponse = response;
pSem->Post();
}
//------------------------------------------------------------------------
//! Get the status
//------------------------------------------------------------------------
XRootDStatus *GetStatus()
{
return pStatus;
}
//------------------------------------------------------------------------
//! Get the response
//------------------------------------------------------------------------
AnyObject *GetResponse()
{
return pResponse;
}
//------------------------------------------------------------------------
//! Wait for the arrival of the response
//------------------------------------------------------------------------
void WaitForResponse()
{
pSem->Wait();
}
private:
XRootDStatus *pStatus;
AnyObject *pResponse;
Semaphore *pSem;
};
//----------------------------------------------------------------------------
// Sending parameters
//----------------------------------------------------------------------------
struct MessageSendParams
{
MessageSendParams():
timeout(0), expires(0), followRedirects(true), stateful(true),
hostList(0), chunkList(0), redirectLimit(0) {}
uint16_t timeout;
time_t expires;
const HostInfo loadBalancer;
bool followRedirects;
bool stateful;
HostList *hostList;
ChunkList *chunkList;
uint16_t redirectLimit;
};
class MessageUtils
{
public:
//------------------------------------------------------------------------
//! Wait and return the status of the query
//------------------------------------------------------------------------
static XRootDStatus WaitForStatus( SyncResponseHandler *handler )
{
handler->WaitForResponse();
XRootDStatus *status = handler->GetStatus();
XRootDStatus ret( *status );
delete status;
return ret;
}
//------------------------------------------------------------------------
//! Wait for the response
//------------------------------------------------------------------------
template<class Type>
static XrdCl::XRootDStatus WaitForResponse(
SyncResponseHandler *handler,
Type *&response )
{
handler->WaitForResponse();
AnyObject *resp = handler->GetResponse();
XRootDStatus *status = handler->GetStatus();
XRootDStatus ret( *status );
delete status;
if( ret.IsOK() )
{
if( resp )
return XRootDStatus( stError, errInternal );
resp->Get( response );
resp->Set( (int *)0 );
if( !response )
{
delete resp;
return XRootDStatus( stError, errInternal );
}
}
return ret;
}
//------------------------------------------------------------------------
//! Create a message
//------------------------------------------------------------------------
template<class Type>
static void CreateRequest( Message *&msg,
Type *&req,
uint32_t payloadSize = 0 )
{
msg = new Message( sizeof(Type)+payloadSize );
req = (Type*)msg->GetBuffer();
msg->Zero();
}
//------------------------------------------------------------------------
//! Send message
//------------------------------------------------------------------------
static Status SendMessage( const URL &url,
Message *msg,
ResponseHandler *handler,
const MessageSendParams &sendParams );
//------------------------------------------------------------------------
//! Process sending params
//------------------------------------------------------------------------
static void ProcessSendParams( MessageSendParams &sendParams );
//------------------------------------------------------------------------
//! Append cgi to the on already present in the message
//!
//! @param msg message concerned
//! @param newCgi the new cgi
//! @param replace indicates whether, in case of a conflict, the new CGI
//! parameter should replace an existing one or be
//! appended to it using a comma
//------------------------------------------------------------------------
static void AppendCGI( Message *msg,
const URL::ParamsMap &newCgi,
bool replace );
//------------------------------------------------------------------------
//! Merge cgi2 into cgi1
//!
//! @param cgi1 cgi to be merged into
//! @param cgi2 cgi to be merged in
//! @param replace indicates whether, in case of a conflict, the new CGI
//! parameter should replace an existing one or be
//! appended to it using a comma
//------------------------------------------------------------------------
static void MergeCGI( URL::ParamsMap &cgi1,
const URL::ParamsMap &cgi2,
bool replace );
};
}
#endif // __XRD_CL_MESSAGE_UTILS_HH__