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