3 #include "RuntimeSALOME.hxx"
5 #include "Exception.hxx"
6 #include "Executor.hxx"
7 #include "Dispatcher.hxx"
15 #include "YacsTrace.hxx"
19 YACS::YACSLoader::YACSLoader* loader;
21 YACS_ORB::YACS_Gen_var myyacsref;
23 class MyDispatcher:public YACS::ENGINE::Dispatcher
26 void dispatch(YACS::ENGINE::Node* object, const std::string& event)
28 std::cerr << "dispatch " << object->getNumId() << std::endl;
29 typedef std::set<YACS_ORB::Observer_ptr>::iterator jt;
30 std::pair<int,std::string> key(object->getNumId(),event);
31 for(jt iter=_observers[key].begin();iter!=_observers[key].end();iter++)
33 (*iter)->notifyObserver((CORBA::Long)object->getNumId(),event.c_str());
37 void addObserver(YACS_ORB::Observer_ptr observer,int numid, const std::string& event)
39 _observers[std::pair<int,std::string>(numid,event)].insert(YACS_ORB::Observer::_duplicate(observer));
43 std::map< std::pair<int,std::string> , std::set<YACS_ORB::Observer_ptr> > _observers;
46 class Yacs_i : public POA_YACS_ORB::YACS_Gen,
47 public PortableServer::RefCountServantBase
52 YACS_ORB::Proc_ptr Load(const char* xmlFile);
53 void Run(YACS_ORB::Proc_ptr p);
54 void addObserver(YACS_ORB::Observer_ptr observer, CORBA::Long numid,const char* event);
57 class Proc_i : public POA_YACS_ORB::Proc,
58 public PortableServer::RefCountServantBase
61 inline Proc_i(YACS::ENGINE::Proc* p) {_proc=p;};
64 CORBA::Long getState(CORBA::Long numid);
65 char * getXMLState(CORBA::Long numid);
66 void getIds(YACS_ORB::longArray_out numids,YACS_ORB::stringArray_out names);
68 YACS::ENGINE::Executor _executor;
69 YACS::ENGINE::Proc* _proc;
74 _executor.RunW(_proc,0);
77 CORBA::Long Proc_i::getState(CORBA::Long numid)
79 if(YACS::ENGINE::Node::idMap.count(numid) == 0)
81 std::cerr << "Unknown node id " << numid << std::endl;
82 return (CORBA::Long)-1;
84 YACS::ENGINE::Node* node= YACS::ENGINE::Node::idMap[numid];
85 CORBA::Long state=node->getEffectiveState();
89 char * Proc_i::getXMLState(CORBA::Long numid)
91 if(YACS::ENGINE::Node::idMap.count(numid) == 0)
93 std::cerr << "Unknown node id " << numid << std::endl;
94 return "<state>unknown</state>";
96 YACS::ENGINE::Node* node= YACS::ENGINE::Node::idMap[numid];
97 std::stringstream msg;
98 msg << "<state>" << node->getEffectiveState() << "</state>";
99 msg << "<name>" << node->getQualifiedName() << "</name>";
100 msg << "<id>" << numid << "</id>";
101 return CORBA::string_dup(msg.str().c_str());
104 void Proc_i::getIds(YACS_ORB::longArray_out numids,YACS_ORB::stringArray_out names)
106 std::list<YACS::ENGINE::Node *> nodes=_proc->getAllRecursiveNodes();
107 int len=nodes.size();
108 names=new YACS_ORB::stringArray;
109 numids=new YACS_ORB::longArray;
113 for(list<YACS::ENGINE::Node *>::const_iterator iter=nodes.begin();iter!=nodes.end();iter++,i++)
115 (*names)[i]=CORBA::string_dup((*iter)->getQualifiedName().c_str());
116 (*numids)[i]=(*iter)->getNumId();
120 YACS_ORB::Proc_ptr Yacs_i::Load(const char* xmlFile)
122 YACS::ENGINE::Proc* proc=loader->load(xmlFile);
123 Proc_i* p=new Proc_i(proc);
124 YACS_ORB::Proc_ptr pp = p->_this();
128 void Yacs_i::addObserver(YACS_ORB::Observer_ptr observer, CORBA::Long numid,const char* event)
130 ((MyDispatcher*)YACS::ENGINE::Dispatcher::getDispatcher())->addObserver(observer,numid,event);
133 void Yacs_i::Run(YACS_ORB::Proc_ptr p)
135 Proc_i *servant=dynamic_cast<Proc_i *> (PortableServer::POA::_the_root_poa()->reference_to_servant(p));
139 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
140 static ostream& operator<<(ostream& os, const CORBA::Exception& e);
142 int main(int argc, char** argv)
144 YACS::ENGINE::RuntimeSALOME::setRuntime();
145 loader= new YACS::YACSLoader::YACSLoader();
146 MyDispatcher* disp=new MyDispatcher();
147 YACS::ENGINE::Dispatcher::setDispatcher(disp);
151 orb = CORBA::ORB_init(argc, argv);
154 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
155 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
157 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
160 // Create and activate servant
161 Yacs_i* myyacs = new Yacs_i();
162 // Obtain a reference to the object, and print it out as a
164 obj = myyacs->_this();
165 CORBA::String_var sior(orb->object_to_string(obj));
166 DEBTRACE("'" << (char*)sior << "'");
167 myyacsref = YACS_ORB::YACS_Gen::_narrow(obj);
169 if( !bindObjectToName(orb, myyacsref,"Yacs") ) return 1;
171 // Decrement the reference count of the object implementation, so
172 // that it will be properly cleaned up when the POA has determined
173 // that it is no longer needed.
174 myyacs->_remove_ref();
178 catch(CORBA::SystemException&) {
179 DEBTRACE("Caught CORBA::SystemException.");
181 catch(CORBA::Exception& ex) {
182 DEBTRACE("Caught CORBA::Exception." << ex);
184 catch(omniORB::fatalException& fe) {
185 DEBTRACE("Caught omniORB::fatalException:");
186 DEBTRACE(" file: " << fe.file());
187 DEBTRACE(" line: " << fe.line());
188 DEBTRACE(" mesg: " << fe.errmsg());
191 DEBTRACE("Caught unknown exception." );
198 //////////////////////////////////////////////////////////////////////
200 static CORBA::Boolean
201 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
203 CosNaming::NamingContext_var rootContext;
206 // Obtain a reference to the root context of the Name service:
207 CORBA::Object_var obj;
208 obj = orb->resolve_initial_references("NameService");
210 // Narrow the reference returned.
211 rootContext = CosNaming::NamingContext::_narrow(obj);
212 if( CORBA::is_nil(rootContext) ) {
213 DEBTRACE("Failed to narrow the root naming context.");
217 catch(CORBA::ORB::InvalidName& ex) {
218 // This should not happen!
219 DEBTRACE("Service required is invalid [does not exist]." );
224 // Bind a context called "test" to the root context:
226 CosNaming::Name contextName;
227 contextName.length(1);
228 contextName[0].id = (const char*) "test"; // string copied
229 contextName[0].kind = (const char*) "my_context"; // string copied
230 // Note on kind: The kind field is used to indicate the type
231 // of the object. This is to avoid conventions such as that used
232 // by files (name.type -- e.g. test.ps = postscript etc.)
234 CosNaming::NamingContext_var testContext;
236 // Bind the context to root.
237 testContext = rootContext->bind_new_context(contextName);
239 catch(CosNaming::NamingContext::AlreadyBound& ex) {
240 // If the context already exists, this exception will be raised.
241 // In this case, just resolve the name and assign testContext
242 // to the object returned:
243 CORBA::Object_var obj;
244 obj = rootContext->resolve(contextName);
245 testContext = CosNaming::NamingContext::_narrow(obj);
246 if( CORBA::is_nil(testContext) ) {
247 DEBTRACE("Failed to narrow naming context.");
252 // Bind objref with name name to the testContext:
253 CosNaming::Name objectName;
254 objectName.length(1);
255 objectName[0].id = name; // string copied
256 objectName[0].kind = (const char*) "Object"; // string copied
259 testContext->bind(objectName, objref);
261 catch(CosNaming::NamingContext::AlreadyBound& ex) {
262 testContext->rebind(objectName, objref);
265 catch(CORBA::COMM_FAILURE& ex) {
266 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
267 << "naming service.");
270 catch(CORBA::SystemException&) {
271 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");
278 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
282 CORBA::TypeCode_var tc = tmp.type();
283 const char *p = tc->name();