μHAL (v2.6.5)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ClientFactory.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
33 #include "uhal/ClientFactory.hpp"
34 
35 
36 #include <boost/spirit/include/qi.hpp>
37 
39 #include "uhal/ProtocolUDP.hpp"
40 #include "uhal/ProtocolTCP.hpp"
41 #include "uhal/ProtocolIPbus.hpp"
43 #include "uhal/ProtocolMmap.hpp"
44 #include "uhal/ProtocolPCIe.hpp"
45 
46 
47 
48 namespace uhal
49 {
50 
52  {
53  if ( mInstance == NULL )
54  {
55  mInstance = new ClientFactory();
56  // ---------------------------------------------------------------------
57  mInstance->add< UDP< IPbus< 1 , 3 > > > ( "ipbusudp-1.3" , "Direct access to hardware via UDP, using IPbus version 1.3" );
58  mInstance->add< UDP< IPbus< 2 , 0 > > > ( "ipbusudp-2.0" , "Direct access to hardware via UDP, using IPbus version 2.0" );
59  // ---------------------------------------------------------------------
60  mInstance->add< TCP< IPbus< 1 , 3 > , 1 > > ( "ipbustcp-1.3" , "Direct access to hardware via TCP, using IPbus version 1.3" );
61  mInstance->add< TCP< IPbus< 2 , 0 > , 1 > > ( "ipbustcp-2.0" , "Direct access to hardware via TCP, using IPbus version 2.0" );
62  // ---------------------------------------------------------------------
63  mInstance->add< TCP< ControlHub < IPbus< 1 , 3 > > , 3 > > ( "chtcp-1.3", "Hardware access via the Control Hub, using IPbus version 1.3" );
64  mInstance->add< TCP< ControlHub < IPbus< 2 , 0 > > , 3 > > ( "chtcp-2.0", "Hardware access via the Control Hub, using IPbus version 2.0" );
65  // ---------------------------------------------------------------------
66  mInstance->add< PCIe > ( "ipbuspcie-2.0" , "Direct access to hardware via PCIe, using IPbus version 2.0" );
67  mInstance->add< Mmap > ( "ipbusmmap-2.0" , "Direct access to hardware via mmap, using IPbus version 2.0" );
68  // ---------------------------------------------------------------------
69 
70  }
71 
72  return *mInstance;
73  }
74 
75 
77 
78 
80  {
81  }
82 
83 
85  {
86  }
87 
88 
89  boost::shared_ptr<ClientInterface> ClientFactory::getClient ( const std::string& aId , const std::string& aUri )
90  {
91  URI lUri;
92 
93  try
94  {
95  grammars::URIGrammar lGrammar;
96  std::string::const_iterator lBegin ( aUri.begin() );
97  std::string::const_iterator lEnd ( aUri.end() );
98  boost::spirit::qi::phrase_parse ( lBegin , lEnd , lGrammar , boost::spirit::ascii::space , lUri );
99  }
100  catch ( const std::exception& aExc )
101  {
102  exception::FailedToParseURI lExc;
103  log ( lExc , "Failed to parse device URI " , Quote ( aUri ) );
104  throw lExc;
105  }
106 
107  log ( Info() , "URI " , Quote ( aUri ) , " parsed as:\n" , lUri );
108  boost::unordered_map< std::string , boost::shared_ptr<CreatorInterface> >::const_iterator lIt = mCreators.find ( lUri.mProtocol );
109 
110  if ( lIt == mCreators.end() )
111  {
112  std::stringstream lStr;
113 
114  for ( std::map< std::string , std::string >::const_iterator lIt = mProductDescriptions.begin() ; lIt != mProductDescriptions.end() ; ++lIt )
115  {
116  lStr << "\n > " << lIt->first << "\t: " << lIt->second;
117  }
118 
119  exception::ProtocolDoesNotExist lExc;
120  log ( lExc , "Protocol " , Quote ( lUri.mProtocol ) , " does not exists in map of creators. Options are:" , lStr.str() );
121  throw lExc;
122  }
123 
124  return lIt->second->create ( aId , lUri );
125  }
126 
127 
128 }
129 
Transport protocol to transfer an IPbus buffer via UDP.
Definition: ProtocolUDP.hpp:84
static ClientFactory & getInstance()
Static method to retrieve the single instance of the class.
Transport protocol to transfer an IPbus buffer via device file, using mmap.
std::map< std::string, std::string > mProductDescriptions
Store the description of the factory product.
boost::shared_ptr< ClientInterface > getClient(const std::string &aId, const std::string &aUri)
Construct an IPbus client based on the protocol identifier specified.
void add(const std::string &aProtocol, const std::string &aDescription="")
Method to create an associate between a protocol identifier and a Creator of a particular type...
_Quote< T > Quote(const T &aT)
virtual ~ClientFactory()
Destructor.
A class to construct an IPbus client based on the protocol identifier specified NOTE! This is a facto...
ClientFactory()
Default constructor This is private since only a single instance is to be created, using the getInstance method.
std::string mProtocol
The "protocol" part of a URI of the form "protocol://host:port/patha/pathb/blah.ext?key1=val1&key2=val2&key3=val3".
Definition: URI.hpp:52
Transport protocol to transfer an IPbus buffer via TCP.
Definition: ProtocolTCP.hpp:85
Transport protocol to transfer an IPbus buffer via PCIe.
InfoLevel Info
Definition: LogLevels.cpp:114
boost::unordered_map< std::string, boost::shared_ptr< CreatorInterface > > mCreators
Hash map associating a creator for a particular protocol with a file name.
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:49
static ClientFactory * mInstance
The single instance of the class.