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

pushsupp.cc

Go to the documentation of this file.
00001 // -*- Mode: C++; -*-
00002 //                            Package   : omniEvents
00003 //   pushsupp.cc              Created on: 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 //    Push Model supplier implementation.
00026 //      
00027 
00028 /*
00029   $Log: pushsupp.cc,v $
00030   Revision 1.3  2003/11/03 22:19:25  alextingle
00031   Removed all platform specific switches. Now uses autoconf, config.h.
00032   Removed stub header in order to allow makefile dependency checking to work
00033   correctly.
00034   Changed int to bool where appropriate.
00035 
00036   Revision 1.1.1.1.2.1  2002/09/28 22:20:51  shamus13
00037   Added ifdefs to enable omniEvents to compile
00038   with both omniORB3 and omniORB4. If __OMNIORB4__
00039   is defined during compilation, omniORB4 headers
00040   and command line option syntax is used, otherwise
00041   fall back to omniORB3 style.
00042 
00043   Revision 1.1.1.1  2002/09/25 19:00:26  shamus13
00044   Import of OmniEvents source tree from release 2.1.1
00045 
00046   Revision 0.14  2000/10/11 01:16:21  naderp
00047   *** empty log message ***
00048 
00049   Revision 0.13  2000/08/30 04:39:48  naderp
00050   Port to omniORB 3.0.1.
00051 
00052   Revision 0.12  2000/03/16 05:37:27  naderp
00053   Added stdlib.h for getopt.
00054 
00055   Revision 0.11  2000/03/06 13:27:10  naderp
00056   Using util getRootNamingContext function.
00057   Using stub headers.
00058   Fixed error messages.
00059 
00060   Revision 0.10  2000/03/02 03:21:20  naderp
00061   Added -r option to connect using nil reference.
00062   Added retry resiliency for handling COMM_FAUILURE exceptions.
00063 
00064 Revision 0.9  99/11/02  13:39:17  13:39:17  naderp (Paul Nader)
00065 Added <signal.h>
00066 
00067   Revision 0.8  1999/11/02 07:57:18  naderp
00068   Updated usage.
00069 
00070 Revision 0.7  99/11/01  19:22:43  19:22:43  naderp (Paul Nader)
00071 Added catch for COMM_FAILURE exception in obtain_push_consumer
00072 and disconnect_push_consumer calls.
00073 
00074 Revision 0.6  99/11/01  16:12:03  16:12:03  naderp (Paul Nader)
00075 omniEvents 2.0 Release.
00076 
00077 Revision 0.5  99/10/27  19:42:31  19:42:31  naderp (Paul Nader)
00078 Ignoring Unix SIGPIPE signal.
00079 Reporting sleep beforhand.
00080 
00081 Revision 0.4  99/04/23  16:05:47  16:05:47  naderp (Paul Nader)
00082 gcc port.
00083 
00084 Revision 0.3  99/04/23  09:34:04  09:34:04  naderp (Paul Nader)
00085 Windows Port.
00086 
00087 Revision 0.2  99/04/21  18:06:26  18:06:26  naderp (Paul Nader)
00088 *** empty log message ***
00089 
00090 Revision 0.1.1.1  98/11/27  16:59:40  16:59:40  naderp (Paul Nader)
00091 Added -s option to sleep after disconnecting.
00092 
00093 Revision 0.1  98/11/25  14:08:25  14:08:25  naderp (Paul Nader)
00094 Initial Revision
00095 
00096 */
00097 
00098 #ifdef HAVE_CONFIG_H
00099 #  include "config.h"
00100 #endif
00101 
00102 #ifdef HAVE_GETOPT
00103 #  include <unistd.h>
00104 extern char* optarg;
00105 extern int optind;
00106 #else
00107 #  include "getopt.h"
00108 #endif
00109 
00110 #ifdef HAVE_IOSTREAM
00111 #  include <iostream>
00112 #else
00113 #  include <iostream.h>
00114 #endif
00115 
00116 #ifdef HAVE_STD_IOSTREAM
00117 using namespace std;
00118 #endif
00119 
00120 #ifdef HAVE_STDLIB_H
00121 #  include <stdlib.h>
00122 #endif
00123 
00124 #ifdef HAVE_SIGNAL_H
00125 #  include <signal.h>
00126 #endif
00127 
00128 #include "naming.h"
00129 
00130 #include "CosEventComm.hh"
00131 #include "CosEventChannelAdmin.hh"
00132 
00133 static void usage();
00134 
00135 class Supplier_i : virtual public CosEventComm::_sk_PushSupplier {
00136 public:
00137   Supplier_i () {};
00138   void disconnect_push_supplier ();
00139 };
00140 
00141 void
00142 Supplier_i::disconnect_push_supplier () {
00143   cout << "EventSupplier: disconnected." << endl;
00144 }
00145 
00146 int main (int argc, char** argv)
00147 {
00148   long l = 0;
00149 #if defined(HAVE_OMNIORB4)
00150   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00151   CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB4_BOA");
00152 #else
00153   CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "omniORB3");
00154   CORBA::BOA_var boa = orb->BOA_init (argc, argv, "omniORB3_BOA");
00155 #endif
00156 
00157   // Process Options
00158   int c;
00159   int discnum = 0;
00160   bool refnil = false;
00161   int sleepInterval = 0;
00162   char *channelName = (char *) "EventChannel";
00163   char *channelKind = (char *) "EventChannel";
00164   while ((c = getopt(argc,argv,"d:rs:n:k:h")) != EOF)
00165   {
00166      switch (c)
00167      {
00168         case 'd': discnum = atoi(optarg);
00169                   break;
00170 
00171         case 'r': refnil = true;
00172                   break;
00173 
00174         case 's': sleepInterval = atoi(optarg);
00175                   break;
00176 
00177         case 'n': channelName = optarg;
00178                   break;
00179 
00180         case 'k': channelKind = optarg;
00181                   break;
00182 
00183         case 'h':
00184         default : usage();
00185                   exit(-1);
00186                   break;
00187      }
00188   }
00189 
00190 #ifdef HAVE_SIGNAL_H
00191   // Ignore broken pipes
00192   signal(SIGPIPE, SIG_IGN);
00193 #endif
00194 
00195   Supplier_i* supplier = new Supplier_i ();
00196   supplier->_obj_is_ready(boa);
00197 
00198   // A Push Supplier can be implemented as a pure client or as a mixed
00199   // client-server process, depending on whether it requires and is
00200   // prepared to service disconnect requests from the channel.
00201   // If its is then let the BOA know we are ready without blocking.
00202   if (! refnil)
00203   {
00204      boa->impl_is_ready(0,1);
00205   }
00206    
00207   CosNaming::NamingContext_var rootContext;
00208   rootContext = getRootNamingContext(orb);
00209 
00210   // Obtain Event Channel Object.
00211   CosNaming::Name name;
00212   name.length (1);
00213   name[0].id = CORBA::string_dup (channelName);
00214   name[0].kind = CORBA::string_dup (channelKind);
00215 
00216   cout << "Looking for EventChannel" << endl;
00217   CosEventChannelAdmin::EventChannel_var event_channel;
00218   try {
00219     CORBA::Object_var obj = rootContext->resolve (name);
00220     event_channel = CosEventChannelAdmin::EventChannel::_narrow (obj);
00221     if (CORBA::is_nil (event_channel))
00222     {
00223        cerr << "Failed to narrow Event Channel !" << endl;
00224        exit(1);
00225     }
00226   }
00227   catch (CORBA::ORB::InvalidName& ex) {
00228      cerr << "Service required is invalid [does not exist]. !" << endl;
00229      exit(1);
00230   }
00231   catch (CORBA::COMM_FAILURE& ex) {
00232      cerr << "Caught COMM_FAILURE exception. "
00233           << "Unable to contact the naming service !"
00234           << endl;
00235      exit(1);
00236   }
00237   catch (omniORB::fatalException& ex) {
00238      cerr << "Caught Fatal Exception !" << endl;
00239      throw;
00240   }
00241   catch (...) {
00242      cerr << "Cannot find event channel ! [\""
00243           << channelName << "\", \"" << channelKind << "\"]"
00244           << endl;
00245      exit(1);
00246   }
00247 
00248   //
00249   // Get Supplier Admin interface - retrying on Comms Failure.
00250   CosEventChannelAdmin::SupplierAdmin_var supplier_admin;
00251   while (1)
00252   {
00253      try {
00254         supplier_admin = event_channel->for_suppliers ();
00255         if (CORBA::is_nil(supplier_admin))
00256         {
00257            cerr << "Event Channel returned nil Supplier Admin !."
00258                 << endl;
00259            exit(1);
00260         }
00261         break;
00262      }
00263      catch (CORBA::COMM_FAILURE& ex) {
00264         cerr << "Caught COMM_FAILURE Exception "
00265              << "obtaining Supplier Admin !. Retrying..."
00266              << endl;
00267         continue;
00268      }
00269      catch (...) {
00270         cerr << "Unexpected System Exeption. "
00271              << "Failed to obtain Supplier Admin !"
00272              << endl;
00273         exit(1);
00274      }
00275   }
00276   cerr << "Obtained SupplierAdmin." << endl;
00277 
00278   while (1)
00279   {
00280      //
00281      // Get proxy consumer - retrying on Comms Failure.
00282      CosEventChannelAdmin::ProxyPushConsumer_var proxy_consumer;
00283      while (1)
00284      {
00285         try {
00286            proxy_consumer = supplier_admin->obtain_push_consumer ();
00287            if (CORBA::is_nil(proxy_consumer))
00288            {
00289               cerr << "Supplier Admin return nil proxy_consumer !."
00290                    << endl;
00291               exit(1);
00292            }
00293            break;
00294         }
00295         catch (CORBA::COMM_FAILURE& ex) {
00296            cerr << "Caught COMM_FAILURE Exception "
00297                 << "obtaining Push Consumer !. Retrying..."
00298                 << endl;
00299            continue;
00300         }
00301         catch (...) {
00302            cerr << "Unexpected System Exeption. "
00303                 << "Failed to obtain Proxy Consumer !"
00304                 << endl;
00305            exit(1);
00306         }
00307      }
00308      cerr << "Obtained ProxyPushConsumer." << endl;
00309    
00310      //
00311      // Connect Push Supplier - retrying on Comms Failure.
00312      CosEventComm::PushSupplier_ptr sptr;
00313      sptr = supplier->_this();
00314      sptr = (! refnil) ? CosEventComm::PushSupplier::_duplicate(sptr)
00315                        : CosEventComm::PushSupplier::_nil();
00316      while (1)
00317      {
00318         try {
00319            proxy_consumer->connect_push_supplier(sptr);
00320            break;
00321         }
00322         catch (CORBA::BAD_PARAM& ex) {
00323            cerr << "Caught BAD_PARAM Exception connecting Push Supplier !"
00324                 << endl;
00325            exit (1);
00326         }
00327         catch (CosEventChannelAdmin::AlreadyConnected& ex) {
00328            cerr << "Pull Supplier already connected !"
00329                 << endl;
00330            break;
00331         }
00332         catch (CORBA::COMM_FAILURE& ex) {
00333            cerr << "Caught COMM_FAILURE Exception "
00334                 << "connecting Push Supplier !. Retrying..."
00335                 << endl;
00336            continue;
00337         }
00338         catch (...) {
00339            cerr << "Unexpected System Exception. "
00340                 << "Failed to connect Push Supplier !"
00341                 << endl;
00342            exit (1);
00343         }
00344      }
00345      cerr << "Connected Push Supplier." << endl;
00346    
00347      // Push data.
00348      for (int i=0; (discnum == 0) || (i < discnum); i++)
00349      {
00350         CORBA::Any any;
00351         any <<= (CORBA::ULong) l++;
00352         try {
00353            cout << "Push Supplier: push() called. ";
00354            proxy_consumer->push(any);
00355            cout << "Data : " << l-1 << endl;
00356         }
00357         catch (...) {
00358            cout << "Failed !" << endl;
00359         }
00360      }
00361    
00362      // Disconnect - retrying on Comms Failure.
00363      while (1)
00364      {
00365         try {
00366            proxy_consumer->disconnect_push_consumer();
00367            break;
00368         }
00369         catch (CORBA::COMM_FAILURE& ex) {
00370            cerr << "Caught COMM_FAILURE Exception "
00371                 << "disconnecting Push Supplier!. Retrying..."
00372                 << endl;
00373            continue;
00374         }
00375         catch (...) {
00376            cerr << "Unexpected System Exception. "
00377                 << "Failed to disconnect Push Supplier!"
00378                 << endl;
00379            exit(1);
00380         }
00381      }
00382      cerr << "ProxyPushConsumer disconnected." << endl;
00383 
00384      // Yawn.
00385      cerr << "Sleeping " << sleepInterval << " Seconds." << endl;
00386      omni_thread::sleep(sleepInterval);
00387   }
00388 
00389   // Not Reached
00390   return 0;
00391 }
00392 
00393 static void
00394 usage()
00395 {
00396    cerr << "\nusage: pushsupp [-d n [-s n]] [-n name] [-k kind] [-h]\n" << endl;
00397    cerr << "         -d n   disconnect after n pushes" << endl;
00398    cerr << "         -r     connect using nil reference" << endl;
00399    cerr << "         -s n   sleep n seconds after disconnecting" << endl;
00400    cerr << "         -n name specify channel name [\"EventChannel\"]" << endl;
00401    cerr << "         -k kind specify channel kind [\"EventChannel\"]" << endl;
00402    cerr << "         -h     display usage" << endl;
00403    cerr << endl;
00404 }

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