XRootD
XrdClRedirectorRegistry.cc
Go to the documentation of this file.
1 /*
2  * XrdClRedirectorRegister.cc
3  *
4  * Created on: May 23, 2016
5  * Author: simonm
6  */
7 
11 #include "XrdCl/XrdClDefaultEnv.hh"
12 #include "XrdCl/XrdClConstants.hh"
13 #include "XrdCl/XrdClLog.hh"
14 
15 #include <arpa/inet.h>
16 
17 namespace XrdCl
18 {
19 
20 void RedirectJob::Run( void* )
21 {
22  // this makes sure the handler takes ownership of the new message
23  if( pHandler->Examine( msg ) != MsgHandler::Action::Ignore )
24  pHandler->Process();
25  delete this;
26 }
27 
28 
30 {
31  static RedirectorRegistry redirector;
32  return redirector;
33 }
34 
36 {
37  RedirectorMap::iterator itr;
38  for( itr = pRegistry.begin(); itr != pRegistry.end(); ++itr )
39  delete itr->second.first;
40 }
41 
42 XRootDStatus RedirectorRegistry::RegisterImpl( const URL &u, ResponseHandler *handler )
43 {
44  URL url = ConvertLocalfile( u );
45 
46  // we can only create a virtual redirector if
47  // a path to a metadata file has been provided
48  if( url.GetPath().empty() ) return XRootDStatus( stError, errNotSupported );
49 
50  // regarding file protocol we only support localhost
51  if( url.GetProtocol() == "file" && url.GetHostName() != "localhost" )
53 
54  XrdSysMutexHelper scopedLock( pMutex );
55  // get the key and check if it is already in the registry
56  const std::string key = url.GetLocation();
57  RedirectorMap::iterator itr = pRegistry.find( key );
58  if( itr != pRegistry.end() )
59  {
60  // increment user counter
61  ++itr->second.second;
62  if( handler ) handler->HandleResponseWithHosts( new XRootDStatus(), 0, 0 );
63  return XRootDStatus( stOK, suAlreadyDone );
64  }
65  // If it is a Metalink create a MetalinkRedirector
66  if( url.IsMetalink() )
67  {
68  MetalinkRedirector *redirector = new MetalinkRedirector( key );
69  XRootDStatus st = redirector->Load( handler );
70  if( !st.IsOK() )
71  delete redirector;
72  else
73  pRegistry[key] = std::pair<VirtualRedirector*, size_t>( redirector, 1 );
74  return st;
75  }
76  else
77  // so far we only support Metalink metadata format
78  return XRootDStatus( stError, errNotSupported );
79 }
80 
81 URL RedirectorRegistry::ConvertLocalfile( const URL &url )
82 {
83  int localml = DefaultLocalMetalinkFile;
84  DefaultEnv::GetEnv()->GetInt( "LocalMetalinkFile", localml );
85 
86  if( localml && url.GetProtocol() == "root" && url.GetHostName() == "localfile" )
87  {
88  Log *log = DefaultEnv::GetLog();
89  log->Warning( PostMasterMsg,
90  "Please note that the 'root://localfile//path/filename.meta4' "
91  "semantic is now deprecated, use 'file://localhost/path/filename.meta4'"
92  "instead!" );
93 
94  URL copy( url );
95  copy.SetHostName( "localhost" );
96  copy.SetProtocol( "file" );
97  return copy;
98  }
99 
100  return url;
101 }
102 
104 {
105  return RegisterImpl( url, 0 );
106 }
107 
109 {
110  SyncResponseHandler handler;
111  Status st = RegisterImpl( url, &handler );
112  if( !st.IsOK() ) return st;
113  return MessageUtils::WaitForStatus( &handler );
114 }
115 
117 {
118  URL url = ConvertLocalfile( u );
119 
120  XrdSysMutexHelper scopedLock( pMutex );
121  // get the key and return the value if it is in the registry
122  // offset 24 is where the path has been stored
123  const std::string key = url.GetLocation();
124  RedirectorMap::const_iterator itr = pRegistry.find( key );
125  if( itr != pRegistry.end() )
126  return itr->second.first;
127  // otherwise return null
128  return 0;
129 }
130 
131 //----------------------------------------------------------------------------
132 // Release the virtual redirector associated with the given URL
133 //----------------------------------------------------------------------------
135 {
136  URL url = ConvertLocalfile( u );
137 
138  XrdSysMutexHelper scopedLock( pMutex );
139  // get the key and return the value if it is in the registry
140  // offset 24 is where the path has been stored
141  const std::string key = url.GetLocation();
142  RedirectorMap::iterator itr = pRegistry.find( key );
143  if( itr == pRegistry.end() ) return;
144  // decrement user counter
145  --itr->second.second;
146  // if nobody is using it delete the object
147  // and remove it from the registry
148  if( !itr->second.second )
149  {
150  delete itr->second.first;
151  pRegistry.erase( itr );
152  }
153 }
154 
155 } /* namespace XrdCl */
static Log * GetLog()
Get default log.
static Env * GetEnv()
Get default client environment.
bool GetInt(const std::string &key, int &value)
Definition: XrdClEnv.cc:89
static XRootDStatus WaitForStatus(SyncResponseHandler *handler)
Wait and return the status of the query.
virtual uint16_t Examine(std::shared_ptr< Message > &msg)=0
virtual void Run(void *arg)
Run the user handler.
Singleton access to URL to virtual redirector mapping.
static RedirectorRegistry & Instance()
Returns reference to the single instance.
void Release(const URL &url)
Release the virtual redirector associated with the given URL.
XRootDStatus RegisterAndWait(const URL &url)
Creates a new virtual redirector and registers it (sync).
VirtualRedirector * Get(const URL &url) const
Get a virtual redirector associated with the given URL.
XRootDStatus Register(const URL &url)
Creates a new virtual redirector and registers it (async).
Handle an async response.
virtual void HandleResponseWithHosts(XRootDStatus *status, AnyObject *response, HostList *hostList)
Synchronize the response.
URL representation.
Definition: XrdClURL.hh:31
bool IsMetalink() const
Is it a URL to a metalink.
Definition: XrdClURL.cc:458
const std::string & GetHostName() const
Get the name of the target host.
Definition: XrdClURL.hh:170
const std::string & GetProtocol() const
Get the protocol.
Definition: XrdClURL.hh:118
std::string GetLocation() const
Get location (protocol://host:port/path)
Definition: XrdClURL.cc:337
const std::string & GetPath() const
Get the path.
Definition: XrdClURL.hh:217
An interface for metadata redirectors.
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
const uint64_t PostMasterMsg
const uint16_t stOK
Everything went OK.
Definition: XrdClStatus.hh:31
const int DefaultLocalMetalinkFile
const uint16_t suAlreadyDone
Definition: XrdClStatus.hh:42
const uint16_t errNotSupported
Definition: XrdClStatus.hh:62
XrdSysError Log
Definition: XrdConfig.cc:112
Procedure execution status.
Definition: XrdClStatus.hh:115
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124