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.4  2004/10/08 09:06:19  alextingle
00032   More robust exception minor code handling.
00033 
00034   Revision 1.3  2004/08/06 16:16:40  alextingle
00035   Simplified call to ORB_init().
00036 
00037   Revision 1.2  2004/08/04 21:52:52  alextingle
00038   'n' & 'N' options now take a full name path. No more 'k' or 'K' options.
00039 
00040   Revision 1.1  2004/05/31 10:29:37  alextingle
00041   New 'tools' directory. Contains useful command line tools that previously lived in the 'examples' directory.
00042 
00043   Revision 1.14  2004/05/28 10:38:23  alextingle
00044   Now uses new omniEvents.idl header. Properly this time!
00045 
00046   Revision 1.13  2004/05/28 10:16:54  alextingle
00047   Now uses new omniEvents.idl header.
00048 
00049   Revision 1.12  2004/04/30 17:48:02  alextingle
00050   New 'real time push' feature: -t option.
00051 
00052   Revision 1.11  2004/04/21 10:23:46  alextingle
00053   If there is no Naming Service, eventc only issues a warning unless it is needed to find the factory, or options -n or -k are set.
00054 
00055   Revision 1.10  2004/04/20 16:51:59  alextingle
00056   All examples updated for latest version on omniEvents. Server may now be
00057   specified as a 'corbaloc' string or IOR, instead of as naming service id/kind.
00058 
00059   Revision 1.9  2004/03/28 00:58:05  alextingle
00060   New options. -c sets CyclePeriod_ns. -i sets the Channel's InsName.
00061 
00062   Revision 1.8  2004/03/26 16:06:30  alextingle
00063   Added verbose (-v) option that prints the new channel's IOR to standard out.
00064 
00065   Revision 1.7  2004/02/20 17:41:40  alextingle
00066   Moved 'endl;' to the actual end!!
00067 
00068   Revision 1.6  2004/02/20 14:01:54  alextingle
00069   New param: -p sets MaxNumProxies for omniEvents 2.5+.
00070   No longer sends parameters that are not explicitly set on
00071   the command line. This leaves the server to decide upon
00072   default values.
00073 
00074   Revision 1.5  2004/02/04 22:29:55  alextingle
00075   Reworked all C++ examples.
00076   Removed catch(...) as it tends to make it harder to see what's going on.
00077   Now uses POA instead of BOA.
00078   Uses omniORB4's Exception name probing.
00079   No longer uses 'naming.h/cc' utility code.
00080 
00081   Revision 1.4  2003/12/21 11:12:01  alextingle
00082   Most exceptions are now caught by a unified catch block.
00083 
00084   Revision 1.3  2003/11/03 22:21:21  alextingle
00085   Removed all platform specific switches. Now uses autoconf, config.h.
00086   Removed stub header in order to allow makefile dependency checking to work
00087   correctly.
00088 
00089   Revision 1.1.1.1.2.1  2002/09/28 22:20:51  shamus13
00090   Added ifdefs to enable omniEvents to compile
00091   with both omniORB3 and omniORB4. If __OMNIORB4__
00092   is defined during compilation, omniORB4 headers
00093   and command line option syntax is used, otherwise
00094   fall back to omniORB3 style.
00095 
00096   Revision 1.1.1.1  2002/09/25 19:00:25  shamus13
00097   Import of OmniEvents source tree from release 2.1.1
00098 
00099   Revision 1.6  2000/09/05 01:05:38  naderp
00100   Added MaxQueueLength QOS.
00101 
00102   Revision 1.5  2000/08/30 04:39:20  naderp
00103   Port to omniORB 3.0.1.
00104 
00105   Revision 1.4  2000/03/16 05:34:30  naderp
00106   Added stdlib.h for solaris getopt()
00107 
00108   Revision 1.3  2000/03/16 02:44:13  naderp
00109   Added iostream and signal headers.
00110 
00111   Revision 1.2  2000/03/06 13:23:50  naderp
00112   Using util getRootNamingContext function.
00113   Using stub headers.
00114 
00115   Revision 1.1  1999/11/01 20:37:42  naderp
00116   Updated usage statement.
00117 
00118 Revision 1.0  99/11/01  17:05:13  17:05:13  naderp (Paul Nader)
00119 omniEvents 2.0.
00120 Added -m switch to support MaxEventsPerConsumer criteria.
00121 
00122 Revision 0.6  99/08/27  11:48:22  11:48:22  naderp (Paul Nader)
00123 Partitioned EventChannelFactory_i from CosEvent_i.
00124 
00125 Revision 0.5  99/05/10  11:27:50  11:27:50  naderp (Paul Nader)
00126 Initialised rootContext.
00127 
00128 Revision 0.4  99/04/23  16:02:22  16:02:22  naderp (Paul Nader)
00129 gcc port.
00130 
00131 Revision 0.3  99/04/23  09:32:58  09:32:58  naderp (Paul Nader)
00132 Windows Port.
00133 
00134 Revision 0.2  99/04/21  18:06:23  18:06:23  naderp (Paul Nader)
00135 *** empty log message ***
00136 
00137 Revision 0.1.1.1  98/11/27  17:01:51  17:01:51  naderp (Paul Nader)
00138 Enclosed supports call in try block to avoid core dump.
00139 Added information messages for exceptions.
00140 
00141 Revision 0.1  98/11/25  14:07:22  14:07:22  naderp (Paul Nader)
00142 Initial Revision
00143 
00144 */
00145 
00146 //
00147 //
00148 
00149 #ifdef HAVE_CONFIG_H
00150 #  include "config.h"
00151 #endif
00152 
00153 #ifdef HAVE_GETOPT
00154 #  include <unistd.h>
00155 extern char* optarg;
00156 extern int optind;
00157 #else
00158 #  include "getopt.h"
00159 #endif
00160 
00161 #ifdef HAVE_IOSTREAM
00162 #  include <iostream>
00163 #else
00164 #  include <iostream.h>
00165 #endif
00166 
00167 #ifdef HAVE_STD_IOSTREAM
00168 using namespace std;
00169 #endif
00170 
00171 #ifdef HAVE_STDLIB_H
00172 #  include <stdlib.h>
00173 #endif
00174 
00175 #ifdef HAVE_SIGNAL_H
00176 #  include <signal.h>
00177 #endif
00178 
00179 #include "omniEvents.hh"
00180 #include "naming.h"
00181 
00182 static void usage(int argc, char **argv);
00183 static void appendCriterion(   CosLifeCycle::Criteria&,const char*,const char*);
00184 static void appendCriterionStr(CosLifeCycle::Criteria&,const char*,const char*);
00185 
00186 int
00187 main(int argc, char **argv)
00188 {
00189   int result =1;
00190 
00191   //
00192   // Start orb.
00193   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv);
00194 
00195   // Process Options
00196   bool verbose =false;
00197   bool needNameService =false;
00198   const char* channelName ="EventChannel";
00199   const char* factoryName ="EventChannelFactory";
00200   CosLifeCycle::Criteria criteria;
00201 
00202   int c;
00203   while ((c = getopt(argc,argv,"n:N:m:c:i:p:q:r:t:vh")) != EOF)
00204   {
00205      switch (c)
00206      {
00207      case 'n':
00208        channelName=optarg;
00209        needNameService=true;       
00210        break;
00211 
00212      case 'N':
00213        factoryName=optarg;
00214        break;
00215 
00216      case 'm': // OLD OPTION
00217        appendCriterion(criteria,"MaxEventsPerConsumer",optarg);
00218        break;
00219 
00220      case 'c':
00221        appendCriterion(criteria,"CyclePeriod_ns",optarg);
00222        break;
00223 
00224      case 'i':
00225        appendCriterionStr(criteria,"InsName",optarg);
00226        break;
00227 
00228      case 'p':
00229        appendCriterion(criteria,"MaxNumProxies",optarg);
00230        break;
00231 
00232      case 'q':
00233        appendCriterion(criteria,"MaxQueueLength",optarg);
00234        break;
00235 
00236      case 'r':
00237        appendCriterion(criteria,"PullRetryPeriod",optarg);
00238        break;
00239 
00240      case 't':
00241        appendCriterionStr(criteria,"FilterId",optarg);
00242        break;
00243 
00244      case 'v':
00245        verbose=true;
00246        break;
00247 
00248      case 'h':
00249        usage(argc,argv);
00250        exit(0);
00251 
00252      default :
00253        usage(argc,argv);
00254        exit(-1);
00255      }
00256   }
00257   // Need the naming service to find the factory if there is no URI argument.
00258   needNameService=(needNameService || optind>=argc);
00259 
00260   //
00261   // Use one big try...catch block.
00262   // 'action' variable keeps track of what we're doing.
00263   const char* action ="start";
00264   try
00265   {
00266     CORBA::Object_var obj;
00267 
00268     //
00269     // Get Name Service root context.(we can carry on without it though)
00270     CosNaming::NamingContext_var rootContext=CosNaming::NamingContext::_nil();
00271     try {
00272       action="resolve initial reference 'NameService'";
00273       obj=orb->resolve_initial_references("NameService");
00274       rootContext=CosNaming::NamingContext::_narrow(obj);
00275       if(CORBA::is_nil(rootContext))
00276           throw CORBA::OBJECT_NOT_EXIST();
00277     }
00278     catch (CORBA::Exception& ex) {
00279        if(needNameService)
00280            throw;
00281        else
00282            cerr<<"Warning - failed to "<<action<<"."<<endl;
00283     }
00284 
00285     //
00286     // Obtain reference to the Event Channel Factory implementation.
00287     // (from command-line argument or from the Naming Service).
00288     if(optind<argc)
00289     {
00290       action="convert URI from command line into object reference";
00291       obj=orb->string_to_object(argv[optind]);
00292     }
00293     else
00294     {
00295       action="find Event Channel Factory in naming service";
00296       obj=rootContext->resolve(str2name(factoryName));
00297     }
00298 
00299     action="narrow object reference to event channel factory";
00300     omniEvents::EventChannelFactory_var factory =
00301       omniEvents::EventChannelFactory::_narrow(obj);
00302     if(CORBA::is_nil(factory))
00303     {
00304        cerr << "Failed to narrow Event Channel Factory reference." << endl;
00305        exit(1);
00306     }
00307 
00308     // Check that the factory is of the right type
00309     action="check factory supports EventChannel object interface";
00310     CosLifeCycle::Key key;
00311     key.length (1);
00312     key[0].id = CORBA::string_dup("EventChannel");
00313     key[0].kind = CORBA::string_dup("object interface");
00314 
00315     if(!factory->supports(key))
00316     {
00317       cerr << "Factory does not support Event Channel Interface! [\""
00318            << factoryName << "\"]" << endl;
00319       exit(1);
00320     }
00321 
00322     //
00323     // Create Event Channel Object.
00324     action="create EventChannel object";
00325     CORBA::Object_var channelObj =factory->create_object(key, criteria);
00326     if (CORBA::is_nil(channelObj))
00327     {
00328        cerr << "Channel Factory returned nil reference! [\""
00329             << channelName << "\"]" << endl;
00330        exit(1);
00331     }
00332 
00333     // Narrow object returned to an Event Channel
00334     CosEventChannelAdmin::EventChannel_var channel =
00335       CosEventChannelAdmin::EventChannel::_narrow(channelObj);
00336     if (CORBA::is_nil(channel))
00337     {
00338        cerr << "Failed to narrow Event Channel! [\""
00339             << channelName << "\"]" << endl;
00340        exit(1);
00341     }
00342     
00343     // Print the new EventChannel's IOR to standard output.
00344     if(verbose)
00345     {
00346       CORBA::String_var sior =orb->object_to_string(channel);
00347       cout<<sior.in()<<endl;
00348     }
00349 
00350     //
00351     // Register event channel with naming service
00352     if(!CORBA::is_nil(rootContext))
00353     {
00354       CosNaming::Name name =str2name(channelName);
00355       try{
00356         action="register (bind) EventChannel with the naming service";
00357         rootContext->bind(name,channel.in());
00358       }
00359       catch(CosNaming::NamingContext::AlreadyBound& ex) {
00360         action="register (rebind) EventChannel with the naming service";
00361         rootContext->rebind(name,channel.in());
00362       }
00363     }
00364     
00365     //
00366     // If we get here, then everything has worked OK.
00367     result=0;
00368 
00369   }
00370   catch (CosLifeCycle::NoFactory& ex) /* create_object() */ {
00371      cerr<<"Failed to create Event Channel: NoFactory"
00372        " (interface not supported) "<<endl;
00373   }
00374   catch (CosLifeCycle::CannotMeetCriteria& ex) /* create_object() */ {
00375      cerr<<"Failed to create Event Channel: CannotMeetCriteria "<<endl;
00376   }
00377   catch (CosLifeCycle::InvalidCriteria& ex) /* create_object() */ {
00378      cerr<<"Failed to create Event Channel: InvalidCriteria "<<endl;
00379   }
00380   catch (CORBA::COMM_FAILURE& ex) {
00381      cerr<<"System exception, unable to "<<action<<": COMM_FAILURE"<<endl;
00382   }
00383   catch (CORBA::SystemException& ex) {
00384      cerr<<"System exception, unable to "<<action;
00385 #if defined(HAVE_OMNIORB4)
00386      cerr<<" "<<ex._name();
00387      if(ex.NP_minorString())
00388          cerr<<" ("<<ex.NP_minorString()<<")";
00389 #endif
00390      cerr<<endl;
00391   }
00392   catch (CORBA::Exception& ex) {
00393      cerr<<"CORBA exception, unable to "<<action
00394 #ifdef HAVE_OMNIORB4
00395          <<": "<<ex._name()
00396 #endif
00397          << endl;
00398   }
00399   catch (omniORB::fatalException& ex) {
00400      cerr<<"Fatal Exception, unable to "<<action<<endl;
00401   }
00402 
00403   return result;
00404 }
00405 
00406 static void
00407 usage(int argc, char **argv)
00408 {
00409   cerr<<
00410 "\nCreate an EventChannel and register it in the naming service.\n"
00411 "syntax: "<<(argc?argv[0]:"eventc")<<" OPTIONS [FACTORY_URI]\n"
00412 "\n"
00413 "FACTORY_URI: The factory may be specified as a URI.\n"
00414 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
00415 " For example: corbaloc::localhost:11169/omniEvents\n"
00416 "\n"
00417 "OPTIONS:                                         DEFAULT:\n"
00418 " -n channel name                                  [\"EventChannel\"]\n"
00419 " -N factory name (if URI is not specified)        [\"EventChannelFactory\"]\n"
00420 " -c override default CyclePeriod_ns of new channel (nanoseconds)\n"
00421 " -i set the InsName of new channel, to enable access via corbaloc\n"
00422 " -p override default MaxNumProxies of new channel\n"
00423 " -q override default MaxQueueLength of new channel\n"
00424 " -r override default PullRetryPeriod for new channel (seconds)\n"
00425 " -t set an event type filter, FilterId=<RepositoryId>\n"
00426 " -v print the IOR of the new EventChannel to standard output.\n"
00427 " -h display this help text\n"
00428 "OLD OPTIONS: (only used by omniEvents v2.4 and earlier)\n"
00429 " -m override default MaxEventsPerConsumer for new channel\n" << endl;
00430 }
00431 
00432 static void appendCriterion(
00433   CosLifeCycle::Criteria& criteria,
00434   const char* name,
00435   const char* value
00436 )
00437 {
00438   CORBA::ULong criteriaLen =criteria.length();
00439   ++criteriaLen;
00440   criteria.length(criteriaLen);
00441   criteria[criteriaLen-1].name=CORBA::string_dup(name);
00442   criteria[criteriaLen-1].value<<=CORBA::ULong(atol(value));
00443 }
00444 
00445 static void appendCriterionStr(
00446   CosLifeCycle::Criteria& criteria,
00447   const char* name,
00448   const char* value
00449 )
00450 {
00451   CORBA::ULong criteriaLen =criteria.length();
00452   ++criteriaLen;
00453   criteria.length(criteriaLen);
00454   criteria[criteriaLen-1].name=CORBA::string_dup(name);
00455   criteria[criteriaLen-1].value<<=value;
00456 }

Generated on Fri Oct 8 15:52:50 2004 for OmniEvents by doxygen1.2.15