hpp-template-corba  4.9.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 #include <iostream>
17 
18 #include <hpp/corba/template/debug.hh>
19 
20 //FIXME: remove me.
21 #define HPP_CORBA_CATCH(msg, ret) \
22  catch(CORBA::UserException& exc) { \
23  hppCorbaDout (error, "CORBA::UserException: " << msg << " " \
24  << 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 {
46  namespace corba
47  {
48  using CORBA::Exception;
49  using CORBA::Object_var;
50  using CORBA::SystemException;
51  using CORBA::ORB_init;
52  using CORBA::PolicyList;
53  using CORBA::Object_ptr;
54  using CORBA::COMM_FAILURE;
55  using omniORB::fatalException;
56 
58 
59  template <class T>
60  Server<T>::Server(int argc, const char* argv[],
61  const char* orb_identifier,
62  const char* options[][2])
63  : ServerBase (argc, argv, orb_identifier, options), impl_ (NULL)
64  {}
65 
67  template <class T>
69  {
70  deactivateAndDestroyServers();
71  }
72 
73  template <class T>
75  {
76  return *impl_;
77  }
78 
79  template <class T>
80  bool Server<T>::initRootPOA(bool inMultiThread)
81  {
82  if (!ServerBase::initRootPOA(inMultiThread)) return false;
83 
84  if (impl_ != NULL) return true;
85  poa_ = main_poa();
86 
87  // create implementation
88  try {
89  impl_ = new T ();
90  setServant(impl_->_this());
91  }
92  HPP_CORBA_CATCH("failed to create the server implementation", false);
93 
94  // activate implementation
95  try {
96  servantId_ = poa_->activate_object(impl_);
97  }
98  HPP_CORBA_CATCH("failed to activate the server implementation", false);
99 
100  return true;
101  }
102 
103  template <class T>
104  bool Server<T>::initOmniINSPOA(const char* object_id)
105  {
106  if (!ServerBase::initOmniINSPOA()) return false;
107 
108  if (impl_ != NULL) return true;
109  poa_ = ins_poa_;
110 
111  // create implementation
112  try {
113  impl_ = new T ();
114  setServant(impl_->_this());
115  }
116  HPP_CORBA_CATCH("failed to create the server implementation", false);
117 
118  try {
119  servantId_ = PortableServer::string_to_ObjectId(object_id);
120  poa_->activate_object_with_id(servantId_, impl_);
121  }
122  HPP_CORBA_CATCH("failed to activate the server implementation", false);
123 
124  return true;
125  }
126 
127  template <class T>
129  {
130  if (impl_) {
131  try {
132  poa_->deactivate_object(servantId_);
133  } catch (const CORBA::OBJECT_NOT_EXIST& exc) {
134  // Servant was already deactivated and deleted.
135  }
136  }
137  }
138 
139  } // end of namespace corba.
140 } // end of namespace hpp.
141 
142 #endif //HPP_CORBA_TEMPLATE_SERVER_HXX
PortableServer::POA_var main_poa()
Definition: server.hh:89
Definition: server.hh:17
CORBA::ORB::InvalidName InvalidName
Definition: server.hxx:57
bool initRootPOA(bool inMultiThread)
PortableServer::POA_var ins_poa_
Definition: server.hh:107
Definition: server.hh:21
Server(int argc, const char *argv[], const char *orb_identifier="", const char *options[][2]=0)
Constructor.
Definition: server.hxx:60
T & implementation()
Return a reference to the implementation.
Definition: server.hxx:74
~Server()
Shutdown CORBA server.
Definition: server.hxx:68
#define HPP_CORBA_CATCH(msg, ret)
Definition: server.hxx:21
void setServant(CORBA::Object_ptr obj)
Template CORBA server.
Definition: server.hh:137
bool initRootPOA(bool inMultiThread)
Definition: server.hxx:80
PortableServer::POA_var poa_
Definition: server.hh:107