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

eventc.cc

Go to the documentation of this file.
00001 // -*- Mode: C++; -*-
00002 //                            Package   : omniEvents
00003 // eventc.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 //    Client to the event channel factory. Requests creation of an event
00026 //    channel and registers it with the Naming service.
00027 //      
00028 
00029 /*
00030   $Log: eventc.cc,v $
00031   Revision 1.3  2003/11/03 22:21:21  alextingle
00032   Removed all platform specific switches. Now uses autoconf, config.h.
00033   Removed stub header in order to allow makefile dependency checking to work
00034   correctly.
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:25  shamus13
00044   Import of OmniEvents source tree from release 2.1.1
00045 
00046   Revision 1.6  2000/09/05 01:05:38  naderp
00047   Added MaxQueueLength QOS.
00048 
00049   Revision 1.5  2000/08/30 04:39:20  naderp
00050   Port to omniORB 3.0.1.
00051 
00052   Revision 1.4  2000/03/16 05:34:30  naderp
00053   Added stdlib.h for solaris getopt()
00054 
00055   Revision 1.3  2000/03/16 02:44:13  naderp
00056   Added iostream and signal headers.
00057 
00058   Revision 1.2  2000/03/06 13:23:50  naderp
00059   Using util getRootNamingContext function.
00060   Using stub headers.
00061 
00062   Revision 1.1  1999/11/01 20:37:42  naderp
00063   Updated usage statement.
00064 
00065 Revision 1.0  99/11/01  17:05:13  17:05:13  naderp (Paul Nader)
00066 omniEvents 2.0.
00067 Added -m switch to support MaxEventsPerConsumer criteria.
00068 
00069 Revision 0.6  99/08/27  11:48:22  11:48:22  naderp (Paul Nader)
00070 Partitioned EventChannelFactory_i from CosEvent_i.
00071 
00072 Revision 0.5  99/05/10  11:27:50  11:27:50  naderp (Paul Nader)
00073 Initialised rootContext.
00074 
00075 Revision 0.4  99/04/23  16:02:22  16:02:22  naderp (Paul Nader)
00076 gcc port.
00077 
00078 Revision 0.3  99/04/23  09:32:58  09:32:58  naderp (Paul Nader)
00079 Windows Port.
00080 
00081 Revision 0.2  99/04/21  18:06:23  18:06:23  naderp (Paul Nader)
00082 *** empty log message ***
00083 
00084 Revision 0.1.1.1  98/11/27  17:01:51  17:01:51  naderp (Paul Nader)
00085 Enclosed supports call in try block to avoid core dump.
00086 Added information messages for exceptions.
00087 
00088 Revision 0.1  98/11/25  14:07:22  14:07:22  naderp (Paul Nader)
00089 Initial Revision
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 "CosEventChannelAdmin.hh"
00129 #include "EventChannelAdmin.hh"
00130 
00131 static void usage();
00132 
00133 int
00134 main(int argc, char **argv)
00135 {
00136   int result =1;
00137 
00138   //
00139   // Start orb and boa.
00140 #if defined(HAVE_OMNIORB4)
00141   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00142   CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB4_BOA");
00143 #else
00144   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB3");
00145   CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB3_BOA");
00146 #endif
00147 
00148   // Process Options
00149   int c;
00150   const char*   channelName          ="EventChannel";
00151   const char*   channelKind          ="EventChannel";
00152   const char*   factoryName          ="EventChannelFactory";
00153   const char*   factoryKind          ="EventChannelFactory";
00154   CORBA::ULong  pullRetryPeriod      =1;
00155   CORBA::ULong  maxQueueLength       =0;
00156   CORBA::ULong  maxEventsPerConsumer =0;
00157 
00158   while ((c = getopt(argc,argv,"n:k:N:K:r:m:q:h")) != EOF)
00159   {
00160      switch (c)
00161      {
00162         case 'n': channelName = optarg;
00163                   break;
00164 
00165         case 'k': channelKind = optarg;
00166                   break;
00167 
00168         case 'N': factoryName = optarg;
00169                   break;
00170 
00171         case 'K': factoryKind = optarg;
00172                   break;
00173 
00174         case 'r': pullRetryPeriod = atol(optarg);
00175                   break;
00176 
00177         case 'm': maxEventsPerConsumer = atol(optarg);
00178                   break;
00179 
00180         case 'q': maxQueueLength = atol(optarg);
00181                   break;
00182 
00183         case 'h':
00184         default : usage();
00185                   exit(-1);
00186                   break;
00187      }
00188   }
00189 
00190   //
00191   // Use one big try...catch block.
00192   // 'action' variable keeps track of what we're doing.
00193   const char* action ="start";
00194   try
00195   {
00196 
00197     //
00198     // Get Name Service root context.
00199     action="get NameService initial reference";
00200     CosNaming::NamingContext_ptr rootContext;
00201     rootContext = getRootNamingContext(orb);
00202 
00203     //
00204     // Obtain reference to the Event Channel Factory implementation.
00205     action="find Event Channel Factory in naming service";
00206     CosNaming::Name name;
00207     name.length (1);
00208     name[0].id   = CORBA::string_dup(factoryName);
00209     name[0].kind = CORBA::string_dup(factoryKind);
00210 
00211     CORBA::Object_var obj = rootContext->resolve(name);
00212     EventChannelAdmin::EventChannelFactory_var factory =
00213       EventChannelAdmin::EventChannelFactory::_narrow(obj);
00214     if(CORBA::is_nil(factory))
00215     {
00216        cerr << "Failed to narrow Event Channel Factory reference." << endl;
00217        exit(1);
00218     }
00219 
00220     // Check that the factory is of the right type
00221     action="check factory supports EventChannel object interface";
00222     CosLifeCycle::Key key;
00223     key.length (1);
00224     key[0].id = CORBA::string_dup("EventChannel");
00225     key[0].kind = CORBA::string_dup("object interface");
00226 
00227     if(!factory->supports(key))
00228     {
00229       cerr << "Factory does not support Event Channel Interface ! [\""
00230            << factoryName << "\", \"" << factoryKind << "\"]"
00231            << endl;
00232       exit (1);
00233     }
00234 
00235     //
00236     // Create Event Channel Object.
00237     action="create EventChannel object";
00238 
00239     // Construct criteria
00240     CosLifeCycle::Criteria criteria;
00241     criteria.length(3);
00242     criteria[0].name = CORBA::string_dup ("PullRetryPeriod");
00243     criteria[0].value <<= (CORBA::ULong) pullRetryPeriod;
00244     criteria[1].name = CORBA::string_dup ("MaxEventsPerConsumer");
00245     criteria[1].value <<= (CORBA::ULong) maxEventsPerConsumer;
00246     criteria[2].name = CORBA::string_dup ("MaxQueueLength");
00247     criteria[2].value <<= (CORBA::ULong) maxQueueLength;
00248 
00249     CORBA::Object_var channelObj =factory->create_object(key, criteria);
00250     if (CORBA::is_nil(channelObj))
00251     {
00252        cerr << "Channel Factory returned nil reference! [\""
00253             << channelName << "\", \"" << channelKind << "\"]" << endl;
00254        exit(1);
00255     }
00256 
00257     // Narrow object returned to an Event Channel
00258     CosEventChannelAdmin::EventChannel_var channel =
00259       CosEventChannelAdmin::EventChannel::_narrow(channelObj);
00260     if (CORBA::is_nil(channel))
00261     {
00262        cerr << "Failed to narrow Event Channel ! [\""
00263             << channelName << "\", \"" << channelKind << "\"]"
00264             << endl;
00265        exit(1);
00266     }
00267 
00268     //
00269     // Register event channel with naming service
00270     name.length(1);
00271     name[0].id   = CORBA::string_dup (channelName);
00272     name[0].kind = CORBA::string_dup (channelKind);
00273 
00274     try{
00275       action="register (bind) EventChannel with the naming service";
00276       rootContext->bind(name,channel.in());
00277     }
00278     catch(CosNaming::NamingContext::AlreadyBound& ex) {
00279       action="register (rebind) EventChannel with the naming service";
00280       rootContext->rebind(name,channel.in());
00281     }
00282     
00283     //
00284     // If we get here, then everything has worked OK.
00285     result=0;
00286 
00287   }
00288   catch (CosLifeCycle::NoFactory& ex) /* create_object() */ {
00289      cerr<<"Failed to create Event Channel: NoFactory"
00290        " (interface not supported) "<<endl;
00291   }
00292   catch (CosLifeCycle::CannotMeetCriteria& ex) /* create_object() */ {
00293      cerr<<"Failed to create Event Channel: CannotMeetCriteria "<<endl;
00294   }
00295   catch (CosLifeCycle::InvalidCriteria& ex) /* create_object() */ {
00296      cerr<<"Failed to create Event Channel: InvalidCriteria "<<endl;
00297   }
00298   catch (CORBA::COMM_FAILURE& ex) {
00299      cerr<<"System exception, unable to "<<action<<": COMM_FAILURE"<<endl;
00300   }
00301   catch (CORBA::SystemException& ex) {
00302      cerr<<"System exception, unable to "<<action
00303 #ifdef HAVE_OMNIORB4
00304          <<": "<<ex._name()<<" ("<<ex.NP_minorString()<<")"
00305 #endif
00306          << endl;
00307   }
00308   catch (CORBA::Exception& ex) {
00309      cerr<<"CORBA exception, unable to "<<action
00310 #ifdef HAVE_OMNIORB4
00311          <<": "<<ex._name()
00312 #endif
00313          << endl;
00314   }
00315   catch (omniORB::fatalException& ex) {
00316      cerr<<"Fatal Exception, unable to "<<action<<endl;
00317   }
00318 
00319   return result;
00320 }
00321 
00322 static void
00323 usage()
00324 {
00325    cerr << "\nusage: eventc [-n name] [-k kind] [-N name] [-K kind] [-r sec] [-m events] [-q events] [-h]\n" << endl;
00326    cerr << "         -n name specify channel name [\"EventChannel\"]" << endl;
00327    cerr << "         -k kind specify channel kind [\"EventChannel\"]" << endl;
00328    cerr << "         -N name specify factory name [\"EventChannelFactory\"]" << endl;
00329    cerr << "         -K kind specify factory kind [\"EventChannelFactory\"]" << endl;
00330    cerr << "         -r Pull retry Period (in seconds) [1 sec]" << endl;
00331    cerr << "         -m Maximum Number of Events queued per Consumer [0 = unlimited]" << endl;
00332    cerr << "         -q Maximum Number of Events queued by the channel [0 = unlimited]" << endl;
00333    cerr << "         -h     display usage" << endl;
00334    cerr << endl;
00335 }
00336 

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