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

test-server.cc

00001 /*
00002 *  Name:      test-server.cc
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Test for CORBA (server)
00005 *  Date:      $Date: 2003/10/06 12:45:11 $
00006 *  Revision:  $Revision: 1.2 $
00007 *
00008 *  Copyright (C) 2001-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program 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
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #include <cstdlib>
00027 #include <fstream>
00028 #include <iostream>
00029 #include <mpcl/net/corba/orb.hh>
00030 #include <mpcl/net/corba/server_application.hh>
00031 #include <mpcl/system/all.hh>
00032 #include <mpcl/test.h>
00033 // <FIXME comment="this must abstracted using mpcl::system">
00034 #include <unistd.h>
00035 // </FIXME>
00036 #include "test-sample_producer.hh"
00037 
00038 
00039 using mpcl::net::corba::TOrb;
00040 using mpcl::net::corba::TServerApplication;
00041 using mpcl::system::GetErrorMessage;
00042 using mpcl::system::tSystem;
00043 using mpcl::text::TString;
00044 
00045 
00046 //
00047 //  L O C A L   V A R I A B L E S
00048 //
00049 
00050 static const char*   _pkcIorFileName = "test-server.ior.txt";
00051 
00052 
00057 template <typename TOrb>
00058 class TSampleProducer                        :
00059   public POA_mpcl::TSampleProducer           ,
00060   public PortableServer::RefCountServantBase
00061 {
00062 
00063   private:
00064 
00066     long int   liValue;
00067 
00069     TOrb&   rtOrb;
00070 
00071 
00072   public:
00073 
00074     TSampleProducer (TOrb& rtORB, long int liSTART_VALUE = 0) :
00075       POA_mpcl::TSampleProducer           ()                  ,
00076       PortableServer::RefCountServantBase ()                  ,
00077       liValue                             (liSTART_VALUE)     ,
00078       rtOrb                               (rtORB)             {}
00079 
00081     CORBA::Long get (void)
00082     {
00083       return liValue++;
00084     }
00085 
00087     void finish (void)
00088     {
00089       rtOrb.finish();
00090     }
00091 
00092 };  // class TSampleProducer
00093 
00094 
00096 class TProducerApplication :
00097   public TServerApplication<TOrb>
00098 {
00099 
00100   private:
00101 
00103     typedef
00104       TSampleProducer<TOrb>
00105       TCorbaSampleProducer;
00106 
00108     typedef
00109       mpcl::TSampleProducer_var
00110       QTCorbaSampleProducer;
00111 
00112 
00113   public:
00114 
00115     TProducerApplication ( const char* pkcNAME    ,
00116                            const char* pkcRELEASE ,
00117                            TOrb&       rtORB      )         :
00118       TServerApplication<TOrb> (pkcNAME, pkcRELEASE, rtORB) {}
00119 
00120     void initialize (void)
00121     {
00122       TCorbaSampleProducer*   ptNamedSampleProducer1;
00123       TCorbaSampleProducer*   ptNamedSampleProducer2;
00124       TCorbaSampleProducer*   ptSampleProducer;
00125       QTCorbaSampleProducer   qtNamedSampleProducer1;
00126       QTCorbaSampleProducer   qtNamedSampleProducer2;
00127       QTCorbaSampleProducer   qtSampleProducer;
00128       std::ofstream           tOfstream (_pkcIorFileName);
00129 
00130       ptNamedSampleProducer1 = new TCorbaSampleProducer (rtOrb, 10);
00131       ptNamedSampleProducer2 = new TCorbaSampleProducer (rtOrb, 20);
00132       ptSampleProducer       = new TCorbaSampleProducer (rtOrb);
00133       qtNamedSampleProducer1 =
00134         rtOrb.enrol<TCorbaSampleProducer, QTCorbaSampleProducer> (ptNamedSampleProducer1, "/mpcl/test-server/sample_producer_1.object");
00135       qtNamedSampleProducer2 =
00136         rtOrb.enrol<TCorbaSampleProducer, QTCorbaSampleProducer> (ptNamedSampleProducer2, "/mpcl/test-server/sample_producer_2.object");
00137       qtSampleProducer =
00138         rtOrb.enrol<TCorbaSampleProducer, QTCorbaSampleProducer> (ptSampleProducer);
00139       tOfstream << rtOrb.name (qtSampleProducer) << std::endl;
00140     }
00141 
00142 
00143   public:
00144 
00145     TString programFileName (void) const
00146     {
00147       return "test-server";
00148     }
00149 
00150 };  // class TProducerApplication
00151 
00152 
00154 int main (int argc, const char** argv)
00155 {
00156 
00157   using std::basic_ifstream;
00158 
00159   TEST_INIT ("tests for CORBA (server)");
00160 
00161   try
00162   {
00163     switch (fork())
00164     {
00165       case 0:
00166       {
00167         //
00168         //  Child process running as a daemon.
00169         //
00170         TOrb                   tOrb (argc, argv);
00171         TProducerApplication   tApplication ("test-server", "0.0.0", tOrb);
00172     
00173         tApplication.initialize();
00174         TEST_FUNCTION (tApplication.start());
00175         break;
00176       }
00177       case -1:
00178       {
00179         throw mpcl::TException ("test-server", GetErrorMessage(), __FILE__, __LINE__);
00180         break;
00181       }
00182       default:
00183       {
00184         basic_ifstream<char>   tIfstream;
00185         int                    I = 0;
00186 
00187         //
00188         //  Parent process. Returns code 77  to  force  AUTOMAKE to  ignore the
00189         //  result of this test.
00190         //
00191         TEST_FAIL_WITH_CODE (77);
00192         do
00193         {
00194           //
00195           //  Tests IOR file readiness from [2, (2 * 10)] seconds.
00196           //
00197           tSystem.sleep (2);
00198           tIfstream.clear();
00199           tIfstream.open (_pkcIorFileName);
00200         } while ( !tIfstream.is_open() && ( I++ < 10 ) );
00201         break;
00202       }
00203     }
00204   }
00205   catch (CORBA::SystemException&)
00206   {
00207     std::cerr << "ORB; caught CORBA::SystemException" << std::endl;
00208     TEST_FAIL;
00209   }
00210   catch (CORBA::Exception&)
00211   {
00212     std::cerr << "ORB; caught CORBA::Exception" << std::endl;
00213     TEST_FAIL;
00214   }
00215   catch (const mpcl::TException& rktEXCEPTION)
00216   {
00217     std::cerr << rktEXCEPTION;
00218     TEST_FAIL;
00219   }
00220   catch (std::exception& rktEXCEPTION)
00221   {
00222     std::cerr << rktEXCEPTION.what();
00223     TEST_FAIL;
00224   }
00225   catch (...)
00226   {
00227     std::cerr << "caught unknown exception" << std::endl;
00228     TEST_FAIL;
00229   }
00230   TEST_MEMORY_STATUS;
00231   TEST_RETURN_CODE;
00232 
00233 }  // main()

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18