corbaUtil.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include "corbaUtil.h"
00040
00041
00042 bool getMultiObjectRef(CORBA::ORB_ptr orb, const char *contextName, CORBA::Object_ptr *dst, int &nObj){
00043 printf("Looking for objects\n");
00044 CosNaming::NamingContext_var rootContext;
00045
00046 try {
00047
00048 CORBA::Object_var obj;
00049 obj = orb->resolve_initial_references("NameService");
00050
00051 rootContext = CosNaming::NamingContext::_narrow(obj);
00052 if( CORBA::is_nil(rootContext) ) {
00053 printf("Failed to narrow the root naming context.\n");
00054 return false;
00055 }
00056 }
00057 catch(CORBA::ORB::InvalidName& ex) {
00058
00059 printf("Service required is invalid [does not exist].\n");
00060 return false;
00061 }
00062
00063
00064
00065 CosNaming::Name_var name = omniURI::stringToName(contextName);
00066
00067 CORBA::Object_var obj = rootContext->resolve(name);
00068
00069 CosNaming::NamingContext_var context;
00070 context = CosNaming::NamingContext::_narrow(obj);
00071
00072 if (CORBA::is_nil(context)) {
00073 printf("No objects found\n");
00074 return false;
00075 }
00076
00077
00078
00079 CosNaming::BindingIterator_var bi;
00080 CosNaming::BindingList_var bl;
00081 CosNaming::Binding_var b;
00082
00083 context->list(0, bl, bi);
00084
00085 if (CORBA::is_nil(bi)){
00086 printf("No objects found\n");
00087 return false;
00088 }
00089
00090 nObj=0;
00091 while (bi->next_one(b)){
00092
00093 CosNaming::Name name_comp = b->binding_name;
00094
00095 char* name = omniURI::nameToString(name_comp);
00096 printf("Binding to %s ... ", name);
00097 delete name;
00098
00099 CORBA::Object_ptr ref = context->resolve(name_comp);
00100 dst[nObj] = ref;
00101
00102 if (!CORBA::is_nil(dst[nObj])){
00103 nObj++;
00104 printf("Done\n");
00105 } else {
00106 printf("Fail\n");
00107 }
00108
00109 }
00110 bi->destroy();
00111
00112 return true;
00113 }
00114
00115
00116
00117 CORBA::Boolean
00118 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,
00119 const char* contextId, const char* contextKind,
00120 const char* objectPrefix, CosNaming::Name& objectName)
00121 {
00122 CosNaming::NamingContext_var rootContext;
00123
00124 try {
00125
00126 CORBA::Object_var obj;
00127 obj = orb->resolve_initial_references("NameService");
00128
00129 if( CORBA::is_nil(obj) ) {
00130 printf("Obj is null.\n");
00131 return 0;
00132 }
00133
00134
00135 rootContext = CosNaming::NamingContext::_narrow(obj);
00136 if( CORBA::is_nil(rootContext) ) {
00137 printf("Failed to narrow the root naming context.\n");
00138 return 0;
00139 }
00140 }
00141 catch(CORBA::ORB::InvalidName& ex) {
00142
00143 printf("Service required is invalid [does not exist].\n");
00144 return 0;
00145 }
00146
00147 try {
00148
00149
00150 CosNaming::Name contextName;
00151 contextName.length(1);
00152 contextName[0].id = (const char*)contextId;
00153 contextName[0].kind = (const char*)contextKind;
00154
00155
00156
00157 CosNaming::NamingContext_var testContext;
00158 try {
00159
00160 testContext = rootContext->bind_new_context(contextName);
00161 }
00162 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00163
00164
00165
00166 CORBA::Object_var obj;
00167 obj = rootContext->resolve(contextName);
00168 testContext = CosNaming::NamingContext::_narrow(obj);
00169 if( CORBA::is_nil(testContext) ) {
00170 printf("Failed to narrow naming context.\n");
00171 return 0;
00172 }
00173 }
00174
00175
00176 objectName.length(1);
00177
00178
00179 bool bound = false;
00180 char CmapID[100];
00181 for (int i=0; i<100 && !bound; i++) {
00182 sprintf(CmapID, "%s_%i", objectPrefix, i);
00183 printf("Binding object %s\n", CmapID);
00184 objectName[0].id = (const char*) CmapID;
00185 objectName[0].kind = (const char*) "Object";
00186
00187 bound = true;
00188 try {
00189 testContext->bind(objectName, objref);
00190 }
00191 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00192
00193 bound = false;
00194 }
00195
00196 }
00197
00198 if (!bound){
00199 printf("Can not bind object\n");
00200 return 0;
00201 } else {
00202 }
00203
00204
00205
00206
00207
00208
00209 }
00210 catch(CORBA::COMM_FAILURE& ex) {
00211 printf("Caught system exception COMM_FAILURE -- unable to contact the naming service.\n");
00212 return 0;
00213 }
00214 catch(CORBA::SystemException&) {
00215 printf("Caught a CORBA::SystemException while using the naming service.\n");
00216 return 0;
00217 }
00218
00219 return 1;
00220 }
00221
00222
00223 void unbindObject (CORBA::ORB_ptr orb, const char* contextId,
00224 const char* contextKind,
00225 CosNaming::Name &objectName){
00226 CosNaming::NamingContext_var rootContext;
00227
00228 try {
00229
00230 CORBA::Object_var obj;
00231 obj = orb->resolve_initial_references("NameService");
00232
00233 if( CORBA::is_nil(obj) ) {
00234 printf("Obj is null.\n");
00235 return;
00236 }
00237
00238
00239 rootContext = CosNaming::NamingContext::_narrow(obj);
00240 if( CORBA::is_nil(rootContext) ) {
00241 printf("Failed to narrow the root naming context.\n");
00242 return;
00243 }
00244 }
00245 catch(CORBA::ORB::InvalidName& ex) {
00246
00247 printf("Service required is invalid [does not exist].\n");
00248 return;
00249 }
00250
00251 CosNaming::Name contextName;
00252 contextName.length(2);
00253 contextName[0].id = (const char*)contextId;
00254 contextName[0].kind = (const char*)contextKind;
00255 contextName[1] = objectName[0];
00256
00257 rootContext->unbind(contextName);
00258 }