Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

pullsupp.cc

Go to the documentation of this file.
00001 // -*- Mode: C++; -*-
00002 //                            Package   : omniEvents
00003 //   pullsupp.cc              Created   : 1/4/98
00004 //                            Author    : Paul Nader (pwn)
00005 //
00006 //    Copyright (C) 1998 Paul Nader.
00007 //
00008 //    This file is part of the omniEvents application.
00009 //
00010 //    omniEvents is free software; you can redistribute it and/or
00011 //    modify it under the terms of the GNU Lesser General Public
00012 //    License as published by the Free Software Foundation; either
00013 //    version 2.1 of the License, or (at your option) any later version.
00014 //
00015 //    omniEvents is distributed in the hope that it will be useful,
00016 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 //    Lesser General Public License for more details.
00019 //
00020 //    You should have received a copy of the GNU Lesser General Public
00021 //    License along with this library; if not, write to the Free Software
00022 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // Description:
00025 //    Pull Model supplier implementation.
00026 //      
00027 
00028 /*
00029   $Log: pullsupp.cc,v $
00030   Revision 1.4  2003/11/17 08:47:09  alextingle
00031   Corrected typo.
00032 
00033   Revision 1.2  2003/11/16 23:24:22  alex
00034   Corrected typo.
00035 
00036   Revision 1.1.1.1  2003/11/11 22:28:56  alex
00037   Import of testing 2.3.0
00038 
00039   Revision 1.3  2003/11/03 22:20:26  alextingle
00040   Removed all platform specific switches. Now uses autoconf, config.h.
00041   Removed stub header in order to allow makefile dependency checking to work
00042   correctly.
00043 
00044   Revision 1.1.1.1.2.1  2002/09/28 22:20:51  shamus13
00045   Added ifdefs to enable omniEvents to compile
00046   with both omniORB3 and omniORB4. If __OMNIORB4__
00047   is defined during compilation, omniORB4 headers
00048   and command line option syntax is used, otherwise
00049   fall back to omniORB3 style.
00050 
00051   Revision 1.1.1.1  2002/09/25 19:00:26  shamus13
00052   Import of OmniEvents source tree from release 2.1.1
00053 
00054   Revision 0.11  2000/08/30 04:39:48  naderp
00055   Port to omniORB 3.0.1.
00056 
00057   Revision 0.10  2000/03/16 05:37:27  naderp
00058   Added stdlib.h for getopt.
00059 
00060   Revision 0.9  2000/03/06 13:26:32  naderp
00061   Using util getRootNamingContext function.
00062   Using stub headers.
00063   Fixed error messages.
00064 
00065   Revision 0.8  2000/03/02 03:16:26  naderp
00066   Added retry resiliency for handling COMM_FAUILURE exceptions.
00067   Replaced condition variable by counting semaphore.
00068 
00069   Revision 0.7  1999/11/02 13:39:13  naderp
00070   Added <signal.h>
00071 
00072   Revision 0.6  1999/11/02 07:57:22  naderp
00073   Updated usage.
00074 
00075 Revision 0.5  99/11/01  16:10:12  16:10:12  naderp (Paul Nader)
00076 omniEvents 2.0 Release.
00077 Ignoring SIGPIPE for UNIX platforms.
00078 
00079 Revision 0.4  99/04/23  16:05:44  16:05:44  naderp (Paul Nader)
00080 gcc port.
00081 
00082 Revision 0.3  99/04/23  09:34:01  09:34:01  naderp (Paul Nader)
00083 Windows Port.
00084 
00085 Revision 0.2  99/04/21  18:06:25  18:06:25  naderp (Paul Nader)
00086 *** empty log message ***
00087 
00088 Revision 0.1.1.1  98/11/27  16:59:33  16:59:33  naderp (Paul Nader)
00089 Added -s option to sleep after disconnecting.
00090 
00091 Revision 0.1  98/11/25  14:08:07  14:08:07  naderp (Paul Nader)
00092 Initial Revision
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    // Exersize Disconnect
00160    if ((_disconnect > 0) && (i == _disconnect)) {
00161       i = 0;
00162       // Signal main thread to disconnect and re-connect.
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    // Exersize Disconnect
00179    if ((_disconnect > 0) && (i == _disconnect)) {
00180       i = 0;
00181       // Signal main thread to disconnect and re-connect.
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   // Process Options
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   // Ignore broken pipes
00231   signal(SIGPIPE, SIG_IGN);
00232 #endif
00233 
00234   Supplier_i* supplier = new Supplier_i (discnum);
00235   supplier->_obj_is_ready(boa);
00236 
00237   // Let  the BOA know we are ready without blocking.
00238   boa->impl_is_ready(0,1);
00239 
00240   //
00241   // Obtain and narrow reference to the Name Service.
00242   CosNaming::NamingContext_var rootContext;
00243   rootContext = getRootNamingContext(orb);
00244 
00245   // Obtain Event Channel Object.
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   // Get Supplier Admin interface - retrying on Comms Failure.
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      // Get proxy consumer - retrying on Comms Failure.
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      // Connect Pull Supplier - retrying on Comms Failure.
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      // Wait for indication to disconnect before re-connecting.
00379      connect_cond.wait();
00380 
00381      // Disconnect - retrying on Comms Failure.
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      // Yawn.
00404      cerr << "Sleeping " << sleepInterval << " Seconds." << endl;
00405      omni_thread::sleep(sleepInterval);
00406   }
00407 
00408   // Not Reached
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 }

Generated on Fri Dec 12 10:53:03 2003 for OmniEvents by doxygen1.2.15