OmniEvents

rmeventc.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 // rmeventc.cc                Created   : 2003/12/21
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2003 Alex Tingle
00006 //
00007 //    This file is part of the omniEvents application.
00008 //
00009 //    omniEvents is free software; you can redistribute it and/or
00010 //    modify it under the terms of the GNU Lesser General Public
00011 //    License as published by the Free Software Foundation; either
00012 //    version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //    omniEvents is distributed in the hope that it will be useful,
00015 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //    Lesser General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU Lesser General Public
00020 //    License along with this library; if not, write to the Free Software
00021 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // Description:
00024 //    Destroys the named EventChannel.
00025 //      
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include "config.h"
00029 #endif
00030 
00031 #ifdef HAVE_GETOPT
00032 #  include <unistd.h>
00033 extern char* optarg;
00034 extern int optind;
00035 #else
00036 #  include "getopt.h"
00037 #endif
00038 
00039 #ifdef HAVE_STDLIB_H
00040 #  include <stdlib.h> // exit()
00041 #endif
00042 
00043 #ifdef HAVE_IOSTREAM
00044 #  include <iostream>
00045 #else
00046 #  include <iostream.h>
00047 #endif
00048 
00049 #include <cstdio>
00050 
00051 #ifdef HAVE_STD_IOSTREAM
00052 using namespace std;
00053 #endif
00054 
00055 #include "CosEventChannelAdmin.hh"
00056 #include "naming.h"
00057 
00058 static void usage(int argc, char **argv);
00059 
00060 int
00061 main(int argc, char **argv)
00062 {
00063   int result =1;
00064 
00065   //
00066   // Start orb.
00067 #if defined(HAVE_OMNIORB4)
00068   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00069 #else
00070   CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB3");
00071 #endif
00072 
00073   // Process Options
00074   int c;
00075 
00076   CosNaming::Name ecName =str2name("EventChannel");
00077 
00078   while ((c = getopt(argc,argv,"n:h")) != EOF)
00079   {
00080      switch (c)
00081      {
00082         case 'n': ecName=str2name(optarg);
00083                   break;
00084 
00085         case 'h': usage(argc,argv);
00086                   exit(0);
00087 
00088         default : usage(argc,argv);
00089                   exit(-1);
00090      }
00091   }
00092 
00093   //
00094   // Use one big try...catch block.
00095   // 'action' variable keeps track of what we're doing.
00096   const char* action ="start";
00097   try
00098   {
00099     CORBA::Object_var obj;
00100     
00101     //
00102     // Obtain object reference to EventChannel
00103     // (from command-line argument or from the Naming Service).
00104     if(optind<argc)
00105     {
00106       action="convert URI from command line into object reference";
00107       obj=orb->string_to_object(argv[optind]);
00108     }
00109     else
00110     {
00111       //
00112       // Get Name Service root context.
00113       action="resolve initial reference 'NameService'";
00114       obj=orb->resolve_initial_references("NameService");
00115       CosNaming::NamingContext_var rootContext=
00116         CosNaming::NamingContext::_narrow(obj);
00117       if(CORBA::is_nil(rootContext))
00118           throw CORBA::OBJECT_NOT_EXIST();
00119 
00120       //
00121       // Obtain reference to the Event Channel.
00122       action="find Event Channel in naming service";
00123       obj=rootContext->resolve(ecName);
00124 
00125       //
00126       // Unbind the Channel's reference in the naming service.
00127       action="unbind Event Channel from naming service";
00128       rootContext->unbind(ecName);
00129     }
00130     
00131     action="narrow object reference to event channel";
00132     CosEventChannelAdmin::EventChannel_var channel =
00133       CosEventChannelAdmin::EventChannel::_narrow(obj);
00134     if(CORBA::is_nil(channel))
00135         throw CORBA::OBJECT_NOT_EXIST();
00136     
00137     //
00138     // Destroy the EventChannel.
00139     action="destroy Event Channel";
00140     channel->destroy();
00141 
00142     //
00143     // Clean up nicely.
00144     action="destroy orb";
00145     orb->destroy();
00146 
00147     //
00148     // If we get here, then everything has worked OK.
00149     result=0;
00150 
00151   }
00152   catch(CORBA::ORB::InvalidName& ex) { // resolve_initial_references
00153      cerr<<"Failed to "<<action<<". ORB::InvalidName"<<endl;
00154   }
00155   catch(CosNaming::NamingContext::InvalidName& ex) { // resolve
00156      cerr<<"Failed to "<<action<<". NamingContext::InvalidName"<<endl;
00157   }
00158   catch(CosNaming::NamingContext::NotFound& ex) { // resolve
00159      cerr<<"Failed to "<<action<<". NamingContext::NotFound"<<endl;
00160   }
00161   catch(CosNaming::NamingContext::CannotProceed& ex) { // resolve
00162      cerr<<"Failed to "<<action<<". NamingContext::CannotProceed"<<endl;
00163   }
00164   catch(CORBA::TRANSIENT& ex) { // _narrow()
00165      cerr<<"Failed to "<<action<<". TRANSIENT"<<endl;
00166   }
00167   catch(CORBA::OBJECT_NOT_EXIST& ex) { // _narrow()
00168      cerr<<"Failed to "<<action<<". OBJECT_NOT_EXIST"<<endl;
00169   }
00170   catch(CORBA::SystemException& ex) {
00171      cerr<<"Failed to "<<action<<".";
00172 #if defined(HAVE_OMNIORB4)
00173      cerr<<" "<<ex._name();
00174      if(ex.NP_minorString())
00175          cerr<<" ("<<ex.NP_minorString()<<")";
00176 #endif
00177      cerr<<endl;
00178   }
00179   catch(CORBA::Exception& ex) {
00180      cerr<<"Failed to "<<action<<"."
00181 #if defined(HAVE_OMNIORB4)
00182        " "<<ex._name()
00183 #endif
00184        <<endl;
00185   }
00186 
00187   return result;
00188 }
00189 
00190 static void
00191 usage(int argc, char **argv)
00192 {
00193   cerr<<
00194 "\nDestroy an EventChannel.\n"
00195 "syntax: "<<(argc?argv[0]:"rmeventc")<<" OPTIONS [CHANNEL_URI]\n"
00196 "\n"
00197 "CHANNEL_URI: The event channel may be specified as a URI.\n"
00198 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
00199 "\n"
00200 "OPTIONS:                                         DEFAULT:\n"
00201 " -n NAME  channel name (if URI is not specified)  [\"EventChannel\"]\n"
00202 " -h       display this help text\n" << endl;
00203 }
00204