channel.cc

Go to the documentation of this file.
00001 //                            Package   : omniEvents
00002 //   channel.cc               Created   : 2005/04/23
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2005 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 //    Demonstates how to make a standalone EventChannel in your own
00025 //    application, using libomniEvents.
00026 //      
00027 
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #  include "config.h"
00031 #endif
00032 
00033 #include <stdlib.h>
00034 #include <signal.h>
00035 
00036 #ifdef HAVE_IOSTREAM
00037 #  include <iostream>
00038 #else
00039 #  include <iostream.h>
00040 #endif
00041 
00042 #ifdef HAVE_STD_IOSTREAM
00043 using namespace std;
00044 #endif
00045 
00046 #include <omniEvents/EventChannel.h>
00047 
00049 void myShutdown(int signum)
00050 {
00051   OmniEvents::Orb::inst().shutdown(signum);
00052 }
00053 
00054 int main(int argc, char **argv)
00055 {
00056   //
00057   // Start orb.
00058   CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
00059 
00060   const char* action=""; // Use this variable to help report errors.
00061   try {
00062 
00063     action="initialise OmniEvents::Orb";
00064     // Your code MUST include these two lines.
00065     OmniEvents::Orb::inst()._orb=orb;
00066     OmniEvents::Orb::inst().resolveInitialReferences();
00067 
00068     action="activate the RootPOA's POAManager";
00069     // You MUST activate the RootPOA's POAManager. You can do this yourself
00070     // in the normal way, or you can use the reference that OmniEvents::Orb
00071     // has resolved for you.
00072     PortableServer::POAManager_var pman;
00073     pman=OmniEvents::Orb::inst()._RootPOA->the_POAManager();
00074     pman->activate();
00075 
00076     action="create EventChannel servant";
00077     // The constructor just allocates memory.
00078     OmniEvents::EventChannel_i* channelSrv =new OmniEvents::EventChannel_i();
00079 
00080     action="activate EventChannel servant";
00081     // activate() creates & activates the EventChannel's POA and CORBA objects.
00082     channelSrv->activate("MyChannel");
00083 
00084     // From this point, clients may invoke EventChannel operations.
00085 
00086     action="start EventChannel's thread";
00087     // Starts the thread that controls the EventChannel. No events will be
00088     // delivered until the thread has started.
00089     channelSrv->start();
00090     
00091     action="obtain an object reference to the EventChannel";
00092     CosEventChannelAdmin::EventChannel_var channelRef =channelSrv->_this();
00093 
00094     // The user interface of this example is simple: The EventChannel's IOR
00095     // is dumped to the standard output stream.
00096     action="stringify the EventChannel reference";
00097     CORBA::String_var sior =orb->object_to_string(channelRef.in());
00098     cout<<sior.in()<<endl;
00099 
00100     action="set signal handlers";
00101     ::signal(SIGINT , ::myShutdown);
00102     ::signal(SIGTERM, ::myShutdown);
00103  
00104     action="collect orphan requests";
00105     // You MUST call this method, it processes orphan (asynchronous) method
00106     // calls made by the EventChannel.
00107     // You can safely call it instead of CORBA::ORB::run(). If you do not
00108     // want to park the main thread, then you must create a new thread for this
00109     // method.
00110     OmniEvents::Orb::inst().run();
00111 
00112     // OmniEvents::Orb::shutdown() has been called by the myShutdown() signal
00113     // handler. (The user pressed Ctrl-C or killed the process.)
00114 
00115     // In order to make run() return, you MUST call OmniEvents::Orb::shutdown().
00116 
00117     action="destroy orb";
00118     orb->destroy();
00119 
00120   }
00121   catch(CORBA::SystemException& ex) {
00122      cerr<<"Failed to "<<action<<".";
00123 #if defined(HAVE_OMNIORB4)
00124      cerr<<" "<<ex._name();
00125      if(ex.NP_minorString())
00126          cerr<<" ("<<ex.NP_minorString()<<")";
00127 #endif
00128      cerr<<endl;
00129      ::exit(1);
00130   }
00131   catch(CORBA::Exception& ex) {
00132      cerr<<"Failed to "<<action<<"."
00133 #if defined(HAVE_OMNIORB4)
00134        " "<<ex._name()
00135 #endif
00136        <<endl;
00137      ::exit(1);
00138   }
00139 
00140   return 0;
00141 }

Generated on Mon Jan 9 03:52:13 2006 for OmniEvents by  doxygen 1.4.6