-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClChannel.cc
396 lines (348 loc) · 13.8 KB
/
XrdClChannel.cc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//------------------------------------------------------------------------------
// Copyright (c) 2011-2014 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// 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/>
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClChannel.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClStream.hh"
#include "XrdCl/XrdClSocket.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClUglyHacks.hh"
#include "XrdCl/XrdClRedirectorRegistry.hh"
#include "XrdCl/XrdClXRootDTransport.hh"
#include "XrdSys/XrdSysPthread.hh"
#include <ctime>
namespace
{
//----------------------------------------------------------------------------
// Filter handler
//----------------------------------------------------------------------------
class FilterHandler: public XrdCl::IncomingMsgHandler
{
public:
//------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------
FilterHandler( XrdCl::MessageFilter *filter ):
pSem( new XrdCl::Semaphore(0) ), pFilter( filter ), pMsg( 0 )
{
}
//------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------
virtual ~FilterHandler()
{
delete pSem;
}
//------------------------------------------------------------------------
// Message handler
//------------------------------------------------------------------------
virtual uint16_t Examine( XrdCl::Message *msg )
{
if( pFilter->Filter( msg ) )
return Take | RemoveHandler;
return Ignore;
}
virtual void Process( XrdCl::Message *msg )
{
pMsg = msg;
pSem->Post();
}
//------------------------------------------------------------------------
// Handle a fault
//------------------------------------------------------------------------
virtual uint8_t OnStreamEvent( StreamEvent event,
XrdCl::Status status )
{
if( event == Ready )
return 0;
pStatus = status;
pSem->Post();
return RemoveHandler;
}
//------------------------------------------------------------------------
// Wait for a status of the message
//------------------------------------------------------------------------
XrdCl::Status WaitForStatus()
{
pSem->Wait();
return pStatus;
}
//------------------------------------------------------------------------
// Wait for the arrival of the message
//------------------------------------------------------------------------
XrdCl::Message *GetMessage()
{
return pMsg;
}
//------------------------------------------------------------------------
// Get underlying message filter sid
//------------------------------------------------------------------------
uint16_t GetSid() const
{
if (pFilter)
return pFilter->GetSid();
return 0;
}
private:
FilterHandler(const FilterHandler &other);
FilterHandler &operator = (const FilterHandler &other);
XrdCl::Semaphore *pSem;
XrdCl::MessageFilter *pFilter;
XrdCl::Message *pMsg;
XrdCl::Status pStatus;
};
//----------------------------------------------------------------------------
// Status handler
//----------------------------------------------------------------------------
class StatusHandler: public XrdCl::OutgoingMsgHandler
{
public:
//------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------
StatusHandler( XrdCl::Message *msg ):
pSem( new XrdCl::Semaphore(0) ),
pMsg( msg ) {}
//------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------
virtual ~StatusHandler()
{
delete pSem;
}
//------------------------------------------------------------------------
// Handle the status information
//------------------------------------------------------------------------
void OnStatusReady( const XrdCl::Message *message,
XrdCl::Status status )
{
if( pMsg == message )
pStatus = status;
pSem->Post();
}
//------------------------------------------------------------------------
// Wait for the status to be ready
//------------------------------------------------------------------------
XrdCl::Status WaitForStatus()
{
pSem->Wait();
return pStatus;
}
private:
StatusHandler(const StatusHandler &other);
StatusHandler &operator = (const StatusHandler &other);
XrdCl::Semaphore *pSem;
XrdCl::Status pStatus;
XrdCl::Message *pMsg;
};
}
namespace XrdCl
{
class TickGeneratorTask: public XrdCl::Task
{
public:
//------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------
TickGeneratorTask( XrdCl::Channel *channel, const std::string &hostId ):
pChannel( channel )
{
std::string name = "TickGeneratorTask for: ";
name += hostId;
SetName( name );
}
//------------------------------------------------------------------------
// Run the task
//------------------------------------------------------------------------
time_t Run( time_t now )
{
XrdSysMutexHelper lck( pMtx );
if( !pChannel ) return 0;
using namespace XrdCl;
pChannel->Tick( now );
Env *env = DefaultEnv::GetEnv();
int timeoutResolution = DefaultTimeoutResolution;
env->GetInt( "TimeoutResolution", timeoutResolution );
return now+timeoutResolution;
}
void Invalidate()
{
XrdSysMutexHelper lck( pMtx );
pChannel = 0;
}
private:
XrdCl::Channel *pChannel;
XrdSysMutex pMtx;
};
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
Channel::Channel( const URL &url,
Poller *poller,
TransportHandler *transport,
TaskManager *taskManager,
JobManager *jobManager ):
pUrl( url.GetHostId() ),
pPoller( poller ),
pTransport( transport ),
pTaskManager( taskManager ),
pTickGenerator( 0 ),
pJobManager( jobManager )
{
Env *env = DefaultEnv::GetEnv();
Log *log = DefaultEnv::GetLog();
int timeoutResolution = DefaultTimeoutResolution;
env->GetInt( "TimeoutResolution", timeoutResolution );
pTransport->InitializeChannel( pChannelData, url.IsSecure() );
log->Debug( PostMasterMsg, "Creating new channel to: %s",
url.GetHostId().c_str() );
pUrl.SetParams( url.GetParams() );
pUrl.SetProtocol( url.GetProtocol() );
//--------------------------------------------------------------------------
// Create the stream
//--------------------------------------------------------------------------
pStream = new Stream( &pUrl );
pStream->SetTransport( transport );
pStream->SetPoller( poller );
pStream->SetIncomingQueue( &pIncoming );
pStream->SetTaskManager( taskManager );
pStream->SetJobManager( jobManager );
pStream->SetChannelData( &pChannelData );
pStream->Initialize();
//--------------------------------------------------------------------------
// Register the task generating timeout events
//--------------------------------------------------------------------------
pTickGenerator = new TickGeneratorTask( this, pUrl.GetHostId() );
pTaskManager->RegisterTask( pTickGenerator, ::time(0)+timeoutResolution );
}
//----------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------
Channel::~Channel()
{
pTickGenerator->Invalidate();
pTaskManager->UnregisterTask( pTickGenerator );
delete pStream;
pTransport->FinalizeChannel( pChannelData );
}
//----------------------------------------------------------------------------
// Send a message synchronously
//----------------------------------------------------------------------------
Status Channel::Send( Message *msg, bool stateful, time_t expires )
{
StatusHandler sh( msg );
Status sc = Send( msg, &sh, stateful, expires );
if( !sc.IsOK() )
return sc;
sc = sh.WaitForStatus();
return sc;
}
//----------------------------------------------------------------------------
// Send the message asynchronously
//----------------------------------------------------------------------------
Status Channel::Send( Message *msg,
OutgoingMsgHandler *handler,
bool stateful,
time_t expires )
{
return pStream->Send( msg, handler, stateful, expires );
}
//----------------------------------------------------------------------------
// Synchronously receive a message - blocks until a message matching
//----------------------------------------------------------------------------
Status Channel::Receive( Message *&msg,
MessageFilter *filter,
time_t expires )
{
FilterHandler fh( filter );
Status sc = Receive( &fh, expires );
if( !sc.IsOK() )
return sc;
sc = fh.WaitForStatus();
if( sc.IsOK() )
msg = fh.GetMessage();
return sc;
}
//----------------------------------------------------------------------------
// Listen to incoming messages
//----------------------------------------------------------------------------
Status Channel::Receive( IncomingMsgHandler *handler, time_t expires )
{
pIncoming.AddMessageHandler( handler, expires );
return Status();
}
//----------------------------------------------------------------------------
// Handle a time event
//----------------------------------------------------------------------------
void Channel::Tick( time_t now )
{
pStream->Tick( now );
}
//----------------------------------------------------------------------------
// Force disconnect of all streams
//----------------------------------------------------------------------------
Status Channel::ForceDisconnect()
{
//--------------------------------------------------------------------------
// Disconnect and recreate the streams
//--------------------------------------------------------------------------
pStream->ForceError( Status( stError, errOperationInterrupted ) );
return Status();
}
//------------------------------------------------------------------------
// Get the number of connected data streams
//------------------------------------------------------------------------
uint16_t Channel::NbConnectedStrm()
{
return XRootDTransport::NbConnectedStrm( pChannelData );
}
//------------------------------------------------------------------------
//! Set the on-connect handler for data streams
//------------------------------------------------------------------------
void Channel::SetOnConnectHandler( Job *onConnJob )
{
pStreams[0]->SetOnConnectHandler( onConnJob );
}
//----------------------------------------------------------------------------
// Query the transport handler
//----------------------------------------------------------------------------
Status Channel::QueryTransport( uint16_t query, AnyObject &result )
{
return pTransport->Query( query, result, pChannelData );
}
//----------------------------------------------------------------------------
// Register channel event handler
//----------------------------------------------------------------------------
void Channel::RegisterEventHandler( ChannelEventHandler *handler )
{
pStream->RegisterEventHandler( handler );
}
//------------------------------------------------------------------------
// Remove a channel event handler
//------------------------------------------------------------------------
void Channel::RemoveEventHandler( ChannelEventHandler *handler )
{
pStream->RemoveEventHandler( handler );
}
}