OmniEvents
ProxyManager.cc
Go to the documentation of this file.
1 // Package : omniEvents
2 // ProxyManager.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 "ProxyManager.h"
25 #include "PersistNode.h"
26 #include "Orb.h"
27 #include "omniEventsLog.h"
28 
29 #include <string>
30 #include <map>
31 #include <assert.h>
32 #include <memory>
33 
34 namespace OmniEvents {
35 
36 //
37 // ProxyManager
38 //
39 
40 void
42  const PortableServer::ObjectId& oid,
43  PortableServer::POA_ptr adapter,
44  PortableServer::Servant serv,
45  CORBA::Boolean cleanup_in_progress,
46  CORBA::Boolean remaining_activations
47 )
48 {
49  auto_ptr<Proxy> narrowed( dynamic_cast<Proxy*>(serv) );
50  assert(narrowed.get()!=NULL);
51  set<Proxy*>::iterator pos =_servants.find(narrowed.get());
52  if(pos!=_servants.end())
53  _servants.erase(pos);
54  else
55  DB(1,"\t\teh? - POA attempted to etherealize unknown servant.");
56  // memory freed when narrowed goes out of scope.
57 }
58 
59 
61 {
62  // Reincarnate all connections from node's children.
63  for(map<string,PersistNode*>::const_iterator i=node._child.begin();
64  i!=node._child.end();
65  ++i)
66  {
67  assert(i->second!=NULL);
68  PortableServer::Servant serv =
69  this->incarnate(PortableServer::ObjectId(),_managedPoa);
70  Proxy* proxy =dynamic_cast<Proxy*>(serv);
71  assert(proxy!=NULL);
72  try
73  {
74  proxy->reincarnate(i->first,*(i->second));
75  }
76  catch(CORBA::BAD_PARAM& ex)
77  {
78  // This will happen when IOR fails to narrow.
79  DB(5,"Failed to reincarnate proxy: "<<i->first.c_str());
80  _servants.erase(proxy);
81  delete proxy;
82  }
83  }
84 }
85 
86 
87 void ProxyManager::output(ostream& os)
88 {
89  for(set<Proxy*>::iterator i =_servants.begin(); i!=_servants.end(); ++i)
90  {
91  (*i)->output(os);
92  }
93 }
94 
95 
96 ProxyManager::ProxyManager(PortableServer::POA_ptr p)
97 : Servant(p),
98  _servants(),
99  _managedPoa(PortableServer::POA::_nil())
100 {}
101 
102 
103 void ProxyManager::activate(const char* name)
104 {
105  using namespace PortableServer;
106 
107  // POLICIES:
108  // Lifespan =PERSISTENT // we can persist
109  // Assignment =USER_ID // write our own oid
110  // Uniqueness =[default] UNIQUE_ID // one servant per object
111  // ImplicitActivation=NO_IMPLICIT_ACTIVATION // disable auto activation
112  // RequestProcessing =USE_SERVANT_MANAGER
113  // ServantRetention =[default] RETAIN
114  // Thread =SINGLE_THREAD_MODEL // keep it simple
115 
116  CORBA::PolicyList policies;
117  policies.length(5);
118  policies[0]=_poa->create_lifespan_policy(PERSISTENT);
119  policies[1]=_poa->create_id_assignment_policy(USER_ID);
120  policies[2]=_poa->create_implicit_activation_policy(NO_IMPLICIT_ACTIVATION);
121  policies[3]=_poa->create_request_processing_policy(USE_SERVANT_MANAGER);
122  policies[4]=_poa->create_thread_policy(SINGLE_THREAD_MODEL);
123 
124  try
125  {
126  // Create a POA for this proxy type in this channel.
127  CORBA::String_var parentName =_poa->the_name();
128  string poaName =string(parentName.in())+"."+name;
129  POAManager_var parentManager =_poa->the_POAManager();
130  _managedPoa=_poa->create_POA(poaName.c_str(),parentManager.in(),policies);
131  }
132  catch(POA::AdapterAlreadyExists& ex) // create_POA
133  {
134  DB(0,"ProxyManager::ProxyManager() - POA::AdapterAlreadyExists")
135  }
136  catch(POA::InvalidPolicy& ex) // create_POA
137  {
138  DB(0,"ProxyManager::ProxyManager() - POA::InvalidPolicy: "<<ex.index)
139  }
140 
141  // Destroy the policy objects (Not strictly necessary in omniORB)
142  for(CORBA::ULong i=0; i<policies.length(); ++i)
143  policies[i]->destroy();
144 
145  string oidStr =string(name)+"Manager";
146  activateObjectWithId(oidStr.c_str());
147  PortableServer::ServantManager_var manager(_this());
148  _managedPoa->set_servant_manager(manager);
149 }
150 
151 
153 {
154  // pass
155 }
156 
157 
158 //
159 // Proxy
160 //
161 
162 
164 {
165  if(!CORBA::is_nil(_req))
166  {
167  Orb::inst().deferredRequest(_req._retn());
168  _req=CORBA::Request::_nil();
169  }
170 }
171 
172 Proxy::Proxy(PortableServer::POA_ptr poa)
173 : Servant(poa),
174  _req(CORBA::Request::_nil())
175 {
176  // pass
177 }
178 
179 void Proxy::keyOutput(ostream& os, const char* name)
180 {
181  PortableServer::POA_var parentPoa=_poa->the_parent();
182  CORBA::String_var channelName=parentPoa->the_name();
183 
184  PortableServer::ObjectId_var oid=_poa->servant_to_id(this);
185  CORBA::String_var oidStr =PortableServer::ObjectId_to_string(oid.in());
186  os<<"ecf/"<<channelName.in()<<"/"<<name<<"/"<<oidStr.in();
187 }
188 
189 void Proxy::eraseKey(const char* name)
190 {
192  {
193  // Remove this key from the persistency logfile.
194  WriteLock log;
195  log.os<<"-";
196  keyOutput(log.os,name);
197  log.os<<'\n';
198  }
199 }
200 
202  ostream& os,
203  const char* name,
204  CORBA::Object_ptr target,
205  const char* extraAttributes
206 )
207 {
208  keyOutput(os,name);
209  if(!CORBA::is_nil(target))
210  {
211  CORBA::String_var iorstr =Orb::inst()._orb->object_to_string(target);
212  os<<" IOR="<<iorstr.in();
213  if(extraAttributes)
214  os<<extraAttributes;
215  }
216  os<<" ;;\n";
217 }
218 
219 
220 }; // end namespace OmniEvents
OmniEvents::Orb::_orb
CORBA::ORB_var _orb
Definition: Orb.h:88
OmniEvents::ProxyManager::_servants
set< Proxy * > _servants
The set of all active Proxies in this object's _managedPoa.
Definition: ProxyManager.h:90
OmniEvents::PersistNode::_child
map< string, PersistNode * > _child
Definition: PersistNode.h:71
PersistNode.h
Orb.h
OmniEvents::Servant::_poa
PortableServer::POA_var _poa
Definition: Servant.h:131
OmniEvents::Proxy::Proxy
Proxy(PortableServer::POA_ptr poa)
Definition: ProxyManager.cc:172
OmniEvents::Proxy::basicOutput
void basicOutput(ostream &os, const char *name, CORBA::Object_ptr target=CORBA::Object::_nil(), const char *extraAttributes=NULL)
Helper method for constructing persistency output.
Definition: ProxyManager.cc:201
DB
#define DB(l, x)
Definition: Orb.h:49
OmniEvents::Proxy::reincarnate
virtual void reincarnate(const string &oid, const PersistNode &node)=0
Re-create a servant from information saved in the log file.
OmniEvents::ProxyManager::reincarnate
void reincarnate(const PersistNode &node)
Re-create servants from information saved in the log file.
Definition: ProxyManager.cc:60
OmniEvents::ProxyManager::ProxyManager
ProxyManager(PortableServer::POA_ptr poa)
Definition: ProxyManager.cc:96
OmniEvents::ProxyManager::activate
void activate(const char *name)
Creates the Proxy-type's POA, and registers this object as its ServantManager.
Definition: ProxyManager.cc:103
OmniEvents::ProxyManager::output
void output(ostream &os)
Save this object's state to a stream.
Definition: ProxyManager.cc:87
OmniEvents::Servant::activateObjectWithId
void activateObjectWithId(const char *oidStr)
Calls activate_object_with_id() to activate this servant in its POA.
Definition: Servant.cc:125
OmniEvents::Orb::inst
static Orb & inst()
Definition: Orb.h:81
OmniEvents::PersistNode
Definition: PersistNode.h:48
OmniEvents::WriteLock
Obtains an output stream to the active persistancy logfile, and locks it for exclusive access.
Definition: omniEventsLog.h:242
OmniEvents::Proxy
Base class for three of the four Proxy servants.
Definition: ProxyManager.h:104
OmniEvents::omniEventsLog::exists
static bool exists()
Library code may create Event Service objects without the need for persistency.
Definition: omniEventsLog.h:144
OmniEvents
Definition: Callback.h:39
omniEventsLog.h
OmniEvents::Proxy::eraseKey
void eraseKey(const char *name)
Helper method for constructing persistency output.
Definition: ProxyManager.cc:189
OmniEvents::ProxyManager::_managedPoa
PortableServer::POA_var _managedPoa
The POA owned & managed by this object.
Definition: ProxyManager.h:95
OmniEvents::Proxy::keyOutput
void keyOutput(ostream &os, const char *name)
Helper method for constructing persistency output.
Definition: ProxyManager.cc:179
OmniEvents::ProxyManager::~ProxyManager
virtual ~ProxyManager()
Definition: ProxyManager.cc:152
OmniEvents::ProxyManager::etherealize
void etherealize(const PortableServer::ObjectId &oid, PortableServer::POA_ptr adapter, PortableServer::Servant serv, CORBA::Boolean cleanup_in_progress, CORBA::Boolean remaining_activations)
Implements etherealize() method from ServantActivator interface.
Definition: ProxyManager.cc:41
OmniEvents::Proxy::~Proxy
virtual ~Proxy()
Definition: ProxyManager.cc:163
ProxyManager.h
OmniEvents::WriteLock::os
ostream & os
Definition: omniEventsLog.h:254
OmniEvents::Orb::deferredRequest
void deferredRequest(CORBA::Request_ptr req, Callback *callback=NULL)
Adopts the request and then stores it in _deferredRequests.
Definition: Orb.cc:187
OmniEvents::Servant
Base class for servants.
Definition: Servant.h:113