Service_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 SERVICE_IMPL_H
23 #define SERVICE_IMPL_H
24 
25 #include <stdlib.h>
26 #include <string>
27 #include <iostream>
28 #include <fstream>
29 #include <map>
30 
31 #include "CF/cf.h"
32 #include "ossie/ossieSupport.h"
33 #include "ossie/debug.h"
34 #include "ossie/CorbaUtils.h"
35 #include "ossie/logging/loghelpers.h"
36 #include "ossie/Containers.h"
37 #include "ossie/Autocomplete.h"
38 #include <signal.h>
39 
41 {
42  ENABLE_LOGGING
43 
44 public:
45  template<class T>
46  static void start_service(T** servPtr, struct sigaction sa, int argc, char* argv[]) {
47  char* devMgr_ior = 0;
48  char* name = 0;
49  const char* logging_config_uri = 0;
50  int debug_level = -1; // use log config uri as log level context
51  std::string logcfg_uri("");
52  std::string dpath("");
53  std::string sname("");
54 
55  std::map<std::string, char*> execparams;
56 
57  for (int i = 0; i < argc; i++) {
58 
59  if (strcmp("DEVICE_MGR_IOR", argv[i]) == 0) {
60  devMgr_ior = argv[++i];
61  } else if (strcmp("SERVICE_NAME", argv[i]) == 0) {
62  name = argv[++i];
63  sname=name;
64  } else if (strcmp("LOGGING_CONFIG_URI", argv[i]) == 0) {
65  logging_config_uri = argv[++i];
66  } else if (strcmp("DEBUG_LEVEL", argv[i]) == 0) {
67  debug_level = atoi(argv[++i]);
68  } else if (strcmp("DOM_PATH", argv[i]) == 0) {
69  dpath = argv[++i];
70  } else if (i > 0) { // any other argument besides the first one is part of the execparams
71  std::string paramName = argv[i];
72  execparams[paramName] = argv[++i];
73  }
74  }
75 
76 
77  // The ORB must be initialized before configuring logging, which may use
78  // CORBA to get its configuration file. Devices do not need persistent IORs.
79  ossie::corba::CorbaInit(argc, argv);
80 
81  // check if logging config URL was specified...
82  if ( logging_config_uri ) logcfg_uri=logging_config_uri;
83 
84  // setup logging context for a servie
85  ossie::logging::ResourceCtxPtr ctx( new ossie::logging::ServiceCtx( sname, dpath ) );
86 
87  // configure the logging library
88  ossie::logging::Configure(logcfg_uri, debug_level, ctx);
89 
90  if ((devMgr_ior == 0) || (name == 0)) {
91  LOG_FATAL(Service_impl, "Per SCA specification, DEVICE_MGR_IOR and SERVICE_NAME must be provided");
92  exit(-1);
93  }
94 
95  LOG_DEBUG(Service_impl, "Name = " << name << " IOR = " << devMgr_ior)
96 
97  // Associate SIGINT to signal_catcher interrupt handler
98  if( sigaction( SIGINT, &sa, NULL ) == -1 ) {
99  LOG_FATAL(Service_impl, "SIGINT association failed");
100  exit(EXIT_FAILURE);
101  }
102 
103  // Associate SIGQUIT to signal_catcher interrupt handler
104  if( sigaction( SIGQUIT, &sa, NULL ) == -1 ) {
105  LOG_FATAL(Service_impl, "SIGQUIT association failed");
106  exit(EXIT_FAILURE);
107  }
108 
109  // Associate SIGTERM to signal_catcher interrupt handler
110  if( sigaction( SIGTERM, &sa, NULL ) == -1 ) {
111  LOG_FATAL(Service_impl, "SIGTERM association failed");
112  exit(EXIT_FAILURE);
113  }
114 
115  /* Ignore SIGInterrupt because when you CTRL-C the node
116  booter we don't want the device to die, and it's the shells responsibility
117  to send CTRL-C to all foreground processes (even children) */
118  signal(SIGINT, SIG_IGN);
119 
120  *servPtr = new T(devMgr_ior, name);
121  PortableServer::ObjectId_var oid = ossie::corba::RootPOA()->activate_object(*servPtr);
122  (*servPtr)->resolveDeviceManager();
123  (*servPtr)->registerServiceWithDevMgr();
124  (*servPtr)->run();
125  (*servPtr)->terminateService();
126  (*servPtr)->_remove_ref();
127 
128  ossie::corba::OrbShutdown(true);
129  }
130 
131  // Get the file path for the logging configuration file
132  static std::string getLogConfig(const char* devmgr_ior, const char* log_config, std::string& devmgr_label) {
133  // connect to the device manager and copy the log config file to the local directory
134 
135  std::string _local_logconfig_path;
136 
137  // connect to device manager
138  CF::DeviceManager_ptr _devMgr_ptr = CF::DeviceManager::_nil();
139  CORBA::Object_var _devMgr_obj = ossie::corba::Orb()->string_to_object(devmgr_ior);
140  if (CORBA::is_nil(_devMgr_obj)) {
141  std::cout << "ERROR:Service_impl:getLogConfig - Invalid device manager IOR: " << devmgr_ior << std::endl;
142  return _local_logconfig_path;
143  }
144 
145  _devMgr_ptr = CF::DeviceManager::_narrow(_devMgr_obj);
146  if (CORBA::is_nil(_devMgr_ptr)) {
147  std::cout << "ERROR:Service_impl:getLogConfig - Could not narrow device manager IOR: " << devmgr_ior << std::endl;
148  return _local_logconfig_path;
149  }
150 
151  // store the dev manager's label
152  devmgr_label = _devMgr_ptr->label();
153 
154  // copy the file to memory
155  CF::File_var logFile;
156  CF::OctetSequence_var logFileData;
157  try {
158  logFile = _devMgr_ptr->fileSys()->open(log_config, true);
159  unsigned int logFileSize = logFile->sizeOf();
160  logFile->read(logFileData, logFileSize);
161  } catch ( ... ) {
162  std::cout << "ERROR:Service_impl:getLogConfig - Could not copy file to local memory. File name: " << log_config << std::endl;
163  return _local_logconfig_path;
164  }
165 
166  // get the log config file name from the path
167  std::string tmp_log_config = log_config;
168  std::string::size_type slash_loc = tmp_log_config.find_last_of("/");
169  if (slash_loc != std::string::npos) {
170  _local_logconfig_path = tmp_log_config.substr(slash_loc + 1);
171  }
172 
173  // write the file to local directory
174  std::fstream _local_logconfig;
175  std::ios_base::openmode _local_logconfig_mode = std::ios::in | std::ios::out | std::ios::trunc;
176  try {
177  _local_logconfig.open(_local_logconfig_path.c_str(), _local_logconfig_mode);
178  if (!_local_logconfig.is_open()) {
179  std::cout << "ERROR:Service_impl:getLogConfig - Could not open log file on local system. File name: " << _local_logconfig_path << std::endl;
180  throw;
181  }
182 
183  _local_logconfig.write((const char*)logFileData->get_buffer(), logFileData->length());
184  if (_local_logconfig.fail()) {
185  std::cout << "ERROR:Service_impl:getLogConfig - Could not write log file on local system. File name: " << _local_logconfig_path << std::endl;
186  throw;
187  }
188  _local_logconfig.close();
189  } catch ( ... ) {
190  std::cout << "ERROR:Service_impl:getLogConfig - Could not copy file to local system. File name: " << _local_logconfig_path << std::endl;
191  _local_logconfig_path.clear(); // so calling function knows not to use value
192  return _local_logconfig_path;
193  }
194 
195  return _local_logconfig_path;
196  }
197 
198 protected:
199  // Enumerated type for comparison
208  };
209  CF::DeviceManager_ptr _deviceManager;
211  CF::Properties originalCap;
212  // Service instance name
213  std::string _name;
214 
215  // Determine whether the contents of the argument are 0
217  // Determine whether the contents of different arguments
218  Service_impl::AnyComparisonType compareAnys (CORBA::Any& first, CORBA::Any& second);
219 
220 public:
221  Service_impl (char*, char*);
222  Service_impl(); // Code that tries to use this constructor will not work
223  Service_impl(Service_impl&); // No copying
224  ~Service_impl ();
225  virtual void resolveDeviceManager ();
226  virtual void registerServiceWithDevMgr ();
227  virtual void run ();
228  virtual void halt ();
229  // Function that is called as the Service terminates
230  virtual void terminateService ();
231 
232  // Return the container with a reference to the Device Manager hosting the Service
233  redhawk::DeviceManagerContainer* getDeviceManager() {
234  return this->_devMgr;
235  }
236  // Return the container with a reference to the Domain Manager that hosts the Service
237  redhawk::DomainManagerContainer* getDomainManager() {
238  return this->_domMgr;
239  }
240 
241 protected:
242  std::string _devMgr_ior;
244  omni_condition component_running;
245 
246 private:
247  void initResources(char*, char*);
248  redhawk::DeviceManagerContainer *_devMgr;
249  redhawk::DomainManagerContainer *_domMgr;
250 };
251 
252 
253 #endif
254 
redhawk::DomainManagerContainer * getDomainManager()
Definition: Service_impl.h:237
bool initialConfiguration
Definition: Service_impl.h:210
Service_impl::AnyComparisonType compareAnyToZero(CORBA::Any &first)
virtual void resolveDeviceManager()
AnyComparisonType
Definition: Service_impl.h:200
Service_impl::AnyComparisonType compareAnys(CORBA::Any &first, CORBA::Any &second)
omni_mutex component_running_mutex
Definition: Service_impl.h:243
CF::Properties originalCap
Definition: Service_impl.h:211
omni_condition component_running
Definition: Service_impl.h:244
CF::DeviceManager_ptr _deviceManager
Definition: Service_impl.h:209
Definition: Service_impl.h:204
virtual void registerServiceWithDevMgr()
virtual void halt()
Definition: Service_impl.h:40
Definition: Service_impl.h:201
virtual void run()
redhawk::DeviceManagerContainer * getDeviceManager()
Definition: Service_impl.h:233
std::string _name
Definition: Service_impl.h:213
static void start_service(T **servPtr, struct sigaction sa, int argc, char *argv[])
Definition: Service_impl.h:46
Definition: Service_impl.h:206
Definition: Service_impl.h:202
Definition: Service_impl.h:205
static std::string getLogConfig(const char *devmgr_ior, const char *log_config, std::string &devmgr_label)
Definition: Service_impl.h:132
std::string _devMgr_ior
Definition: Service_impl.h:242
Definition: Service_impl.h:207
Definition: Service_impl.h:203
virtual void terminateService()