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 "omniEvents.hh"
00188 #include "naming.h"
00189
00190 static void usage(int argc, char **argv);
00191 static void appendCriterion( CosLifeCycle::Criteria&,const char*,const char*);
00192 static void appendCriterionStr(CosLifeCycle::Criteria&,const char*,const char*);
00193
00194 int
00195 main(int argc, char **argv)
00196 {
00197 int result =1;
00198
00199
00200
00201 CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
00202
00203
00204 bool verbose =false;
00205 bool needNameService =false;
00206 const char* channelName ="EventChannel";
00207 const char* factoryName ="EventChannelFactory";
00208 CosLifeCycle::Criteria criteria;
00209
00210 int c;
00211 while ((c = getopt(argc,argv,"n:N:m:c:i:p:q:R:r:t:vh")) != EOF)
00212 {
00213 switch (c)
00214 {
00215 case 'n':
00216 channelName=optarg;
00217 needNameService=true;
00218 break;
00219
00220 case 'N':
00221 factoryName=optarg;
00222 break;
00223
00224 case 'm':
00225 appendCriterion(criteria,"MaxEventsPerConsumer",optarg);
00226 break;
00227
00228 case 'c':
00229 appendCriterion(criteria,"CyclePeriod_ns",optarg);
00230 break;
00231
00232 case 'i':
00233 appendCriterionStr(criteria,"InsName",optarg);
00234 break;
00235
00236 case 'p':
00237 appendCriterion(criteria,"MaxNumProxies",optarg);
00238 break;
00239
00240 case 'q':
00241 appendCriterion(criteria,"MaxQueueLength",optarg);
00242 break;
00243
00244 case 'R':
00245 appendCriterion(criteria,"PullRetryPeriod_ms",optarg);
00246 break;
00247
00248 case 'r':
00249 appendCriterion(criteria,"PullRetryPeriod",optarg);
00250 break;
00251
00252 case 't':
00253 appendCriterionStr(criteria,"FilterId",optarg);
00254 break;
00255
00256 case 'v':
00257 verbose=true;
00258 break;
00259
00260 case 'h':
00261 usage(argc,argv);
00262 exit(0);
00263
00264 default :
00265 usage(argc,argv);
00266 exit(-1);
00267 }
00268 }
00269
00270 needNameService=(needNameService || optind>=argc);
00271
00272
00273
00274
00275 const char* action ="start";
00276 try
00277 {
00278 CORBA::Object_var obj;
00279
00280
00281
00282 CosNaming::NamingContext_var rootContext=CosNaming::NamingContext::_nil();
00283 try {
00284 action="resolve initial reference 'NameService'";
00285 obj=orb->resolve_initial_references("NameService");
00286 rootContext=CosNaming::NamingContext::_narrow(obj);
00287 if(CORBA::is_nil(rootContext))
00288 throw CORBA::OBJECT_NOT_EXIST();
00289 }
00290 catch (CORBA::Exception& ex) {
00291 if(needNameService)
00292 throw;
00293 else
00294 cerr<<"Warning - failed to "<<action<<"."<<endl;
00295 }
00296
00297
00298
00299
00300 if(optind<argc)
00301 {
00302 action="convert URI from command line into object reference";
00303 obj=orb->string_to_object(argv[optind]);
00304 }
00305 else
00306 {
00307 action="find Event Channel Factory in naming service";
00308 obj=rootContext->resolve(str2name(factoryName));
00309 }
00310
00311 action="narrow object reference to event channel factory";
00312 omniEvents::EventChannelFactory_var factory =
00313 omniEvents::EventChannelFactory::_narrow(obj);
00314 if(CORBA::is_nil(factory))
00315 {
00316 cerr << "Failed to narrow Event Channel Factory reference." << endl;
00317 exit(1);
00318 }
00319
00320
00321 action="check factory supports EventChannel object interface";
00322 CosLifeCycle::Key key;
00323 key.length (1);
00324 key[0].id = CORBA::string_dup("EventChannel");
00325 key[0].kind = CORBA::string_dup("object interface");
00326
00327 if(!factory->supports(key))
00328 {
00329 cerr << "Factory does not support Event Channel Interface! [\""
00330 << factoryName << "\"]" << endl;
00331 exit(1);
00332 }
00333
00334
00335
00336 action="create EventChannel object";
00337 CORBA::Object_var channelObj =factory->create_object(key, criteria);
00338 if (CORBA::is_nil(channelObj))
00339 {
00340 cerr << "Channel Factory returned nil reference! [\""
00341 << channelName << "\"]" << endl;
00342 exit(1);
00343 }
00344
00345
00346 CosEventChannelAdmin::EventChannel_var channel =
00347 CosEventChannelAdmin::EventChannel::_narrow(channelObj);
00348 if (CORBA::is_nil(channel))
00349 {
00350 cerr << "Failed to narrow Event Channel! [\""
00351 << channelName << "\"]" << endl;
00352 exit(1);
00353 }
00354
00355
00356 if(verbose)
00357 {
00358 CORBA::String_var sior =orb->object_to_string(channel);
00359 cout<<sior.in()<<endl;
00360 }
00361
00362
00363
00364 if(!CORBA::is_nil(rootContext))
00365 {
00366 CosNaming::Name name =str2name(channelName);
00367 try{
00368 action="register (bind) EventChannel with the naming service";
00369 rootContext->bind(name,channel.in());
00370 }
00371 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00372 action="register (rebind) EventChannel with the naming service";
00373 rootContext->rebind(name,channel.in());
00374 }
00375 }
00376
00377
00378
00379 action="destroy orb";
00380 orb->destroy();
00381
00382
00383
00384 result=0;
00385
00386 }
00387 catch (CosLifeCycle::NoFactory& ex) {
00388 cerr<<"Failed to create Event Channel: NoFactory"
00389 " (interface not supported) "<<endl;
00390 }
00391 catch (CosLifeCycle::CannotMeetCriteria& ex) {
00392 cerr<<"Failed to create Event Channel: CannotMeetCriteria "<<endl;
00393 }
00394 catch (CosLifeCycle::InvalidCriteria& ex) {
00395 cerr<<"Failed to create Event Channel: InvalidCriteria "<<endl;
00396 }
00397 catch (CORBA::COMM_FAILURE& ex) {
00398 cerr<<"System exception, unable to "<<action<<": COMM_FAILURE"<<endl;
00399 }
00400 catch (CORBA::SystemException& ex) {
00401 cerr<<"System exception, unable to "<<action;
00402 #if defined(HAVE_OMNIORB4)
00403 cerr<<" "<<ex._name();
00404 if(ex.NP_minorString())
00405 cerr<<" ("<<ex.NP_minorString()<<")";
00406 #endif
00407 cerr<<endl;
00408 }
00409 catch (CORBA::Exception& ex) {
00410 cerr<<"CORBA exception, unable to "<<action
00411 #ifdef HAVE_OMNIORB4
00412 <<": "<<ex._name()
00413 #endif
00414 << endl;
00415 }
00416 catch (omniORB::fatalException& ex) {
00417 cerr<<"Fatal Exception, unable to "<<action<<endl;
00418 }
00419
00420 return result;
00421 }
00422
00423 static void
00424 usage(int argc, char **argv)
00425 {
00426 cerr<<
00427 "\nCreate an EventChannel and register it in the naming service.\n"
00428 "syntax: "<<(argc?argv[0]:"eventc")<<" OPTIONS [FACTORY_URI]\n"
00429 "\n"
00430 "FACTORY_URI: The factory may be specified as a URI.\n"
00431 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
00432 " For example: corbaloc::localhost:11169/omniEvents\n"
00433 "\n"
00434 "OPTIONS: DEFAULT:\n"
00435 " -n channel name [\"EventChannel\"]\n"
00436 " -N factory name (if URI is not specified) [\"EventChannelFactory\"]\n"
00437 " -c override default CyclePeriod_ns of new channel (nanoseconds)\n"
00438 " -i set the InsName of new channel, to enable access via corbaloc\n"
00439 " -p override default MaxNumProxies of new channel\n"
00440 " -q override default MaxQueueLength of new channel\n"
00441 " -R override default PullRetryPeriod_ms for new channel (milliseconds)\n"
00442 " -t set an event type filter, FilterId=<RepositoryId>\n"
00443 " -v print the IOR of the new EventChannel to standard output.\n"
00444 " -h display this help text\n"
00445 "OLD OPTIONS: (only used by omniEvents v2.4 and earlier)\n"
00446 " -m override default MaxEventsPerConsumer for new channel\n" << endl;
00447 }
00448
00449 static void appendCriterion(
00450 CosLifeCycle::Criteria& criteria,
00451 const char* name,
00452 const char* value
00453 )
00454 {
00455 CORBA::ULong criteriaLen =criteria.length();
00456 ++criteriaLen;
00457 criteria.length(criteriaLen);
00458 criteria[criteriaLen-1].name=CORBA::string_dup(name);
00459 criteria[criteriaLen-1].value<<=CORBA::ULong(atol(value));
00460 }
00461
00462 static void appendCriterionStr(
00463 CosLifeCycle::Criteria& criteria,
00464 const char* name,
00465 const char* value
00466 )
00467 {
00468 CORBA::ULong criteriaLen =criteria.length();
00469 ++criteriaLen;
00470 criteria.length(criteriaLen);
00471 criteria[criteriaLen-1].name=CORBA::string_dup(name);
00472 criteria[criteriaLen-1].value<<=value;
00473 }