CorbaUtil.H
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 #ifndef CORBAUTIL_H
00039 #define CORBAUTIL_H
00040
00041 #include <omniORB4/Naming.hh>
00042 #include <omniORB4/omniURI.h>
00043 #include <cstdio>
00044
00045 OMNI_USING_NAMESPACE(omni);
00046
00047
00048
00049 inline bool getMultiObjectRef(CORBA::ORB_ptr orb, const char *contextName, CORBA::Object_ptr *dst, int &nObj){
00050 LINFO("Looking for objects");
00051 CosNaming::NamingContext_var rootContext;
00052
00053 try {
00054
00055 CORBA::Object_var obj;
00056 obj = orb->resolve_initial_references("NameService");
00057
00058 rootContext = CosNaming::NamingContext::_narrow(obj);
00059 if( CORBA::is_nil(rootContext) ) {
00060 LFATAL("Failed to narrow the root naming context.");
00061 return false;
00062 }
00063 }
00064 catch(CORBA::ORB::InvalidName& ex) {
00065
00066 LFATAL("Service required is invalid [does not exist].");
00067 return false;
00068 }
00069
00070
00071
00072 CosNaming::Name_var name = omniURI::stringToName(contextName);
00073
00074 CORBA::Object_var obj = rootContext->resolve(name);
00075
00076 CosNaming::NamingContext_var context;
00077 context = CosNaming::NamingContext::_narrow(obj);
00078
00079 if (CORBA::is_nil(context)) {
00080 LINFO("No objects found\n");
00081 return false;
00082 }
00083
00084
00085
00086 CosNaming::BindingIterator_var bi;
00087 CosNaming::BindingList_var bl;
00088 CosNaming::Binding_var b;
00089
00090 context->list(0, bl, bi);
00091
00092 if (CORBA::is_nil(bi)){
00093 LINFO("No objects found\n");
00094 return false;
00095 }
00096
00097 nObj=0;
00098 while (bi->next_one(b)){
00099
00100 CosNaming::Name name_comp = b->binding_name;
00101
00102 char* name = omniURI::nameToString(name_comp);
00103 LINFO("Binding to %s ... ", name);
00104 delete name;
00105
00106 CORBA::Object_ptr ref = context->resolve(name_comp);
00107 dst[nObj] = ref;
00108
00109 if (!CORBA::is_nil(dst[nObj])){
00110 nObj++;
00111 LINFO("Done\n");
00112 } else {
00113 LINFO("Fail\n");
00114 }
00115
00116 }
00117 bi->destroy();
00118
00119 return true;
00120 }
00121
00122
00123
00124 inline static CORBA::Boolean
00125 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,
00126 const char* contextId, const char* contextKind,
00127 const char* objectPrefix, CosNaming::Name& objectName)
00128 {
00129 CosNaming::NamingContext_var rootContext;
00130
00131 try {
00132
00133 CORBA::Object_var obj;
00134 obj = orb->resolve_initial_references("NameService");
00135
00136 if( CORBA::is_nil(obj) ) {
00137 LFATAL("Obj is null.");
00138 return 0;
00139 }
00140
00141
00142 rootContext = CosNaming::NamingContext::_narrow(obj);
00143 if( CORBA::is_nil(rootContext) ) {
00144 LFATAL("Failed to narrow the root naming context.");
00145 return 0;
00146 }
00147 }
00148 catch(CORBA::ORB::InvalidName& ex) {
00149
00150 LFATAL("Service required is invalid [does not exist].");
00151 return 0;
00152 }
00153
00154 try {
00155
00156
00157 CosNaming::Name contextName;
00158 contextName.length(1);
00159 contextName[0].id = (const char*)contextId;
00160 contextName[0].kind = (const char*)contextKind;
00161
00162
00163
00164 CosNaming::NamingContext_var testContext;
00165 try {
00166
00167 testContext = rootContext->bind_new_context(contextName);
00168 }
00169 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00170
00171
00172
00173 CORBA::Object_var obj;
00174 obj = rootContext->resolve(contextName);
00175 testContext = CosNaming::NamingContext::_narrow(obj);
00176 if( CORBA::is_nil(testContext) ) {
00177 LFATAL("Failed to narrow naming context.");
00178 return 0;
00179 }
00180 }
00181
00182
00183 objectName.length(1);
00184
00185
00186 bool bound = false;
00187 char CmapID[100];
00188 for (int i=0; i<100 && !bound; i++) {
00189 sprintf(CmapID, "%s_%i", objectPrefix, i);
00190 LINFO("Binding object %s", CmapID);
00191 objectName[0].id = (const char*) CmapID;
00192 objectName[0].kind = (const char*) "Object";
00193
00194 bound = true;
00195 try {
00196 testContext->bind(objectName, objref);
00197 }
00198 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00199
00200 bound = false;
00201 }
00202
00203 }
00204
00205 if (!bound){
00206 LFATAL("Can not bind object");
00207 return 0;
00208 } else {
00209 }
00210
00211
00212
00213
00214
00215
00216 }
00217 catch(CORBA::COMM_FAILURE& ex) {
00218 LFATAL("Caught system exception COMM_FAILURE -- unable to contact the naming service.");
00219 return 0;
00220 }
00221 catch(CORBA::SystemException&) {
00222 LFATAL("Caught a CORBA::SystemException while using the naming service.");
00223 return 0;
00224 }
00225
00226 return 1;
00227 }
00228
00229
00230 inline void unbindObject (CORBA::ORB_ptr orb, const char* contextId,
00231 const char* contextKind,
00232 CosNaming::Name &objectName){
00233 CosNaming::NamingContext_var rootContext;
00234
00235 try {
00236
00237 CORBA::Object_var obj;
00238 obj = orb->resolve_initial_references("NameService");
00239
00240 if( CORBA::is_nil(obj) ) {
00241 LFATAL("Obj is null.");
00242 return;
00243 }
00244
00245
00246 rootContext = CosNaming::NamingContext::_narrow(obj);
00247 if( CORBA::is_nil(rootContext) ) {
00248 LFATAL("Failed to narrow the root naming context.");
00249 return;
00250 }
00251 }
00252 catch(CORBA::ORB::InvalidName& ex) {
00253
00254 LFATAL("Service required is invalid [does not exist].");
00255 return;
00256 }
00257
00258 CosNaming::Name contextName;
00259 contextName.length(2);
00260 contextName[0].id = (const char*)contextId;
00261 contextName[0].kind = (const char*)contextKind;
00262 contextName[1] = objectName[0];
00263
00264 rootContext->unbind(contextName);
00265 }
00266
00267
00268 inline Point2DOrb toOrb(const Point2D<int> &p){
00269 Point2DOrb_var pOrb;
00270 pOrb->i = p.i;
00271 pOrb->j = p.j;
00272 return pOrb._retn();
00273 }
00274
00275 #endif
00276