Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / yacsorb / yacs_clt.cxx
1 #include <Python.h>
2 #include <yacs.hh>
3 #include "RuntimeSALOME.hxx"
4 #include "Proc.hxx"
5 #include "parsers.hxx"
6
7 #include <vector>
8 #include <string>
9 #include <iostream>
10 #include <pthread.h>
11
12 YACS::YACSLoader::YACSLoader* loader;
13
14 class Obs_i : public POA_YACS_ORB::Observer,
15               public PortableServer::RefCountServantBase
16 {
17 public:
18   inline Obs_i(YACS_ORB::Proc_ptr proc) {_server_proc=proc;}
19   virtual ~Obs_i() {}
20   void notifyObserver(CORBA::Long numid, const char* event);
21 protected:
22   YACS_ORB::Proc_ptr _server_proc;
23 };
24
25 void Obs_i::notifyObserver(CORBA::Long numid, const char* event)
26 {
27   std::cerr << "Obs_i::notifyObserver " << numid << event << std::endl;
28   std::cerr << "Obs_i::notifyObserver:state= " << _server_proc->getState(numid) << std::endl;
29   std::cerr << "Obs_i::notifyObserver:XMLstate= " << _server_proc->getXMLState(numid) << std::endl;
30 }
31
32 YACS_ORB::YACS_Gen_var yacsref;
33 YACS_ORB::Proc_ptr server_proc;
34
35 void * run(void *obj)
36 {
37     yacsref->Run(server_proc);
38 }
39
40 int main(int argc, char** argv)
41 {
42   YACS::ENGINE::RuntimeSALOME::setRuntime();
43   loader= new YACS::YACSLoader::YACSLoader();
44
45   try 
46   {
47     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
48     CORBA::Object_var obj = orb->string_to_object("corbaname:rir:#test.my_context/Yacs.Object");
49     yacsref = YACS_ORB::YACS_Gen::_narrow(obj);
50     if( CORBA::is_nil(yacsref) ) 
51       {
52         std::cerr << "Can't narrow reference to type yacs (or it was nil)." << std::endl;
53         return 1;
54       }
55
56     //Activate POA
57     CORBA::Object_var poa = orb->resolve_initial_references("RootPOA");
58     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(poa);
59     PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
60     poa_man->activate();
61
62     char* xmlFile="/local/chris/SALOME2/SUPERV/YACS/BR_CC/YACS_SRC/src/yacsloader/samples/aschema.xml";
63     //Load XML file in client
64     YACS::ENGINE::Proc* local_proc=loader->load(xmlFile);
65     //Load xml file in server
66     server_proc = yacsref->Load(xmlFile);
67     //Create an observer for server_proc
68     Obs_i* obs=new Obs_i(server_proc);
69     YACS_ORB::Observer_ptr obs_ptr = obs->_this();
70     //Get ids and names
71     YACS_ORB::stringArray_var names;
72     YACS_ORB::longArray_var ids;
73     server_proc->getIds(ids.out(),names.out());
74     //Register it 
75     int len=ids->length();
76     int numid;
77     for(int i=0;i<len;i++)
78       {
79         numid=ids[i];
80         yacsref->addObserver(obs_ptr,numid,"status");
81       }
82     //Execute Proc in thread ??
83     pthread_t th;
84     //pthread_create(&th,NULL,run,0);
85     yacsref->Run(server_proc);
86
87     //orb->run();
88     orb->destroy();
89   }
90   catch(CORBA::COMM_FAILURE& ex) {
91       std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
92          << "object." << std::endl;
93   }
94   catch(CORBA::SystemException&) {
95       std::cerr << "Caught a CORBA::SystemException." << std::endl;
96   }
97   catch(CORBA::Exception&) {
98       std::cerr << "Caught CORBA::Exception." << std::endl;
99   }
100   catch(omniORB::fatalException& fe) {
101       std::cerr << "Caught omniORB::fatalException:" << std::endl;
102       std::cerr << "  file: " << fe.file() << std::endl;
103       std::cerr << "  line: " << fe.line() << std::endl;
104       std::cerr << "  mesg: " << fe.errmsg() << std::endl;
105   }
106   catch(...) {
107       std::cerr << "Caught unknown exception." << std::endl;
108   }
109   return 0;
110 }