OmniEvents
Servant.cc
Go to the documentation of this file.
1 // Package : omniEvents
2 // Servant.cc Created : 2003/12/04
3 // Author : Alex Tingle
4 //
5 // Copyright (C) 2003-2005 Alex Tingle.
6 //
7 // This file is part of the omniEvents application.
8 //
9 // omniEvents is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // omniEvents is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 
24 #include "Servant.h"
25 #include "Orb.h"
26 
27 #ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h> // getpid
29 #endif
30 
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h> // getpid
33 #elif defined(HAVE_PROCESS_H)
34 # include <process.h>
35 #endif
36 
37 #include <stdio.h> // sprintf
38 #include <cstdlib>
39 #include <assert.h>
40 
41 #ifdef HAVE_IOSTREAM
42 # include <iostream>
43 #else
44 # include <iostream.h>
45 #endif
46 
47 #ifdef HAVE_STD_IOSTREAM
48 using namespace std;
49 #endif
50 
51 namespace OmniEvents {
52 
53 
54 CORBA::Object_ptr createReference(
55  PortableServer::POA_ptr poa, // POA to own new object
56  const char* repositoryId // e.g. _tc_ProxyPushSupplier->id()
57 )
58 {
59  CORBA::String_var oidStr =newUniqueId();
60 
61  PortableServer::ObjectId_var oid =
62  PortableServer::string_to_ObjectId(oidStr.in());
63 
64  CORBA::Object_var obj =
65  poa->create_reference_with_id(oid.in(),repositoryId);
66 
67  assert(!CORBA::is_nil(obj));
68  return obj._retn();
69 }
70 
71 char* newUniqueId()
72 {
73  static long count=0;
74  static omni_mutex mutex;
75  int mypid =getpid(); // MS VC++6 doesn't have type pid_t!
76  unsigned long sec,nsec;
77  omni_thread::get_time(&sec,&nsec); // More portable than just time().
78  char buf[128];
79  {
80  omni_mutex_lock l(mutex);
81  sprintf(buf,"%lx.%d.%lx",++count,mypid,sec);
82  }
83  return CORBA::string_dup(buf);
84 }
85 
86 
87 //
88 // Servant
89 //
90 
91 
92 #if OMNIEVENTS__DEBUG_SERVANT
93 # define OMNIEVENTS__ADDR "["<<long(this)<<"] "
94 int Servant::_objectCount =0;
95 #else
96 # define OMNIEVENTS__ADDR
97 #endif
98 
99 
100 Servant::Servant(PortableServer::POA_ptr poa)
101 : _poa(PortableServer::POA::_duplicate(poa))
102 {
103 #if OMNIEVENTS__DEBUG_SERVANT
104  ++_objectCount;
105  DB(21,OMNIEVENTS__ADDR "Servant::Servant() count="<<_objectCount)
106 #endif
107 }
108 
109 
111 {
112 #if OMNIEVENTS__DEBUG_SERVANT
113  --_objectCount;
114  DB(20,OMNIEVENTS__ADDR "Servant::~Servant() count="<<_objectCount)
115 #endif
116 }
117 
118 
119 PortableServer::POA_ptr Servant::_default_POA()
120 {
121  return PortableServer::POA::_duplicate(_poa.in());
122 }
123 
124 
125 void Servant::activateObjectWithId(const char* oidStr)
126 {
127  using namespace PortableServer;
128  CORBA::String_var poaName =_poa->the_name();
129  DB(5,OMNIEVENTS__ADDR "Activating object "<<poaName.in()<<"/"<<oidStr);
130  try
131  {
132  ObjectId_var oid =string_to_ObjectId(oidStr);
133  _poa->activate_object_with_id(oid.in(),this);
134  }
135  catch(CORBA::BAD_PARAM& ex)
136  {
137  DB(0,"Can't activate "<<oidStr<<": "
138  "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
139  throw;
140  }
141  catch(POA::ServantAlreadyActive& ex)
142  {
143  DB(0,"Can't activate "<<oidStr<<": Servant is already active.")
144  throw;
145  }
146  catch(POA::ObjectAlreadyActive& ex)
147  {
148  DB(0,"Can't activate "<<oidStr<<": Object is already active.")
149  throw;
150  }
151  catch(POA::WrongPolicy& ex)
152  {
153  DB(0,"Can't activate "<<oidStr<<": POA '"<<poaName.in()
154  <<"' has wrong policy for activate_object_with_id().")
155  exit(1); // Programming error - so quit.
156  }
157 }
158 
159 
161 {
162  using namespace PortableServer;
163  CORBA::String_var poaName =_poa->the_name();
164 
165  ObjectId_var oid;
166  try
167  {
168  oid=_poa->servant_to_id(this);
169  }
170  catch(POA::ServantNotActive& ex)
171  {
172  DB(0,"Can't deactivate servant: POA '"<<poaName.in()
173  <<"' says it is not active.")
174  return;
175  }
176  catch(POA::WrongPolicy& ex)
177  {
178  DB(0,"Can't deactivate servant: POA '"<<poaName.in()
179  <<"' has wrong policy for servant_to_id().")
180  exit(1); // Programming error - so quit.
181  }
182 
183  CORBA::String_var oidStr;
184  try
185  {
186  oidStr=ObjectId_to_string(oid.in());
187  }
188  catch(CORBA::BAD_PARAM& ex)
189  {
190  DB(0,"Can't deactivate servant. ObjectId looks bad: "
191  "BAD_PARAM" IF_OMNIORB4(" ("<<NP_MINORSTRING(ex)<<")") )
192  return;
193  }
194 
195  try
196  {
197  DB(7,OMNIEVENTS__ADDR "Deactivating object "<<poaName<<"/"<<oidStr.in());
198  _poa->deactivate_object(oid.in());
199  }
200  catch(POA::ObjectNotActive& ex)
201  {
202  DB(0,"Can't deactivate "<<oidStr<<": Object is not active.")
203  return;
204  }
205  catch(POA::WrongPolicy& ex)
206  {
207  DB(0,"Can't deactivate "<<oidStr<<": POA '"<<poaName.in()
208  <<"' has wrong policy for deactivate_object().")
209  exit(1); // Programming error - so quit.
210  }
211 }
212 
213 }; // end namespace OmniEvents
#define DB(l, x)
Definition: Orb.h:49
#define IF_OMNIORB4(omniORB4_code)
Definition: Orb.h:46
#define NP_MINORSTRING(systemException)
Definition: Orb.h:52
#define OMNIEVENTS__ADDR
Definition: Servant.cc:96
static omni_mutex mutex
Definition: pushcons.cc:168
CORBA::Object_ptr createReference(PortableServer::POA_ptr poa, const char *repositoryId)
Helper method called by createNarrowedReference().
Definition: Servant.cc:54
char * newUniqueId()
Generates a unique object ID string, based upon the current PID and time.
Definition: Servant.cc:71
virtual ~Servant()
Definition: Servant.cc:110
virtual PortableServer::POA_ptr _default_POA()
Definition: Servant.cc:119
PortableServer::POA_var _poa
Definition: Servant.h:131
void activateObjectWithId(const char *oidStr)
Calls activate_object_with_id() to activate this servant in its POA.
Definition: Servant.cc:125
void deactivateObject()
Calls deactivate_object() to deactivate this servant in its POA.
Definition: Servant.cc:160