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 #ifdef HAVE_CONFIG_H
00097 # include "config.h"
00098 #endif
00099
00100 #ifdef HAVE_GETOPT
00101 # include <unistd.h>
00102 extern char* optarg;
00103 extern int optind;
00104 #else
00105 # include "getopt.h"
00106 #endif
00107
00108 #ifdef HAVE_IOSTREAM
00109 # include <iostream>
00110 #else
00111 # include <iostream.h>
00112 #endif
00113
00114 #ifdef HAVE_STD_IOSTREAM
00115 using namespace std;
00116 #endif
00117
00118 #ifdef HAVE_STDLIB_H
00119 # include <stdlib.h>
00120 #endif
00121
00122 #ifdef HAVE_SIGNAL_H
00123 # include <signal.h>
00124 #endif
00125
00126 #include "naming.h"
00127
00128 #include "CosEventComm.hh"
00129 #include "CosEventChannelAdmin.hh"
00130
00131 static omni_mutex mutex;
00132 static omni_condition connect_cond(&mutex);
00133 static void usage();
00134
00135 class Consumer_i : virtual public CosEventComm::_sk_PushConsumer {
00136 public:
00137 Consumer_i (long disconnect = 0) : _disconnect(disconnect) {}
00138
00139 void push (const CORBA::Any& data);
00140 void disconnect_push_consumer ();
00141
00142 private:
00143 long _disconnect;
00144 };
00145
00146 void Consumer_i::push (const CORBA::Any& data) {
00147 CORBA::ULong l;
00148 static int i = 0;
00149
00150 i++;
00151 data >>= l;
00152 cout << "Push Consumer: push () called. Data : "
00153 << l
00154 << endl;
00155
00156
00157 if (i == _disconnect) {
00158 i = 0;
00159
00160
00161
00162
00163
00164 connect_cond.signal();
00165 }
00166 }
00167
00168 void Consumer_i::disconnect_push_consumer () {
00169 cout << "Push Consumer: disconnected." << endl;
00170 }
00171
00172 int
00173 main(int argc, char **argv)
00174 {
00175
00176
00177 #if defined(HAVE_OMNIORB4)
00178 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00179 CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB4_BOA");
00180 #else
00181 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB3");
00182 CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB3_BOA");
00183 #endif
00184
00185
00186 int c;
00187 int discnum = 0;
00188 int sleepInterval = 0;
00189 char *channelName = (char *) "EventChannel";
00190 char *channelKind = (char *) "EventChannel";
00191
00192 while ((c = getopt(argc,argv,"hd:s:n:k:")) != EOF)
00193 {
00194 switch (c)
00195 {
00196 case 'd': discnum = atoi(optarg);
00197 break;
00198
00199 case 's': sleepInterval = atoi(optarg);
00200 break;
00201
00202 case 'n': channelName = optarg;
00203 break;
00204
00205 case 'k': channelKind = optarg;
00206 break;
00207
00208 case 'h':
00209 default : usage();
00210 exit(-1);
00211 break;
00212 }
00213 }
00214
00215 #ifdef HAVE_SIGNAL_H
00216
00217 signal(SIGPIPE, SIG_IGN);
00218 #endif
00219
00220 Consumer_i* consumer = new Consumer_i (discnum);
00221 consumer->_obj_is_ready(boa);
00222
00223
00224 boa->impl_is_ready(0,1);
00225
00226
00227
00228 CosNaming::NamingContext_ptr rootContext;
00229 rootContext = getRootNamingContext(orb);
00230
00231
00232
00233 CosNaming::Name name;
00234 name.length (1);
00235 name[0].id = CORBA::string_dup (channelName);
00236 name[0].kind = CORBA::string_dup (channelKind);
00237
00238 cout << "Looking for EventChannel" << endl;
00239 CosEventChannelAdmin::EventChannel_var channel;
00240 try {
00241 CORBA::Object_var obj = rootContext->resolve(name);
00242 channel = CosEventChannelAdmin::EventChannel::_narrow(obj);
00243 if (CORBA::is_nil(channel))
00244 {
00245 cerr << "Failed to narrow Event Channel reference !" << endl;
00246 exit(1);
00247 }
00248 }
00249 catch (CORBA::COMM_FAILURE& ex) {
00250 cerr << "Caught system exception COMM_FAILURE, unable to contact the "
00251 << "naming service !" << endl;
00252 exit(1);
00253 }
00254 catch (omniORB::fatalException& ex) {
00255 cerr << "Caught Fatal Exception !" << endl;
00256 throw;
00257 }
00258 catch (...) {
00259 cerr << "Cannot find event channel ! [\""
00260 << channelName << "\", \"" << channelKind << "\"]"
00261 << endl;
00262 exit (1);
00263 }
00264
00265
00266
00267 CosEventChannelAdmin::ConsumerAdmin_var consumer_admin;
00268 while (1)
00269 {
00270 try {
00271 consumer_admin = channel->for_consumers ();
00272 if (CORBA::is_nil (consumer_admin))
00273 {
00274 cerr << "Event Channel returned consumer_admin nil reference !"
00275 << endl;
00276 exit (1);
00277 }
00278 break;
00279 }
00280 catch (CORBA::COMM_FAILURE& ex) {
00281 cerr << "Caught COMM_FAILURE exception "
00282 << "obtaining Consumer Admin !. Retrying..."
00283 << endl;
00284 continue;
00285 }
00286 catch (...) {
00287 cerr << "Unexpected System Exception."
00288 << "Failed to obtain ConsumerAdmin !" << endl;
00289 exit (1);
00290 }
00291 }
00292 cerr << "Obtained ConsumerAdmin." << endl;
00293
00294 while (1) {
00295
00296
00297 CosEventChannelAdmin::ProxyPushSupplier_var proxy_supplier;
00298 while (1)
00299 {
00300 try {
00301 proxy_supplier = consumer_admin->obtain_push_supplier ();
00302 if (CORBA::is_nil (proxy_supplier))
00303 {
00304 cerr << "Consumer Admin returned proxy_supplier nil reference !"
00305 << endl;
00306 exit (1);
00307 }
00308 break;
00309 }
00310 catch (CORBA::COMM_FAILURE& ex) {
00311 cerr << "Caught COMM_FAILURE Exception "
00312 << "obtaining Push Supplier !. Retrying..."
00313 << endl;
00314 continue;
00315 }
00316 catch (...) {
00317 cerr << "Unexpected System Exception."
00318 << "Failed to obtain Proxy Supplier !"
00319 << endl;
00320 exit (1);
00321 }
00322 }
00323 cerr << "Obtained ProxyPushSupplier." << endl;
00324
00325
00326
00327 CosEventComm::PushConsumer::_duplicate(consumer->_this());
00328 while (1)
00329 {
00330 try {
00331 proxy_supplier->connect_push_consumer(consumer->_this());
00332 break;
00333 }
00334 catch (CORBA::BAD_PARAM& ex) {
00335 cerr << "Caught BAD_PARAM Exception connecting Push Consumer !"
00336 << endl;
00337 exit (1);
00338 }
00339 catch (CosEventChannelAdmin::AlreadyConnected& ex) {
00340 cerr << "Push Consumer already connected !"
00341 << endl;
00342 break;
00343 }
00344 catch (CORBA::COMM_FAILURE& ex) {
00345 cerr << "Caught COMM_FAILURE exception "
00346 << "connecting push consumer !. Retrying..."
00347 << endl;
00348 continue;
00349 }
00350 catch (...) {
00351 cerr << "Unexpected System Exception. "
00352 << "Failed to connect Push Consumer !"
00353 << endl;
00354 exit (1);
00355 }
00356 }
00357 cerr << "Connected Push Consumer." << endl;
00358
00359
00360 {
00361 omni_mutex_lock condition_lock(mutex);
00362 connect_cond.wait();
00363 }
00364
00365
00366 while (1)
00367 {
00368 try {
00369 proxy_supplier->disconnect_push_supplier();
00370 break;
00371 }
00372 catch (CORBA::COMM_FAILURE& ex) {
00373 cerr << "Caught COMM_FAILURE Exception "
00374 << "disconnecting Push Consumer !. Retrying..."
00375 << endl;
00376 continue;
00377 }
00378 catch (...) {
00379 cerr << "Unexpected System Exception. "
00380 << "Failed to disconnect Push Consumer!."
00381 << endl;
00382 exit (1);
00383 }
00384 }
00385 cerr << "Disconnected Push Consumer." << endl;
00386
00387
00388 cerr << "Sleeping " << sleepInterval << " Seconds." << endl;
00389 omni_thread::sleep(sleepInterval);
00390 }
00391
00392
00393
00394 channel->destroy();
00395 cerr << "Destroyed channel !" << endl;
00396
00397 return 0;
00398 }
00399
00400 static void
00401 usage()
00402 {
00403 cerr << "\nusage: pushcons [-d n [-s n]] [-n name] [-k kind] [-h]\n" << endl;
00404 cerr << " -d n disconnect after n pushes" << endl;
00405 cerr << " -s n sleep n seconds after disconnecting" << endl;
00406 cerr << " -n name specify channel name [\"EventChannel\"]" << endl;
00407 cerr << " -k kind specify channel kind [\"EventChannel\"]" << endl;
00408 cerr << " -h display usage" << endl;
00409 cerr << endl;
00410 }