Salome HOME
Tests omniORB Client and Server
[modules/superv.git] / src / Supervision / SuperV_Client_omniORB4.cxx
1
2 #include <iostream>
3 #include <CORBA.h>
4 #include "ObjRef_omniORB4.hh"
5
6 #include <stdarg.h>
7 #include <map>
8
9 using namespace std;
10
11 using namespace CORBA ;
12
13 CORBA::ORB_var _ORB ; 
14
15 static void DynInvoke( ObjRef_omniORB4::ObjRef_ptr objComponent ,
16                        const char *method , 
17                        CORBA::Any * inParams , int nInParams ,
18                        CORBA::Any * outParams , int nOutParams ) {
19
20   Request_var req = objComponent->_request( method ) ;
21   const char *ArgName ;
22   int Type ;
23   int i ;
24
25   NVList_ptr arguments = req->arguments() ;
26
27   for ( i = 0 ; i < nInParams ; i++ ) {
28     CORBA::Any & data = inParams[i] ;
29     ArgName           = "" ;
30     Type              = data.type()->kind() ;
31     cout << "DynInvoke CORBA::ARG_IN" << i << " Type " << Type << endl ;
32     arguments->add_value( ArgName , data , CORBA::ARG_IN ) ;
33   }
34 //Check ObjectReference in ArgInput1
35   CORBA::Object_ptr myObjRefFromAny ;
36 //The folowing line runs with omniORB3 but not with omniORB4 :
37 //  inParams[0] >>= myObjRefFromAny ;
38 //The folowing line runs with omniORB4 !!!...
39   inParams[0] >>= (CORBA::Any::to_object ) myObjRefFromAny ;
40   CORBA::String_var myObjRefIORFromAny ;
41   myObjRefIORFromAny = _ORB->object_to_string( myObjRefFromAny ) ;
42   if ( CORBA::is_nil( myObjRefFromAny ) ) {
43     cout << "The object reference from the Any is a nil reference in DynInvoke "
44          << myObjRefIORFromAny << endl ;
45   }
46   else {
47     cout << "The object reference from the Any in DynInvoke is "
48          << myObjRefIORFromAny << endl ;
49   }
50
51
52   for ( i = 0 ; i < nOutParams ; i++ ) {
53     CORBA::Any & data = outParams[i] ;
54     ArgName           = "" ;
55     Type              = data.type()->kind() ;
56     cout << "DynInvoke CORBA::ARG_IN" << i << " Type " << Type << endl ;
57     arguments->add_value( ArgName , data , CORBA::ARG_OUT ) ;
58   }
59
60   req->invoke();
61
62   if( req->env()->exception() ) {
63     req->env()->exception()->_raise() ;
64     return ; // pas utile ?
65   }
66
67   for ( i = 0 ; i < nOutParams ; i++ ) {
68     outParams[i] = *( arguments->item( i + nInParams )->value() ) ;
69   }
70
71   return;
72
73 }
74
75 int main( int argc , char ** argv ) {
76
77   try {
78     if ( argc != 2 ) {
79       cout << "Usage : SuperV_Client_omniORB4  IOR" << endl ;
80       return 1 ;
81     }
82     CORBA::ORB_var orb = CORBA::ORB_init( argc , argv , "omniORB4" ) ;
83     _ORB = orb ;
84     CORBA::Object_var obj = orb->string_to_object( argv[1] ) ;
85     ObjRef_omniORB4::ObjRef_var myObjRef = ObjRef_omniORB4::ObjRef::_narrow( obj ) ;
86     if ( CORBA::is_nil( myObjRef ) ) {
87       cout << "Cannot narrow the object reference or it was a nil reference" << endl ;
88       return 1 ;
89     }
90
91 //ArgInput1 :
92     CORBA::Any myAnyObjRef ;
93     myAnyObjRef <<= myObjRef ;
94 //Check ObjectReference in ArgInput1
95     CORBA::Object_ptr myObjRefFromAny ;
96 //The folowing line runs with omniORB3 but not with omniORB4 :
97 //    myAnyObjRef >>= myObjRefFromAny ;
98 //The folowing line runs with omniORB4 !!!...
99     myAnyObjRef >>= (CORBA::Any::to_object ) myObjRefFromAny ;
100     CORBA::String_var myObjRefIORFromAny ;
101     myObjRefIORFromAny = orb->object_to_string( myObjRefFromAny ) ;
102     if ( CORBA::is_nil( myObjRefFromAny ) ) {
103       cout << "The object reference from the Any is a nil reference before DynInvoke "
104            << myObjRefIORFromAny << endl ;
105     }
106     else {
107       cout << "The object reference from the Any before DynInvoke is "
108            << myObjRefIORFromAny << endl ;
109     }
110
111 //ArgOutput1 :
112     CORBA::Any myAnygetObjRef ;
113     myAnygetObjRef <<= ObjRef_omniORB4::ObjRef::_nil() ;
114
115 //Dynamic invocation
116     DynInvoke( myObjRef , "getObjRef" , &myAnyObjRef , 1 , &myAnygetObjRef , 1 ) ;
117
118 //Check ObjectReference in ArgInput1
119     if ( CORBA::is_nil( myObjRefFromAny ) ) {
120       cout << "The object reference from the Any is a nil reference after DynInvoke "
121            << myObjRefIORFromAny << endl ;
122     }
123     else {
124       cout << "The object reference from the Any after DynInvoke is "
125            << myObjRefIORFromAny << endl ;
126     }
127
128 //ArgOutput1 :
129     CORBA::Object_ptr mygetObjRef ;
130     myAnygetObjRef >>= mygetObjRef ;
131     CORBA::String_var mygetObjRefIOR = orb->object_to_string( mygetObjRef ) ;
132     if ( CORBA::is_nil( mygetObjRef ) ) {
133       cout << "The returned object reference is a nil reference "
134            << (char * ) mygetObjRefIOR << endl ;
135       return 1 ;
136     }
137     cout << "The returned object reference is " << mygetObjRefIOR << endl ;
138     ObjRef_omniORB4::ObjRef_var mygetObjRefnarrowed ;
139     mygetObjRefnarrowed = ObjRef_omniORB4::ObjRef::_narrow( mygetObjRef ) ;
140     if ( CORBA::is_nil( mygetObjRef ) ) {
141       cout << "The returned narrowed object reference is a nil reference "
142            << mygetObjRefIOR << endl ;
143       return 1 ;
144     }
145
146     mygetObjRefnarrowed->ping() ;
147
148     orb->destroy() ;
149   }
150   catch(CORBA::COMM_FAILURE & ex ) {
151     cout << "Caught CORBA::COMM_FAILURE" << endl ;
152   }
153   catch(CORBA::SystemException & ) {
154     cout << "Caught CORBA::SystemException" << endl ;
155   }
156   catch(CORBA::Exception & ) {
157     cout << "Caught CORBA::Exception" << endl ;
158   }
159   catch(omniORB::fatalException & fe ) {
160     cout << "Caught omniORB::fatalException" << endl ;
161     cout << "      file   " << fe.file() << endl ;
162     cout << "      line   " << fe.line() << endl ;
163     cout << "      errmsg " << fe.errmsg() << endl ;
164   }
165   catch( ... ) {
166     cout << "Caught unknown exception" << endl ;
167   }
168   return 0 ;
169 }