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 #ifdef HAVE_CONFIG_H
00126 # include "config.h"
00127 #endif
00128
00129 #ifdef HAVE_GETOPT
00130 # include <unistd.h>
00131 extern char* optarg;
00132 extern int optind;
00133 #else
00134 # include "getopt.h"
00135 #endif
00136
00137 #ifdef HAVE_IOSTREAM
00138 # include <iostream>
00139 #else
00140 # include <iostream.h>
00141 #endif
00142
00143 #ifdef HAVE_STD_IOSTREAM
00144 using namespace std;
00145 #endif
00146
00147 #ifdef HAVE_STDLIB_H
00148 # include <stdlib.h>
00149 #endif
00150
00151 #ifdef HAVE_SIGNAL_H
00152 # include <signal.h>
00153 #endif
00154
00155 #include "CosEventComm.hh"
00156 #include "CosEventChannelAdmin.hh"
00157 #include "naming.h"
00158
00159 static void usage(int argc, char **argv);
00160
00161 class Supplier_i : virtual public POA_CosEventComm::PushSupplier {
00162 public:
00163 Supplier_i () {};
00164 void disconnect_push_supplier ();
00165 };
00166
00167 void
00168 Supplier_i::disconnect_push_supplier () {
00169 cout << "Push Supplier: disconnected." << endl;
00170 }
00171
00172 int main (int argc, char** argv)
00173 {
00174 long l = 0;
00175 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv);
00176
00177
00178 int discnum =0;
00179 bool refnil =false;
00180 int sleepInterval =0;
00181 const char* channelName ="EventChannel";
00182
00183 int c;
00184 while ((c = getopt(argc,argv,"d:rs:n:h")) != EOF)
00185 {
00186 switch (c)
00187 {
00188 case 'd': discnum = atoi(optarg);
00189 break;
00190
00191 case 'r': refnil = true;
00192 break;
00193
00194 case 's': sleepInterval = atoi(optarg);
00195 break;
00196
00197 case 'n': channelName = optarg;
00198 break;
00199
00200 case 'h':
00201 default : usage(argc,argv);
00202 exit(-1);
00203 break;
00204 }
00205 }
00206
00207 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
00208
00209 signal(SIGPIPE, SIG_IGN);
00210 #endif
00211
00212 Supplier_i* supplier = NULL;
00213 CosEventChannelAdmin::EventChannel_var channel;
00214
00215 const char* action="";
00216 try {
00217 CORBA::Object_var obj;
00218
00219
00220
00221
00222
00223 if(!refnil)
00224 {
00225 supplier=new Supplier_i();
00226
00227 action="resolve initial reference 'RootPOA'";
00228 obj=orb->resolve_initial_references("RootPOA");
00229 PortableServer::POA_var rootPoa =PortableServer::POA::_narrow(obj);
00230 if(CORBA::is_nil(rootPoa))
00231 throw CORBA::OBJECT_NOT_EXIST();
00232
00233 action="activate the RootPOA's POAManager";
00234 PortableServer::POAManager_var pman =rootPoa->the_POAManager();
00235 pman->activate();
00236 }
00237
00238
00239
00240
00241 if(optind<argc)
00242 {
00243 action="convert URI from command line into object reference";
00244 obj=orb->string_to_object(argv[optind]);
00245 }
00246 else
00247 {
00248 action="resolve initial reference 'NameService'";
00249 obj=orb->resolve_initial_references("NameService");
00250 CosNaming::NamingContext_var rootContext=
00251 CosNaming::NamingContext::_narrow(obj);
00252 if(CORBA::is_nil(rootContext))
00253 throw CORBA::OBJECT_NOT_EXIST();
00254
00255 action="find EventChannel in NameService";
00256 cout << action << endl;
00257 obj=rootContext->resolve(str2name(channelName));
00258 }
00259
00260 action="narrow object reference to event channel";
00261 channel=CosEventChannelAdmin::EventChannel::_narrow(obj);
00262 if(CORBA::is_nil(channel))
00263 {
00264 cerr << "Failed to narrow Event Channel reference." << endl;
00265 exit(1);
00266 }
00267
00268 }
00269 catch(CORBA::ORB::InvalidName& ex) {
00270 cerr<<"Failed to "<<action<<". ORB::InvalidName"<<endl;
00271 exit(1);
00272 }
00273 catch(CosNaming::NamingContext::InvalidName& ex) {
00274 cerr<<"Failed to "<<action<<". NamingContext::InvalidName"<<endl;
00275 exit(1);
00276 }
00277 catch(CosNaming::NamingContext::NotFound& ex) {
00278 cerr<<"Failed to "<<action<<". NamingContext::NotFound"<<endl;
00279 exit(1);
00280 }
00281 catch(CosNaming::NamingContext::CannotProceed& ex) {
00282 cerr<<"Failed to "<<action<<". NamingContext::CannotProceed"<<endl;
00283 exit(1);
00284 }
00285 catch(CORBA::TRANSIENT& ex) {
00286 cerr<<"Failed to "<<action<<". TRANSIENT"<<endl;
00287 exit(1);
00288 }
00289 catch(CORBA::OBJECT_NOT_EXIST& ex) {
00290 cerr<<"Failed to "<<action<<". OBJECT_NOT_EXIST"<<endl;
00291 exit(1);
00292 }
00293 catch(CORBA::SystemException& ex) {
00294 cerr<<"Failed to "<<action<<".";
00295 #if defined(HAVE_OMNIORB4)
00296 cerr<<" "<<ex._name();
00297 if(ex.NP_minorString())
00298 cerr<<" ("<<ex.NP_minorString()<<")";
00299 #endif
00300 cerr<<endl;
00301 exit(1);
00302 }
00303 catch(CORBA::Exception& ex) {
00304 cerr<<"Failed to "<<action<<"."
00305 #if defined(HAVE_OMNIORB4)
00306 " "<<ex._name()
00307 #endif
00308 <<endl;
00309 exit(1);
00310 }
00311
00312
00313
00314 CosEventChannelAdmin::SupplierAdmin_var supplier_admin;
00315 while (1)
00316 {
00317 try {
00318 supplier_admin = channel->for_suppliers ();
00319 if (CORBA::is_nil(supplier_admin))
00320 {
00321 cerr << "Event Channel returned nil Supplier Admin!"
00322 << endl;
00323 exit(1);
00324 }
00325 break;
00326 }
00327 catch (CORBA::COMM_FAILURE& ex) {
00328 cerr << "Caught COMM_FAILURE Exception "
00329 << "obtaining Supplier Admin! Retrying..."
00330 << endl;
00331 continue;
00332 }
00333 }
00334 cout << "Obtained SupplierAdmin." << endl;
00335
00336 while (1)
00337 {
00338
00339
00340 CosEventChannelAdmin::ProxyPushConsumer_var proxy_consumer;
00341 while (1)
00342 {
00343 try {
00344 proxy_consumer = supplier_admin->obtain_push_consumer ();
00345 if (CORBA::is_nil(proxy_consumer))
00346 {
00347 cerr << "Supplier Admin returned nil proxy_consumer!"<< endl;
00348 exit(1);
00349 }
00350 break;
00351 }
00352 catch (CORBA::COMM_FAILURE& ex) {
00353 cerr << "Caught COMM_FAILURE Exception "
00354 << "obtaining Proxy Push Consumer! Retrying..."
00355 << endl;
00356 continue;
00357 }
00358 }
00359 cout << "Obtained ProxyPushConsumer." << endl;
00360
00361
00362
00363 CosEventComm::PushSupplier_var sptr =CosEventComm::PushSupplier::_nil();
00364 if (! refnil) {
00365 sptr = supplier->_this();
00366 }
00367
00368 while (1)
00369 {
00370 try {
00371 proxy_consumer->connect_push_supplier(sptr.in());
00372 break;
00373 }
00374 catch (CORBA::BAD_PARAM& ex) {
00375 cerr << "Caught BAD_PARAM Exception connecting Push Supplier!"
00376 << endl;
00377 exit (1);
00378 }
00379 catch (CosEventChannelAdmin::AlreadyConnected& ex) {
00380 cerr << "Proxy Push Consumer already connected!"
00381 << endl;
00382 break;
00383 }
00384 catch (CORBA::COMM_FAILURE& ex) {
00385 cerr << "Caught COMM_FAILURE Exception "
00386 << "connecting Push Supplier! Retrying..."
00387 << endl;
00388 continue;
00389 }
00390 }
00391 cout << "Connected Push Supplier." << endl;
00392
00393
00394 for (int i=0; (discnum == 0) || (i < discnum); i++)
00395 {
00396 CORBA::Any any;
00397 any <<= (CORBA::ULong) l++;
00398 try {
00399 cout << "Push Supplier: push() called. " << flush;
00400 proxy_consumer->push(any);
00401 cout << "Data : " << l-1 << endl;
00402 }
00403 catch(CosEventComm::Disconnected&) {
00404 cout << "Failed. Caught Disconnected Exception!" << endl;
00405 }
00406 catch(CORBA::COMM_FAILURE&) {
00407 cout << "Failed. Caught COMM_FAILURE Exception!" << endl;
00408 }
00409 }
00410
00411
00412 while (1)
00413 {
00414 try {
00415 proxy_consumer->disconnect_push_consumer();
00416 break;
00417 }
00418 catch (CORBA::COMM_FAILURE& ex) {
00419 cerr << "Caught COMM_FAILURE Exception "
00420 << "disconnecting Push Supplier! Retrying..."
00421 << endl;
00422 continue;
00423 }
00424 }
00425 cout << "ProxyPushConsumer disconnected." << endl;
00426
00427
00428 cout << "Sleeping " << sleepInterval << " seconds." << endl;
00429 omni_thread::sleep(sleepInterval);
00430 }
00431
00432
00433 return 0;
00434 }
00435
00436 static void
00437 usage(int argc, char **argv)
00438 {
00439 cerr<<
00440 "\nCreate a PushSupplier to send events to a channel.\n"
00441 "syntax: "<<(argc?argv[0]:"pushsupp")<<" OPTIONS [CHANNEL_URI]\n"
00442 "\n"
00443 "CHANNEL_URI: The event channel may be specified as a URI.\n"
00444 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
00445 "\n"
00446 "OPTIONS: DEFAULT:\n"
00447 " -d NUM disconnect after sending NUM events [0 - never disconnect]\n"
00448 " -r connect using a nil reference\n"
00449 " -s SECS sleep SECS seconds after disconnecting [0]\n"
00450 " -n NAME channel name (if URI is not specified) [\"EventChannel\"]\n"
00451 " -h display this help text\n" << endl;
00452 }