Salome HOME
updated copyright message
[modules/yacs.git] / src / yacsorb / yacs_clt.cxx
1 // Copyright (C) 2006-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Python.h>
21 #include <yacs.hh>
22 #include "RuntimeSALOME.hxx"
23 #include "Proc.hxx"
24 #include "parsers.hxx"
25
26 #include <vector>
27 #include <string>
28 #include <iostream>
29 #include <pthread.h>
30
31 YACS::YACSLoader::YACSLoader* loader;
32
33 class Obs_i : public POA_YACS_ORB::Observer,
34               public PortableServer::RefCountServantBase
35 {
36 public:
37   inline Obs_i(YACS_ORB::Proc_ptr proc) {_server_proc=proc;}
38   virtual ~Obs_i() {}
39   void notifyObserver(CORBA::Long numid, const char* event);
40 protected:
41   YACS_ORB::Proc_ptr _server_proc;
42 };
43
44 void Obs_i::notifyObserver(CORBA::Long numid, const char* event)
45 {
46   std::cerr << "Obs_i::notifyObserver " << numid << event << std::endl;
47   std::cerr << "Obs_i::notifyObserver:state= " << _server_proc->getState(numid) << std::endl;
48   std::cerr << "Obs_i::notifyObserver:XMLstate= " << _server_proc->getXMLState(numid) << std::endl;
49 }
50
51 YACS_ORB::YACS_Gen_var yacsref;
52 YACS_ORB::Proc_ptr server_proc;
53
54 void * run(void *obj)
55 {
56     yacsref->Run(server_proc);
57 }
58
59 int main(int argc, char** argv)
60 {
61   YACS::ENGINE::RuntimeSALOME::setRuntime();
62   loader= new YACS::YACSLoader::YACSLoader();
63
64   try 
65   {
66     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
67     CORBA::Object_var obj = orb->string_to_object("corbaname:rir:#test.my_context/Yacs.Object");
68     yacsref = YACS_ORB::YACS_Gen::_narrow(obj);
69     if( CORBA::is_nil(yacsref) ) 
70       {
71         std::cerr << "Can't narrow reference to type yacs (or it was nil)." << std::endl;
72         return 1;
73       }
74
75     //Activate POA
76     CORBA::Object_var poa = orb->resolve_initial_references("RootPOA");
77     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(poa);
78     PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
79     poa_man->activate();
80
81     char* xmlFile="/local/chris/SALOME2/SUPERV/YACS/BR_CC/YACS_SRC/src/yacsloader/samples/aschema.xml";
82     //Load XML file in client
83     YACS::ENGINE::Proc* local_proc=loader->load(xmlFile);
84     //Load xml file in server
85     server_proc = yacsref->Load(xmlFile);
86     //Create an observer for server_proc
87     Obs_i* obs=new Obs_i(server_proc);
88     YACS_ORB::Observer_ptr obs_ptr = obs->_this();
89     //Get ids and names
90     YACS_ORB::stringArray_var names;
91     YACS_ORB::longArray_var ids;
92     server_proc->getIds(ids.out(),names.out());
93     //Register it 
94     int len=ids->length();
95     int numid;
96     for(int i=0;i<len;i++)
97       {
98         numid=ids[i];
99         yacsref->addObserver(obs_ptr,numid,"status");
100       }
101     //Execute Proc in thread ??
102     pthread_t th;
103     //pthread_create(&th,NULL,run,0);
104     yacsref->Run(server_proc);
105
106     //orb->run();
107     orb->destroy();
108   }
109   catch(CORBA::COMM_FAILURE& ex) {
110       std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
111          << "object." << std::endl;
112   }
113   catch(CORBA::SystemException&) {
114       std::cerr << "Caught a CORBA::SystemException." << std::endl;
115   }
116   catch(CORBA::Exception&) {
117       std::cerr << "Caught CORBA::Exception." << std::endl;
118   }
119   catch(omniORB::fatalException& fe) {
120       std::cerr << "Caught omniORB::fatalException:" << std::endl;
121       std::cerr << "  file: " << fe.file() << std::endl;
122       std::cerr << "  line: " << fe.line() << std::endl;
123       std::cerr << "  mesg: " << fe.errmsg() << std::endl;
124   }
125   catch(...) {
126       std::cerr << "Caught unknown exception." << std::endl;
127   }
128   return 0;
129 }