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