RTXI  2.4
The Real-Time eXperiment Interface Documentation
daq.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include <daq.h>
21 #include <algorithm>
22 
23 DAQ::Device::Device(std::string name,IO::channel_t *chan,size_t size) : IO::Block(name,chan,size)
24 {
25  DAQ::Manager::getInstance()->insertDevice(this);
26 }
27 
29 {
30  DAQ::Manager::getInstance()->removeDevice(this);
31 }
32 
33 DAQ::Driver::Driver(const std::string &n) : name(n)
34 {
35  DAQ::Manager::getInstance()->registerDriver(this,name);
36 }
37 
39 {
40  DAQ::Manager::getInstance()->unregisterDriver(name);
41 }
42 
43 void DAQ::Manager::foreachDevice(void (*callback)(DAQ::Device *,void *),void *param)
44 {
45  Mutex::Locker lock(&mutex);
46  for (std::list<Device *>::iterator i = devices.begin(); i != devices.end(); ++i)
47  {
48  callback(*i,param);
49  }
50 }
51 
52 DAQ::Device *DAQ::Manager::loadDevice(const std::string &name,const std::list<std::string> &args)
53 {
54  Mutex::Locker lock(&mutex);
55 
56  if (driverMap.find(name) == driverMap.end())
57  {
58  ERROR_MSG("DAQ::Manager::loadDevice : Driver %s does not exist\n",name.c_str());
59  return 0;
60  }
61 
62  DAQ::Device *device = driverMap[name]->createDevice(args);
63  return device;
64 }
65 
66 void DAQ::Manager::insertDevice(DAQ::Device *device)
67 {
68  if (!device)
69  {
70  ERROR_MSG("DAQ::Manager::insertDevice : Invalid device\n");
71  return;
72  }
73 
74  Mutex::Locker lock(&mutex);
75 
76  if (std::find(devices.begin(),devices.end(),device) != devices.end())
77  {
78  ERROR_MSG("DAQ::Device::insertDevice : Device already present\n");
79  return;
80  }
81 
82  devices.push_back(device);
83 }
84 
85 void DAQ::Manager::removeDevice(DAQ::Device *device)
86 {
87  if (!device)
88  {
89  ERROR_MSG("DAQ::Manager::removeDevice : Invalid device\n");
90  return;
91  }
92 
93  Mutex::Locker lock(&mutex);
94  devices.remove(device);
95 }
96 
97 void DAQ::Manager::registerDriver(Driver *driver,const std::string &name)
98 {
99  if (!driver)
100  {
101  ERROR_MSG("DAQ::Manager::registerDriver : Invalid driver\n");
102  return;
103  }
104  Mutex::Locker lock(&mutex);
105 
106  if (driverMap.find(name) != driverMap.end())
107  {
108  ERROR_MSG("DAQ::Manager::registerDriver : Driver already registered\n");
109  return;
110  }
111  driverMap[name] = driver;
112 }
113 
114 void DAQ::Manager::unregisterDriver(const std::string &name)
115 {
116  Mutex::Locker lock(&mutex);
117 
118  if (driverMap.find(name) == driverMap.end())
119  {
120  ERROR_MSG("DAQ::Manager::unregisterDriver : Driver not registered\n");
121  return;
122  }
123  driverMap.erase(name);
124 }
125 
126 static Mutex mutex;
127 DAQ::Manager *DAQ::Manager::instance = 0;
128 
130 {
131  if (instance)
132  return instance;
133 
134  /*************************************************************************
135  * Seems like alot of hoops to jump through, but static allocation isn't *
136  * thread-safe. So effort must be taken to ensure mutual exclusion. *
137  *************************************************************************/
138 
139  Mutex::Locker lock(&::mutex);
140  if (!instance)
141  {
142  static Manager manager;
143  instance = &manager;
144  }
145 
146  return instance;
147 }
ERROR_MSG
void ERROR_MSG(const std::string &errmsg,...)
Definition: debug.cpp:27
IO
Connection Oriented Classes.
Definition: io.h:34
DAQ::Manager::getInstance
static Manager * getInstance(void)
Definition: daq.cpp:129
daq.h
Mutex
Definition: mutex.h:28
DAQ::Manager::foreachDevice
void foreachDevice(void(*callback)(Device *, void *), void *param)
Definition: daq.cpp:43
IO::channel_t
Definition: io.h:56
RT::Device::Device
Device(void)
Definition: rt.cpp:119
DAQ::Manager
Definition: daq.h:73
DAQ::Manager::loadDevice
Device * loadDevice(const std::string &driver, const std::list< std::string > &params)
Definition: daq.cpp:52
Mutex::Locker
Definition: mutex.h:36
DAQ::Driver::Driver
Driver(const std::string &name)
Definition: daq.cpp:33
DAQ::Driver::~Driver
virtual ~Driver(void)
Definition: daq.cpp:38
DAQ::Device
Definition: daq.h:139
DAQ::Device::~Device
virtual ~Device(void)
Definition: daq.cpp:28