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