hpp-template-corba  4.14.0
Template corba server
server.hxx
Go to the documentation of this file.
1 // Copyright (C) 2009, 2010 by Florent Lamiraux, Thomas Moulard, JRL.
2 //
3 // This file is part of hpp-template-corba
4 //
5 // This software is provided "as is" without warranty of any kind,
6 // either expressed or implied, including but not limited to the
7 // implied warranties of fitness for a particular purpose.
8 //
9 // See the COPYING file for more information.
10 
11 #ifndef HPP_CORBA_TEMPLATE_SERVER_HXX
12 #define HPP_CORBA_TEMPLATE_SERVER_HXX
13 
14 #include <errno.h>
15 #include <pthread.h>
16 
18 #include <iostream>
19 
20 // FIXME: remove me.
21 #define HPP_CORBA_CATCH(msg, ret) \
22  catch (CORBA::UserException & exc) { \
23  hppCorbaDout(error, \
24  "CORBA::UserException: " << msg << " " << exc._name()); \
25  return ret; \
26  } \
27  catch (CORBA::SystemException&) { \
28  hppCorbaDout(error, "CORBA::SystemException: " << msg); \
29  return ret; \
30  } \
31  catch (CORBA::Exception&) { \
32  hppCorbaDout(error, "CORBA::Exception: " << msg); \
33  return ret; \
34  } \
35  catch (omniORB::fatalException & fe) { \
36  hppCorbaDout(error, "CORBA::fatalException: " << msg); \
37  return ret; \
38  } \
39  catch (...) { \
40  hppCorbaDout(error, "CORBA: unknown exception: " << msg); \
41  return ret; \
42  }
43 
44 namespace hpp {
45 namespace corba {
46 using CORBA::COMM_FAILURE;
47 using CORBA::Exception;
48 using CORBA::Object_ptr;
49 using CORBA::Object_var;
50 using CORBA::ORB_init;
51 using CORBA::PolicyList;
52 using CORBA::SystemException;
53 using omniORB::fatalException;
54 
56 
57 template <class T>
58 Server<T>::Server(int argc, const char* argv[], const char* orb_identifier,
59  const char* options[][2])
60  : ServerBase(argc, argv, orb_identifier, options), impl_(NULL) {}
61 
63 template <class T>
65  deactivateAndDestroyServers();
66 }
67 
68 template <class T>
70  return *impl_;
71 }
72 
73 template <class T>
74 bool Server<T>::initRootPOA(bool inMultiThread) {
75  if (!ServerBase::initRootPOA(inMultiThread)) return false;
76 
77  if (impl_ != NULL) return true;
78  poa_ = main_poa();
79 
80  // create implementation
81  try {
82  impl_ = new T();
83  setServant(impl_->_this());
84  }
85  HPP_CORBA_CATCH("failed to create the server implementation", false);
86 
87  // activate implementation
88  try {
89  servantId_ = poa_->activate_object(impl_);
90  }
91  HPP_CORBA_CATCH("failed to activate the server implementation", false);
92 
93  return true;
94 }
95 
96 template <class T>
97 bool Server<T>::initOmniINSPOA(const char* object_id) {
98  if (!ServerBase::initOmniINSPOA()) return false;
99 
100  if (impl_ != NULL) return true;
101  poa_ = ins_poa_;
102 
103  // create implementation
104  try {
105  impl_ = new T();
106  setServant(impl_->_this());
107  }
108  HPP_CORBA_CATCH("failed to create the server implementation", false);
109 
110  try {
111  servantId_ = PortableServer::string_to_ObjectId(object_id);
112  poa_->activate_object_with_id(servantId_, impl_);
113  }
114  HPP_CORBA_CATCH("failed to activate the server implementation", false);
115 
116  return true;
117 }
118 
119 template <class T>
121  if (impl_) {
122  try {
123  poa_->deactivate_object(servantId_);
124  } catch (const CORBA::OBJECT_NOT_EXIST& exc) {
125  // Servant was already deactivated and deleted.
126  }
127  }
128 }
129 
130 } // end of namespace corba.
131 } // end of namespace hpp.
132 
133 #endif // HPP_CORBA_TEMPLATE_SERVER_HXX
PortableServer::POA_var main_poa()
Definition: server.hh:86
Definition: server.hh:18
CORBA::ORB::InvalidName InvalidName
Definition: server.hxx:55
bool initRootPOA(bool inMultiThread)
PortableServer::POA_var ins_poa_
Definition: server.hh:95
Definition: server.hh:20
Server(int argc, const char *argv[], const char *orb_identifier="", const char *options[][2]=0)
Constructor.
Definition: server.hxx:58
T & implementation()
Return a reference to the implementation.
Definition: server.hxx:69
~Server()
Shutdown CORBA server.
Definition: server.hxx:64
#define HPP_CORBA_CATCH(msg, ret)
Definition: server.hxx:21
void setServant(CORBA::Object_ptr obj)
Template CORBA server.
Definition: server.hh:124
bool initRootPOA(bool inMultiThread)
Definition: server.hxx:74
PortableServer::POA_var poa_
Definition: server.hh:95