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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 #ifdef HAVE_CONFIG_H
00158 # include "config.h"
00159 #endif
00160
00161 #ifdef HAVE_GETOPT
00162 # include <unistd.h>
00163 extern char* optarg;
00164 extern int optind;
00165 #else
00166 # include "getopt.h"
00167 #endif
00168
00169 #ifdef HAVE_IOSTREAM
00170 # include <iostream>
00171 #else
00172 # include <iostream.h>
00173 #endif
00174
00175 #ifdef HAVE_STD_IOSTREAM
00176 using namespace std;
00177 #endif
00178
00179 #ifdef HAVE_STDLIB_H
00180 # include <stdlib.h>
00181 #endif
00182
00183 #ifdef HAVE_SIGNAL_H
00184 # include <signal.h>
00185 #endif
00186
00187 #include <cstdio>
00188
00189 #include "omniEvents.hh"
00190 #include "naming.h"
00191
00192 static void usage(int argc, char **argv);
00193 static void appendCriterion( CosLifeCycle::Criteria&,const char*,const char*);
00194 static void appendCriterionStr(CosLifeCycle::Criteria&,const char*,const char*);
00195
00196 int
00197 main(int argc, char **argv)
00198 {
00199 int result =1;
00200
00201
00202
00203 CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
00204
00205
00206 bool verbose =false;
00207 bool needNameService =false;
00208 const char* channelName ="EventChannel";
00209 const char* factoryName ="EventChannelFactory";
00210 CosLifeCycle::Criteria criteria;
00211
00212 int c;
00213 while ((c = getopt(argc,argv,"n:N:m:c:i:p:q:R:r:t:vh")) != EOF)
00214 {
00215 switch (c)
00216 {
00217 case 'n':
00218 channelName=optarg;
00219 needNameService=true;
00220 break;
00221
00222 case 'N':
00223 factoryName=optarg;
00224 break;
00225
00226 case 'm':
00227 appendCriterion(criteria,"MaxEventsPerConsumer",optarg);
00228 break;
00229
00230 case 'c':
00231 appendCriterion(criteria,"CyclePeriod_ns",optarg);
00232 break;
00233
00234 case 'i':
00235 appendCriterionStr(criteria,"InsName",optarg);
00236 break;
00237
00238 case 'p':
00239 appendCriterion(criteria,"MaxNumProxies",optarg);
00240 break;
00241
00242 case 'q':
00243 appendCriterion(criteria,"MaxQueueLength",optarg);
00244 break;
00245
00246 case 'R':
00247 appendCriterion(criteria,"PullRetryPeriod_ms",optarg);
00248 break;
00249
00250 case 'r':
00251 appendCriterion(criteria,"PullRetryPeriod",optarg);
00252 break;
00253
00254 case 't':
00255 appendCriterionStr(criteria,"FilterId",optarg);
00256 break;
00257
00258 case 'v':
00259 verbose=true;
00260 break;
00261
00262 case 'h':
00263 usage(argc,argv);
00264 exit(0);
00265
00266 default :
00267 usage(argc,argv);
00268 exit(-1);
00269 }
00270 }
00271
00272 needNameService=(needNameService || optind>=argc);
00273
00274
00275
00276
00277 const char* action ="start";
00278 try
00279 {
00280 CORBA::Object_var obj;
00281
00282
00283
00284 CosNaming::NamingContext_var rootContext=CosNaming::NamingContext::_nil();
00285 try {
00286 action="resolve initial reference 'NameService'";
00287 obj=orb->resolve_initial_references("NameService");
00288 rootContext=CosNaming::NamingContext::_narrow(obj);
00289 if(CORBA::is_nil(rootContext))
00290 throw CORBA::OBJECT_NOT_EXIST();
00291 }
00292 catch (CORBA::Exception& ex) {
00293 if(needNameService)
00294 throw;
00295 else
00296 cerr<<"Warning - failed to "<<action<<"."<<endl;
00297 }
00298
00299
00300
00301
00302 if(optind<argc)
00303 {
00304 action="convert URI from command line into object reference";
00305 obj=orb->string_to_object(argv[optind]);
00306 }
00307 else
00308 {
00309 action="find Event Channel Factory in naming service";
00310 obj=rootContext->resolve(str2name(factoryName));
00311 }
00312
00313 action="narrow object reference to event channel factory";
00314 omniEvents::EventChannelFactory_var factory =
00315 omniEvents::EventChannelFactory::_narrow(obj);
00316 if(CORBA::is_nil(factory))
00317 {
00318 cerr << "Failed to narrow Event Channel Factory reference." << endl;
00319 exit(1);
00320 }
00321
00322
00323 action="check factory supports EventChannel object interface";
00324 CosLifeCycle::Key key;
00325 key.length (1);
00326 key[0].id = CORBA::string_dup("EventChannel");
00327 key[0].kind = CORBA::string_dup("object interface");
00328
00329 if(!factory->supports(key))
00330 {
00331 cerr << "Factory does not support Event Channel Interface! [\""
00332 << factoryName << "\"]" << endl;
00333 exit(1);
00334 }
00335
00336
00337
00338 action="create EventChannel object";
00339 CORBA::Object_var channelObj =factory->create_object(key, criteria);
00340 if (CORBA::is_nil(channelObj))
00341 {
00342 cerr << "Channel Factory returned nil reference! [\""
00343 << channelName << "\"]" << endl;
00344 exit(1);
00345 }
00346
00347
00348 CosEventChannelAdmin::EventChannel_var channel =
00349 CosEventChannelAdmin::EventChannel::_narrow(channelObj);
00350 if (CORBA::is_nil(channel))
00351 {
00352 cerr << "Failed to narrow Event Channel! [\""
00353 << channelName << "\"]" << endl;
00354 exit(1);
00355 }
00356
00357
00358 if(verbose)
00359 {
00360 CORBA::String_var sior =orb->object_to_string(channel);
00361 cout<<sior.in()<<endl;
00362 }
00363
00364
00365
00366 if(!CORBA::is_nil(rootContext))
00367 {
00368 CosNaming::Name name =str2name(channelName);
00369 try{
00370 action="register (bind) EventChannel with the naming service";
00371 rootContext->bind(name,channel.in());
00372 }
00373 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00374 action="register (rebind) EventChannel with the naming service";
00375 rootContext->rebind(name,channel.in());
00376 }
00377 }
00378
00379
00380
00381 action="destroy orb";
00382 orb->destroy();
00383
00384
00385
00386 result=0;
00387
00388 }
00389 catch (CosLifeCycle::NoFactory& ex) {
00390 cerr<<"Failed to create Event Channel: NoFactory"
00391 " (interface not supported) "<<endl;
00392 }
00393 catch (CosLifeCycle::CannotMeetCriteria& ex) {
00394 cerr<<"Failed to create Event Channel: CannotMeetCriteria "<<endl;
00395 }
00396 catch (CosLifeCycle::InvalidCriteria& ex) {
00397 cerr<<"Failed to create Event Channel: InvalidCriteria "<<endl;
00398 }
00399 catch (CORBA::COMM_FAILURE& ex) {
00400 cerr<<"System exception, unable to "<<action<<": COMM_FAILURE"<<endl;
00401 }
00402 catch (CORBA::SystemException& ex) {
00403 cerr<<"System exception, unable to "<<action;
00404 #if defined(HAVE_OMNIORB4)
00405 cerr<<" "<<ex._name();
00406 if(ex.NP_minorString())
00407 cerr<<" ("<<ex.NP_minorString()<<")";
00408 #endif
00409 cerr<<endl;
00410 }
00411 catch (CORBA::Exception& ex) {
00412 cerr<<"CORBA exception, unable to "<<action
00413 #ifdef HAVE_OMNIORB4
00414 <<": "<<ex._name()
00415 #endif
00416 << endl;
00417 }
00418 catch (omniORB::fatalException& ex) {
00419 cerr<<"Fatal Exception, unable to "<<action<<endl;
00420 }
00421
00422 return result;
00423 }
00424
00425 static void
00426 usage(int argc, char **argv)
00427 {
00428 cerr<<
00429 "\nCreate an EventChannel and register it in the naming service.\n"
00430 "syntax: "<<(argc?argv[0]:"eventc")<<" OPTIONS [FACTORY_URI]\n"
00431 "\n"
00432 "FACTORY_URI: The factory may be specified as a URI.\n"
00433 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
00434 " For example: corbaloc::localhost:11169/omniEvents\n"
00435 "\n"
00436 "OPTIONS: DEFAULT:\n"
00437 " -n channel name [\"EventChannel\"]\n"
00438 " -N factory name (if URI is not specified) [\"EventChannelFactory\"]\n"
00439 " -c override default CyclePeriod_ns of new channel (nanoseconds)\n"
00440 " -i set the InsName of new channel, to enable access via corbaloc\n"
00441 " -p override default MaxNumProxies of new channel\n"
00442 " -q override default MaxQueueLength of new channel\n"
00443 " -R override default PullRetryPeriod_ms for new channel (milliseconds)\n"
00444 " -t set an event type filter, FilterId=<RepositoryId>\n"
00445 " -v print the IOR of the new EventChannel to standard output.\n"
00446 " -h display this help text\n"
00447 "OLD OPTIONS: (only used by omniEvents v2.4 and earlier)\n"
00448 " -m override default MaxEventsPerConsumer for new channel\n" << endl;
00449 }
00450
00451 static void appendCriterion(
00452 CosLifeCycle::Criteria& criteria,
00453 const char* name,
00454 const char* value
00455 )
00456 {
00457 CORBA::ULong criteriaLen =criteria.length();
00458 ++criteriaLen;
00459 criteria.length(criteriaLen);
00460 criteria[criteriaLen-1].name=CORBA::string_dup(name);
00461 criteria[criteriaLen-1].value<<=CORBA::ULong(atol(value));
00462 }
00463
00464 static void appendCriterionStr(
00465 CosLifeCycle::Criteria& criteria,
00466 const char* name,
00467 const char* value
00468 )
00469 {
00470 CORBA::ULong criteriaLen =criteria.length();
00471 ++criteriaLen;
00472 criteria.length(criteriaLen);
00473 criteria[criteriaLen-1].name=CORBA::string_dup(name);
00474 criteria[criteriaLen-1].value<<=value;
00475 }