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