ImR_Locator.cpp

Go to the documentation of this file.
00001 // $Id: ImR_Locator.cpp 78894 2007-07-14 14:56:10Z sowayaa $
00002 
00003 #include "ImR_Locator_i.h"
00004 #include "Locator_NT_Service.h"
00005 #include "Locator_Options.h"
00006 #include "orbsvcs/Shutdown_Utilities.h"
00007 
00008 class ImR_Locator_Shutdown : public Shutdown_Functor
00009 {
00010 public:
00011   ImR_Locator_Shutdown (ImR_Locator_i& imr);
00012 
00013   void operator() (int which_signal);
00014 private:
00015   ImR_Locator_i& imr_;
00016 };
00017 
00018 ImR_Locator_Shutdown::ImR_Locator_Shutdown (ImR_Locator_i &imr)
00019   : imr_(imr)
00020 {
00021 }
00022 
00023 void
00024 ImR_Locator_Shutdown::operator () (int /*which_signal*/)
00025 {
00026   try
00027     {
00028       this->imr_.shutdown (true);
00029     }
00030   catch (const CORBA::Exception& ex)
00031     {
00032       ex._tao_print_exception ("ImR: ");
00033     }
00034 }
00035 
00036 int
00037 run_standalone (Options& opts)
00038 {
00039   ImR_Locator_i server;
00040 
00041   ImR_Locator_Shutdown killer (server);
00042   Service_Shutdown kill_contractor(killer);
00043 
00044   try
00045     {
00046       int status = server.init (opts);
00047       if (status == -1)
00048         {
00049           return 1;
00050         }
00051       else
00052         {
00053           // Run the server if it is initialized correctly.
00054           server.run ();
00055 
00056           // End the server after its work is done.
00057           status = server.fini ();
00058 
00059           if (status == -1)
00060             return 1;
00061         }
00062       return 0;
00063     }
00064   catch (const CORBA::SystemException& sysex)
00065     {
00066       sysex._tao_print_exception ("System Exception");
00067     }
00068   catch (const CORBA::UserException& userex)
00069     {
00070       userex._tao_print_exception ("User Exception");
00071     }
00072   catch (const CORBA::Exception& ex)
00073     {
00074       ex._tao_print_exception ("Unknown Exception");
00075     }
00076 
00077   return 1;
00078 }
00079 
00080 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
00081 ACE_NT_SERVICE_DEFINE (service, Locator_NT_Service, IMR_LOCATOR_SERVICE_NAME);
00082 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
00083 
00084 int
00085 run_service (void)
00086 {
00087 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
00088 
00089   SERVICE::instance ()->name (IMR_LOCATOR_SERVICE_NAME, IMR_LOCATOR_DISPLAY_NAME);
00090 
00091   ACE_NT_SERVICE_RUN (service, SERVICE::instance (), ret);
00092 
00093   if (ret == 0)
00094     ACE_ERROR ((LM_ERROR, "%p\n", "Couldn't start service"));
00095 
00096   return ret;
00097 #else /* ACE_WIN32 */
00098   return 1;
00099 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
00100 }
00101 
00102 /**
00103  * Executes the various commands that are useful for a NT service.  Right
00104  * now these include 'install' and 'remove'.  Others, such as 'start' and
00105  * 'stop' can be added, but the 'net' program in Windows already handles
00106  * these commands.
00107  */
00108 static int
00109 run_service_command (Options& opts)
00110 {
00111   if (opts.service_command () == Options::SC_NONE)
00112     return 0;
00113 
00114 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_SERVICES)
00115   SERVICE::instance()->name (IMR_LOCATOR_SERVICE_NAME, IMR_LOCATOR_DISPLAY_NAME);
00116 
00117   if (opts.service_command () == Options::SC_INSTALL)
00118     {
00119       const DWORD MAX_PATH_LENGTH = 4096;
00120       char pathname[MAX_PATH_LENGTH];
00121 
00122       DWORD length = ACE_TEXT_GetModuleFileName (0, pathname, MAX_PATH_LENGTH);
00123       if (length == 0 || length >= MAX_PATH_LENGTH - sizeof(" -s"))
00124         {
00125           ACE_ERROR ((LM_ERROR, "Error: Could not get module file name\n"));
00126           return -1;
00127         }
00128 
00129       // Append the command used for running the implrepo as a service
00130       ACE_OS::strcat (pathname, ACE_TEXT (" -s"));
00131 
00132       int ret =  SERVICE::instance ()->insert (SERVICE_DEMAND_START,
00133                                            SERVICE_ERROR_NORMAL,
00134                                            pathname);
00135       if (ret != -1)
00136         {
00137           ACE_DEBUG ((LM_DEBUG, "ImR: Service installed.\n"));
00138           opts.save_registry_options ();
00139         }
00140       else
00141         {
00142           ACE_ERROR ((LM_ERROR, "Error: Failed to install service. error:%d\n", errno));
00143         }
00144       if (ret == 0)
00145         return 1;
00146     }
00147   else if (opts.service_command () == Options::SC_REMOVE)
00148     {
00149       int ret = SERVICE::instance ()->remove ();
00150       ACE_DEBUG ((LM_DEBUG, "ImR: Service removed.\n"));
00151       if (ret == 0)
00152         return 1; // If successfull, then we don't want to continue.
00153     }
00154   else
00155     {
00156       ACE_ERROR ((LM_ERROR, "Error: Unknown service command :%d \n",
00157         opts.service_command ()));
00158       return -1;
00159     }
00160 
00161   return -1;
00162 
00163 #else /* ACE_WIN32 */
00164   ACE_ERROR ((LM_ERROR, "NT Service not supported on this platform"));
00165   return -1;
00166 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_SERVICES */
00167 }
00168 
00169 int
00170 main (int argc, char *argv[])
00171 {
00172   Options opts;
00173 
00174   int result = opts.init (argc, argv);
00175   if (result < 0)
00176     return 1;  // Error
00177   else if (result > 0)
00178     return 0;  // No error, but we should exit anyway.
00179 
00180   result = run_service_command(opts);
00181   if (result < 0)
00182     return 1;  // Error
00183   else if (result > 0)
00184     return 0;  // No error, but we should exit anyway.
00185 
00186   if (opts.service ())
00187   {
00188     return run_service ();
00189   }
00190 
00191   return run_standalone (opts);
00192 }
00193 

Generated on Sun Jan 27 15:54:50 2008 for TAO_Implementation_Repository by doxygen 1.3.6