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_semaphore connect_cond(0);
00132 static void usage();
00133
00134 class Supplier_i : virtual public CosEventComm::_sk_PullSupplier {
00135 public:
00136 Supplier_i (long disconnect = 0) : i(0), _disconnect(disconnect), l(0) {};
00137 CORBA::Any *pull();
00138 CORBA::Any *try_pull(CORBA::Boolean &has_event);
00139 void disconnect_pull_supplier ();
00140
00141 private:
00142 long i;
00143 long _disconnect;
00144 CORBA::ULong l;
00145 };
00146
00147 void
00148 Supplier_i::disconnect_pull_supplier () {
00149 cout << "Pull Supplier: disconnected by channel." << endl;
00150 }
00151
00152 CORBA::Any *
00153 Supplier_i::pull() {
00154 cout << "Pull Supplier: pull() called. Data : ";
00155 CORBA::Any *any = new CORBA::Any();
00156 *any <<= l++;
00157 cout << l-1 << endl;
00158
00159
00160 if ((_disconnect > 0) && (i == _disconnect)) {
00161 i = 0;
00162
00163 connect_cond.post();
00164 }
00165 i++;
00166 return (any);
00167 }
00168
00169 CORBA::Any *
00170 Supplier_i::try_pull(CORBA::Boolean &has_event)
00171 {
00172 cout << "Pull Supplier: pull() called. Data : ";
00173 CORBA::Any *any = new CORBA::Any();
00174 *any <<= l++;
00175 cout << l-1 << endl;
00176 has_event = 1;
00177
00178
00179 if ((_disconnect > 0) && (i == _disconnect)) {
00180 i = 0;
00181
00182 connect_cond.post();
00183 }
00184 i++;
00185 return (any);
00186 }
00187
00188
00189 int
00190 main (int argc, char** argv)
00191 {
00192 #if defined(HAVE_OMNIORB4)
00193 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00194 CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB4_BOA");
00195 #else
00196 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "omniORB3");
00197 CORBA::BOA_var boa = orb->BOA_init (argc, argv, "omniORB3_BOA");
00198 #endif
00199
00200
00201 int c;
00202 int discnum = 0;
00203 int sleepInterval = 0;
00204 char *channelName = (char *) "EventChannel";
00205 char *channelKind = (char *) "EventChannel";
00206 while ((c = getopt(argc,argv,"d:s:n:k:h")) != EOF)
00207 {
00208 switch (c)
00209 {
00210 case 'd': discnum = atoi(optarg);
00211 break;
00212
00213 case 's': sleepInterval = atoi(optarg);
00214 break;
00215
00216 case 'n': channelName = optarg;
00217 break;
00218
00219 case 'k': channelKind = optarg;
00220 break;
00221
00222 case 'h':
00223 default : usage();
00224 exit(-1);
00225 break;
00226 }
00227 }
00228
00229 #ifdef HAVE_SIGNAL_H
00230
00231 signal(SIGPIPE, SIG_IGN);
00232 #endif
00233
00234 Supplier_i* supplier = new Supplier_i (discnum);
00235 supplier->_obj_is_ready(boa);
00236
00237
00238 boa->impl_is_ready(0,1);
00239
00240
00241
00242 CosNaming::NamingContext_var rootContext;
00243 rootContext = getRootNamingContext(orb);
00244
00245
00246 CosNaming::Name name;
00247 name.length (1);
00248 name[0].id = CORBA::string_dup (channelName);
00249 name[0].kind = CORBA::string_dup (channelKind);
00250
00251 cout << "Looking for EventChannel" << endl;
00252 CosEventChannelAdmin::EventChannel_var event_channel;
00253 try {
00254 CORBA::Object_var obj = rootContext->resolve (name);
00255 event_channel = CosEventChannelAdmin::EventChannel::_narrow (obj);
00256 if (CORBA::is_nil (event_channel))
00257 {
00258 cerr << "Failed to narrow Event Channel !" << endl;
00259 exit(1);
00260 }
00261 }
00262 catch (CORBA::ORB::InvalidName& ex) {
00263 cerr << "Service required is invalid [does not exist]. !" << endl;
00264 exit(1);
00265 }
00266 catch (CORBA::COMM_FAILURE& ex) {
00267 cerr << "Caught COMM_FAILURE exception."
00268 << "Unable to contact the naming service !"
00269 << endl;
00270 exit(1);
00271 }
00272 catch (omniORB::fatalException& ex) {
00273 cerr << "Caught Fatal Exception !" << endl;
00274 throw;
00275 }
00276 catch (...) {
00277 cerr << "Cannot find event channel ! [\""
00278 << channelName << "\", \"" << channelKind << "\"]"
00279 << endl;
00280 exit(1);
00281 }
00282
00283
00284
00285 CosEventChannelAdmin::SupplierAdmin_var supplier_admin;
00286 while (1)
00287 {
00288 try {
00289 supplier_admin = event_channel->for_suppliers ();
00290 if (CORBA::is_nil(supplier_admin))
00291 {
00292 cerr << "Event Channel returned nil Supplier Admin. !"
00293 << endl;
00294 exit(1);
00295 }
00296 break;
00297 }
00298 catch (CORBA::COMM_FAILURE& ex) {
00299 cerr << "Caught COMM_FAILURE exception "
00300 << "obtaining Supplier Admin !. Retrying... "
00301 << endl;
00302 continue;
00303 }
00304 catch (...) {
00305 cerr << "Unexpected System Exception. "
00306 << "Failed to obtain Supplier Admin !"
00307 << endl;
00308 exit(1);
00309 }
00310 }
00311 cerr << "Obtained SupplierAdmin." << endl;
00312
00313 while (1)
00314 {
00315
00316
00317 CosEventChannelAdmin::ProxyPullConsumer_var proxy_consumer;
00318 while (1)
00319 {
00320 try {
00321 proxy_consumer = supplier_admin->obtain_pull_consumer ();
00322 if (CORBA::is_nil(proxy_consumer))
00323 {
00324 cerr << "Supplier Admin returned nil proxy_consumer !"
00325 << endl;
00326 exit(1);
00327 }
00328 break;
00329 }
00330 catch (CORBA::COMM_FAILURE& ex) {
00331 cerr << "Caught COMM_FAILURE exception "
00332 << "obtaining Pull Consumer !. Retrying... "
00333 << endl;
00334 continue;
00335 }
00336 catch (...) {
00337 cerr << "Unexpected System Exception. "
00338 << "Failed to obtain Proxy Consumer !"
00339 << endl;
00340 exit(1);
00341 }
00342 }
00343 cerr << "Obtained ProxyPullConsumer." << endl;
00344
00345
00346 CosEventComm::PullSupplier::_duplicate(supplier->_this());
00347 while (1)
00348 {
00349 try {
00350 proxy_consumer->connect_pull_supplier(supplier->_this());
00351 break;
00352 }
00353 catch (CORBA::BAD_PARAM& ex) {
00354 cerr << "Caught BAD_PARAM Exception connecting Pull Supplier !"
00355 << endl;
00356 exit (1);
00357 }
00358 catch (CosEventChannelAdmin::AlreadyConnected& ex) {
00359 cerr << "Pull Supplier already connected !"
00360 << endl;
00361 break;
00362 }
00363 catch (CORBA::COMM_FAILURE& ex) {
00364 cerr << "Caught COMM_FAILURE Exception "
00365 << "connecting Pull Supplier !. Retrying..."
00366 << endl;
00367 continue;
00368 }
00369 catch (...) {
00370 cerr << "Unexpected System Exception. "
00371 << "Failed to connect Pull Supplier !"
00372 << endl;
00373 exit (1);
00374 }
00375 }
00376 cerr << "Connected Pull Supplier." << endl;
00377
00378
00379 connect_cond.wait();
00380
00381
00382 while (1)
00383 {
00384 try {
00385 proxy_consumer->disconnect_pull_consumer();
00386 break;
00387 }
00388 catch (CORBA::COMM_FAILURE& ex) {
00389 cerr << "Caught COMM_FAILURE Exception "
00390 << "disconnecting Pull Supplier !. Retrying... "
00391 << endl;
00392 continue;
00393 }
00394 catch (...) {
00395 cerr << "Unexpected System Exception. "
00396 << "Failed to disconnect Pull Suplier!"
00397 << endl;
00398 exit (1);
00399 }
00400 }
00401 cout << "Disconnected Pull Supplier." << endl;
00402
00403
00404 cerr << "Sleeping " << sleepInterval << " Seconds." << endl;
00405 omni_thread::sleep(sleepInterval);
00406 }
00407
00408
00409 return (0);
00410 }
00411
00412 static void
00413 usage()
00414 {
00415 cerr << "\nusage: pullsupp [-d n [-s n]] [-n name] [-k kind] [-h]\n" << endl;
00416 cerr << " -d n disconnect after n pulls" << endl;
00417 cerr << " -s n sleep n Seconds after disconnecting" << endl;
00418 cerr << " -n name specify channel name [\"EventChannel\"]" << endl;
00419 cerr << " -k kind specify channel kind [\"EventChannel\"]" << endl;
00420 cerr << " -h display usage" << endl;
00421 cerr << endl;
00422 }