Salome HOME
Copyrights update
[modules/superv.git] / src / Supervision / SuperV_Client_omniORB4.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <iostream>
22 #include <CORBA.h>
23 #include "ObjRef_omniORB4.hh"
24
25 #include <stdarg.h>
26 #include <map>
27
28 using namespace std;
29
30 using namespace CORBA ;
31
32 CORBA::ORB_var _ORB ; 
33
34 static void DynInvoke( ObjRef_omniORB4::ObjRef_ptr objComponent ,
35                        const char *method , 
36                        CORBA::Any * inParams , int nInParams ,
37                        CORBA::Any * outParams , int nOutParams ) {
38
39   Request_var req = objComponent->_request( method ) ;
40   const char *ArgName ;
41   int Type ;
42   int i ;
43
44   NVList_ptr arguments = req->arguments() ;
45
46   for ( i = 0 ; i < nInParams ; i++ ) {
47     CORBA::Any & data = inParams[i] ;
48     ArgName           = "" ;
49     Type              = data.type()->kind() ;
50     cout << "DynInvoke CORBA::ARG_IN" << i << " Type " << Type << endl ;
51     arguments->add_value( ArgName , data , CORBA::ARG_IN ) ;
52   }
53 //Check ObjectReference in ArgInput1
54   CORBA::Object_ptr myObjRefFromAny ;
55 //The folowing line runs with omniORB3 but not with omniORB4 :
56 //  inParams[0] >>= myObjRefFromAny ;
57 //The folowing line runs with omniORB4 !!!...
58   inParams[0] >>= (CORBA::Any::to_object ) myObjRefFromAny ;
59   CORBA::String_var myObjRefIORFromAny ;
60   myObjRefIORFromAny = _ORB->object_to_string( myObjRefFromAny ) ;
61   if ( CORBA::is_nil( myObjRefFromAny ) ) {
62     cout << "The object reference from the Any is a nil reference in DynInvoke "
63          << myObjRefIORFromAny << endl ;
64   }
65   else {
66     cout << "The object reference from the Any in DynInvoke is "
67          << myObjRefIORFromAny << endl ;
68   }
69
70
71   for ( i = 0 ; i < nOutParams ; i++ ) {
72     CORBA::Any & data = outParams[i] ;
73     ArgName           = "" ;
74     Type              = data.type()->kind() ;
75     cout << "DynInvoke CORBA::ARG_IN" << i << " Type " << Type << endl ;
76     arguments->add_value( ArgName , data , CORBA::ARG_OUT ) ;
77   }
78
79   req->invoke();
80
81   if( req->env()->exception() ) {
82     req->env()->exception()->_raise() ;
83     return ; // pas utile ?
84   }
85
86   for ( i = 0 ; i < nOutParams ; i++ ) {
87     outParams[i] = *( arguments->item( i + nInParams )->value() ) ;
88   }
89
90   return;
91
92 }
93
94 int main( int argc , char ** argv ) {
95
96   try {
97     if ( argc != 2 ) {
98       cout << "Usage : SuperV_Client_omniORB4  IOR" << endl ;
99       return 1 ;
100     }
101     CORBA::ORB_var orb = CORBA::ORB_init( argc , argv , "omniORB4" ) ;
102     _ORB = orb ;
103     CORBA::Object_var obj = orb->string_to_object( argv[1] ) ;
104     ObjRef_omniORB4::ObjRef_var myObjRef = ObjRef_omniORB4::ObjRef::_narrow( obj ) ;
105     if ( CORBA::is_nil( myObjRef ) ) {
106       cout << "Cannot narrow the object reference or it was a nil reference" << endl ;
107       return 1 ;
108     }
109
110 //ArgInput1 :
111     CORBA::Any myAnyObjRef ;
112     myAnyObjRef <<= myObjRef ;
113 //Check ObjectReference in ArgInput1
114     CORBA::Object_ptr myObjRefFromAny ;
115 //The folowing line runs with omniORB3 but not with omniORB4 :
116 //    myAnyObjRef >>= myObjRefFromAny ;
117 //The folowing line runs with omniORB4 !!!...
118     myAnyObjRef >>= (CORBA::Any::to_object ) myObjRefFromAny ;
119     CORBA::String_var myObjRefIORFromAny ;
120     myObjRefIORFromAny = orb->object_to_string( myObjRefFromAny ) ;
121     if ( CORBA::is_nil( myObjRefFromAny ) ) {
122       cout << "The object reference from the Any is a nil reference before DynInvoke "
123            << myObjRefIORFromAny << endl ;
124     }
125     else {
126       cout << "The object reference from the Any before DynInvoke is "
127            << myObjRefIORFromAny << endl ;
128     }
129
130 //ArgOutput1 :
131     CORBA::Any myAnygetObjRef ;
132     myAnygetObjRef <<= ObjRef_omniORB4::ObjRef::_nil() ;
133
134 //Dynamic invocation
135     DynInvoke( myObjRef , "getObjRef" , &myAnyObjRef , 1 , &myAnygetObjRef , 1 ) ;
136
137 //Check ObjectReference in ArgInput1
138     if ( CORBA::is_nil( myObjRefFromAny ) ) {
139       cout << "The object reference from the Any is a nil reference after DynInvoke "
140            << myObjRefIORFromAny << endl ;
141     }
142     else {
143       cout << "The object reference from the Any after DynInvoke is "
144            << myObjRefIORFromAny << endl ;
145     }
146
147 //ArgOutput1 :
148     CORBA::Object_ptr mygetObjRef ;
149     myAnygetObjRef >>= mygetObjRef ;
150     CORBA::String_var mygetObjRefIOR = orb->object_to_string( mygetObjRef ) ;
151     if ( CORBA::is_nil( mygetObjRef ) ) {
152       cout << "The returned object reference is a nil reference "
153            << (char * ) mygetObjRefIOR << endl ;
154       return 1 ;
155     }
156     cout << "The returned object reference is " << mygetObjRefIOR << endl ;
157     ObjRef_omniORB4::ObjRef_var mygetObjRefnarrowed ;
158     mygetObjRefnarrowed = ObjRef_omniORB4::ObjRef::_narrow( mygetObjRef ) ;
159     if ( CORBA::is_nil( mygetObjRef ) ) {
160       cout << "The returned narrowed object reference is a nil reference "
161            << mygetObjRefIOR << endl ;
162       return 1 ;
163     }
164
165     mygetObjRefnarrowed->ping() ;
166
167     orb->destroy() ;
168   }
169   catch(CORBA::COMM_FAILURE & ex ) {
170     cout << "Caught CORBA::COMM_FAILURE" << endl ;
171   }
172   catch(CORBA::SystemException & ) {
173     cout << "Caught CORBA::SystemException" << endl ;
174   }
175   catch(CORBA::Exception & ) {
176     cout << "Caught CORBA::Exception" << endl ;
177   }
178   catch(omniORB::fatalException & fe ) {
179     cout << "Caught omniORB::fatalException" << endl ;
180     cout << "      file   " << fe.file() << endl ;
181     cout << "      line   " << fe.line() << endl ;
182     cout << "      errmsg " << fe.errmsg() << endl ;
183   }
184   catch( ... ) {
185     cout << "Caught unknown exception" << endl ;
186   }
187   return 0 ;
188 }