Device_impl.h
Go to the documentation of this file.
1 /*
2  * This file is protected by Copyright. Please refer to the COPYRIGHT file
3  * distributed with this source distribution.
4  *
5  * This file is part of REDHAWK core.
6  *
7  * REDHAWK core is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * REDHAWK core is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.
19  */
20 
21 
22 #ifndef DEVICE_IMPL_H
23 #define DEVICE_IMPL_H
24 
25 #include <signal.h>
26 #include <string>
27 #include <iostream>
28 #include <fstream>
29 #include <map>
30 
31 #include "Resource_impl.h"
32 #include "CF/cf.h"
33 #include "ossie/debug.h"
34 #include "ossie/Events.h"
35 #include "ossie/CorbaUtils.h"
36 #include "ossie/Autocomplete.h"
37 
38 class Device_impl;
39 
41 #ifdef BEGIN_AUTOCOMPLETE_IGNORE
42  public virtual POA_CF::Device,
43 #endif
44  public Resource_impl
45 {
46  ENABLE_LOGGING
47 
48 public:
49 
50  // Return the Log configuration file
51  static std::string getLogConfig(const char* devmgr_ior, const char* log_config, std::string& devmgr_label);
52  Device_impl (char*, char*, char*, char*);
53  Device_impl (char*, char*, char*, char*, char*);
54  Device_impl (char*, char*, char*, char*, CF::Properties& capacities);
55  Device_impl (char*, char*, char*, char*, CF::Properties& capacities, char*);
56  ~Device_impl ();
57 
58  template<class T>
59  static void start_device(T** devPtr, struct sigaction sa, int argc, char* argv[]) {
60  start_device(boost::bind(&Device_impl::make_device<T>,boost::ref(*devPtr),_1,_2,_3,_4,_5), sa, argc, argv);
61  }
62  virtual void halt ();
63  void releaseObject () throw (CF::LifeCycle::ReleaseError, CORBA::SystemException);
64  char* label () throw (CORBA::SystemException);
65  CF::Device::UsageType usageState ()throw (CORBA::SystemException);
66  CF::Device::AdminType adminState ()throw (CORBA::SystemException);
67  CF::Device::OperationalType operationalState ()throw (CORBA::SystemException);
68  CF::AggregateDevice_ptr compositeDevice ()throw (CORBA::SystemException);
69  void adminState (CF::Device::AdminType _adminType) throw (CORBA::SystemException);
70  void deallocateCapacity (const CF::Properties& capacities) throw (CF::Device::InvalidState, CF::Device::InvalidCapacity, CORBA::SystemException);
71  CORBA::Boolean allocateCapacity (const CF::Properties& capacities) throw (CF::Device::InvalidState, CF::Device::InvalidCapacity, CF::Device::InsufficientCapacity, CORBA::SystemException);
72  void configure (const CF::Properties& configProperties) throw (CF::PropertySet::PartialConfiguration, CF::PropertySet::InvalidConfiguration, CORBA::SystemException);
73  // resolve domain awareness
74  void setAdditionalParameters(std::string &softwareProfile, std::string &application_registrar_ior, const std::string &nic);
75  virtual void run ();
76  const CF::DeviceManager_ptr getDeviceManager() const ;
77 
78  // Returns true if the Device is in an unlocked state
79  bool isUnlocked ();
80  // Returns true if the Device is in a locked state
81  bool isLocked ();
82  // Returns true if the Device is a disabled state
83  bool isDisabled ();
84  // Returns true if the Device is a busy state
85  bool isBusy ();
86  // Returns true if the Device is an idle state
87  bool isIdle ();
88  // Set admin state (LOCKED, SHUTTING_DOWN, UNLOCKED)
89  void setAdminState (CF::Device::AdminType _adminType);
90 
91 protected:
92  // Admin state (LOCKED, SHUTTING_DOWN, UNLOCKED)
93  CF::Device::AdminType _adminState;
94  // Admin state (IDLE, ACTIVE, BUSY)
95  CF::Device::UsageType _usageState;
96  // Admin state (ENABLED, DISABLED)
97  CF::Device::OperationalType _operationalState;
98  // Pointer to this child's parent (CF::AggregateDevice::nil otherwise)
99  CF::AggregateDevice_ptr _aggregateDevice;
100  // Device label
101  std::string _label;
102  // String pointer to this child's parent (empty string otherwise
103  std::string _compositeDev_ior;
104 
113  };
114  CF::DeviceManager_ptr _deviceManager;
115  redhawk::events::PublisherPtr idm_publisher;
116  int sig_fd;
117  //
118  // call after device has been created and assigned exec params
119  //
120  virtual void postConstruction( std::string &softwareProfile,
121  std::string &registrar_ior,
122  const std::string &idm_channel_ior="",
123  const std::string &nic="",
124  const int sigfd=-1 );
125  // resolve domain context for this device, what domain and device manager am I associated with
126  void resolveDomainContext();
127 
128  //
129  // Support for publishing state changes
130  //
131  void sendStateChange( StandardEvent::StateChangeType &fromState,
132  StandardEvent::StateChangeType &toState,
133  StandardEvent::StateChangeCategoryType category );
134  void connectIDMChannel( const std::string &idm_ior="" );
136  CF::Properties originalCap;
137  void deallocate (CORBA::Any& deviceCapacity, const CORBA::Any& resourceRequest);
138  bool allocate (CORBA::Any& deviceCapacity, const CORBA::Any& resourceRequest);
140  Device_impl::AnyComparisonType compareAnys (CORBA::Any& first, CORBA::Any& second);
141  std::string _devMgr_ior;
142 
143  // Change the value of _usageState
144  void setUsageState (CF::Device::UsageType newUsageState);
145 
146  // Function that is called when the usage state for the Device should be re-evaluated
147  void updateUsageState ();
148 
149  template <typename T>
150  void setAllocationImpl (const std::string& id, bool (*alloc)(const T&), void (*dealloc)(const T&))
151  {
152  useNewAllocation = true;
153  try {
154  PropertyWrapper<T>* wrapper = getAllocationPropertyById<T>(id);
155  wrapper->setAllocator(alloc);
156  wrapper->setDeallocator(dealloc);
157  } catch (const std::exception& error) {
158  LOG_WARN(Device_impl, "Cannot set allocation implementation: " << error.what());
159  }
160  }
161 
162  template <class C, typename T>
163  void setAllocationImpl (const std::string& id, C* target, bool (C::*alloc)(const T&),
164  void (C::*dealloc)(const T&))
165  {
166  useNewAllocation = true;
167  try {
168  PropertyWrapper<T>* wrapper = getAllocationPropertyById<T>(id);
169  wrapper->setAllocator(target, alloc);
170  wrapper->setDeallocator(target, dealloc);
171  } catch (const std::exception& error) {
172  LOG_WARN(Device_impl, "Cannot set allocation implementation: " << error.what());
173  }
174  }
175 
176  template <typename Alloc, typename Dealloc>
177  void setAllocationImpl (const char* id, Alloc alloc, Dealloc dealloc)
178  {
179  setAllocationImpl(std::string(id), alloc, dealloc);
180  }
181 
182  template <typename Target, typename Alloc, typename Dealloc>
183  void setAllocationImpl (const char* id, Target target, Alloc alloc, Dealloc dealloc)
184  {
185  setAllocationImpl(std::string(id), target, alloc, dealloc);
186  }
187 
188  template <typename T>
189  PropertyWrapper<T>* getAllocationPropertyById (const std::string& id)
190  {
191  PropertyWrapper<T>* property = getPropertyWrapperById<T>(id);
192  if (!property->isAllocatable()) {
193  throw std::invalid_argument("Property '" + id + "' is not allocatable");
194  }
195  return property;
196  }
197 
198 
199  // Associate a function callback with an allocation request against a property
200  template <typename T, typename Alloc, typename Dealloc>
201  void setAllocationImpl (T& value, Alloc alloc, Dealloc dealloc)
202  {
203  useNewAllocation = true;
204  try {
205  PropertyWrapper<T>* wrapper = getPropertyWrapper(value);
206  wrapper->setAllocator(alloc);
207  wrapper->setDeallocator(dealloc);
208  } catch (const std::exception& error) {
209  LOG_WARN(Device_impl, "Cannot set allocation implementation: " << error.what());
210  }
211  }
212 
213  // Associate a function callback on a target object (usually this) with an allocation request against a property
214  template <typename T, typename Target, typename Alloc, typename Dealloc>
215  void setAllocationImpl (T& value, Target target, Alloc alloc, Dealloc dealloc)
216  {
217  useNewAllocation = true;
218  try {
219  PropertyWrapper<T>* wrapper = getPropertyWrapper(value);
220  wrapper->setAllocator(target, alloc);
221  wrapper->setDeallocator(target, dealloc);
222  } catch (const std::exception& error) {
223  LOG_WARN(Device_impl, "Cannot set allocation implementation: " << error.what());
224  }
225  }
226 
227  // Return a container with a pointer to the Device Manager hosting this Device
228  redhawk::DeviceManagerContainer* getDeviceManager() {
229  return this->_devMgr;
230  }
231 
232 private:
233  // Adapter template function for device constructors. This is the only part of
234  // device creation that requires type-specific knowledge.
235  template <class T>
236  static Device_impl* make_device(T*& device, char* devMgrIOR, char* identifier, char* label,
237  char* profile, char* compositeDeviceIOR)
238  {
239  if (compositeDeviceIOR) {
240  // The AggregateDevice version of the constructor implicitly activates the new device,
241  // which may be an unsafe behavior.
242  device = new T(devMgrIOR, identifier, label, profile, compositeDeviceIOR);
243  } else {
244  device = new T(devMgrIOR, identifier, label, profile);
245  PortableServer::ObjectId_var oid = ossie::corba::RootPOA()->activate_object(device);
246  }
247  return device;
248  }
249  // Generic implementation of start_device, taking a function pointer to
250  // a component constructor (via make_device).
251  typedef boost::function<Device_impl* (char*, char*, char*, char*, char*)> ctor_type;
252  static void start_device(ctor_type ctor, struct sigaction sa, int argc, char* argv[]);
253  void initResources(char*, char*, char*, char*);
254  // Check for valid allocation properties
255  void validateCapacities (const CF::Properties& capacities);
256  // New per-property callback-based capacity management
257  bool useNewAllocation;
258  bool allocateCapacityNew (const CF::Properties& capacities);
259  void deallocateCapacityNew (const CF::Properties& capacities);
260  // Legacy capacity management
261  bool allocateCapacityLegacy (const CF::Properties& capacities);
262  void deallocateCapacityLegacy (const CF::Properties& capacities);
263  // container to the Device Manager
264  redhawk::DeviceManagerContainer *_devMgr;
265 
266 
267  private:
268  Device_impl(); // Code that tries to use this constructor will not work
269  Device_impl(Device_impl&); // No copying
270 
271 };
272 
273 
274 #endif
275 
void deallocateCapacity(const CF::Properties &capacities)
CF::AggregateDevice_ptr compositeDevice()
void setAllocationImpl(const char *id, Target target, Alloc alloc, Dealloc dealloc)
Definition: Device_impl.h:183
CF::Device::UsageType usageState()
Definition: Device_impl.h:110
void setAdminState(CF::Device::AdminType _adminType)
bool allocate(CORBA::Any &deviceCapacity, const CORBA::Any &resourceRequest)
char * getLogConfig()
void releaseObject()
CF::Device::OperationalType operationalState()
AnyComparisonType
Definition: Device_impl.h:105
char * identifier()
virtual void halt()
void connectIDMChannel(const std::string &idm_ior="")
CF::AggregateDevice_ptr _aggregateDevice
Definition: Device_impl.h:99
CORBA::Boolean allocateCapacity(const CF::Properties &capacities)
void deallocate(CORBA::Any &deviceCapacity, const CORBA::Any &resourceRequest)
std::string _compositeDev_ior
Definition: Device_impl.h:103
bool isUnlocked()
virtual void postConstruction(std::string &softwareProfile, std::string &registrar_ior, const std::string &idm_channel_ior="", const std::string &nic="", const int sigfd=-1)
void setAdditionalParameters(std::string &softwareProfile, std::string &application_registrar_ior, const std::string &nic)
bool initialConfiguration
Definition: Device_impl.h:135
int sig_fd
Definition: Device_impl.h:116
CF::Device::UsageType _usageState
Definition: Device_impl.h:95
void updateUsageState()
char * label()
Definition: Device_impl.h:109
void setAllocationImpl(const char *id, Alloc alloc, Dealloc dealloc)
Definition: Device_impl.h:177
void setUsageState(CF::Device::UsageType newUsageState)
bool isLocked()
CF::Device::AdminType adminState()
std::string _label
Definition: Device_impl.h:101
PropertyWrapper< T > * getAllocationPropertyById(const std::string &id)
Definition: Device_impl.h:189
redhawk::DeviceManagerContainer * getDeviceManager()
Definition: Device_impl.h:228
Definition: Device_impl.h:111
char * softwareProfile()
void resolveDomainContext()
redhawk::events::PublisherPtr idm_publisher
Definition: Device_impl.h:115
Definition: Resource_impl.h:40
bool isIdle()
void setAllocationImpl(T &value, Alloc alloc, Dealloc dealloc)
Definition: Device_impl.h:201
Definition: Device_impl.h:40
virtual void run()
CF::Properties originalCap
Definition: Device_impl.h:136
Definition: Device_impl.h:107
CF::Device::AdminType _adminState
Definition: Device_impl.h:93
CF::Device::OperationalType _operationalState
Definition: Device_impl.h:97
static void start_device(T **devPtr, struct sigaction sa, int argc, char *argv[])
Definition: Device_impl.h:59
Definition: Device_impl.h:112
void sendStateChange(StandardEvent::StateChangeType &fromState, StandardEvent::StateChangeType &toState, StandardEvent::StateChangeCategoryType category)
void configure(const CF::Properties &configProperties)
void setAllocationImpl(T &value, Target target, Alloc alloc, Dealloc dealloc)
Definition: Device_impl.h:215
void setAllocationImpl(const std::string &id, bool(*alloc)(const T &), void(*dealloc)(const T &))
Definition: Device_impl.h:150
Device_impl::AnyComparisonType compareAnyToZero(CORBA::Any &first)
PropertyWrapper< T > * getPropertyWrapper(T &value)
Definition: PropertySet_impl.h:288
Device_impl::AnyComparisonType compareAnys(CORBA::Any &first, CORBA::Any &second)
void setAllocationImpl(const std::string &id, C *target, bool(C::*alloc)(const T &), void(C::*dealloc)(const T &))
Definition: Device_impl.h:163
Definition: Device_impl.h:106
bool isDisabled()
bool isBusy()
std::string _devMgr_ior
Definition: Device_impl.h:141
Definition: Device_impl.h:108
const CF::DeviceManager_ptr getDeviceManager() const
CF::DeviceManager_ptr _deviceManager
Definition: Device_impl.h:114