diff options
author | wolff1 <wolff1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2009-04-17 21:48:43 +0000 |
---|---|---|
committer | wolff1 <wolff1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2009-04-17 21:48:43 +0000 |
commit | 985f6056fcc6faa9917fe9db82b6464541d7ad98 (patch) | |
tree | 138952bb98c89a1d4ea73124788cb7890a148875 | |
parent | 9be876cfdd3a057c3de2d408bf11d9db862e0422 (diff) | |
download | ATCD-985f6056fcc6faa9917fe9db82b6464541d7ad98.tar.gz |
ChangeLogTag: Fri Apr 17 21:42:53 UTC 2009 Friedhelm Wolf <fwolf@dre.vanderbilt.edu>
12 files changed, 989 insertions, 0 deletions
diff --git a/TAO/ChangeLog b/TAO/ChangeLog index 4b3dfbf262d..3cd0bddd404 100644 --- a/TAO/ChangeLog +++ b/TAO/ChangeLog @@ -1,3 +1,9 @@ +Fri Apr 17 21:42:53 UTC 2009 Friedhelm Wolf <fwolf@dre.vanderbilt.edu> + + * orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer: + + Added new testing application. + Mon Mar 16 18:15:45 UTC 2009 Jeff Parsons <j.parsons@vanderbilt.edu> * orbsvcs/orbsvcs/LWFT/DDSStateUpdate_T.cpp: diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.cpp b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.cpp new file mode 100644 index 00000000000..1ed8649ac7d --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.cpp @@ -0,0 +1,128 @@ +// $Id$ + +#include <fstream> +#include <ace/High_Res_Timer.h> +#include "Client_Timer_Handler.h" +#include "WorkerC.h" +#include "ace/Reactor.h" +#include "tao/ORB_Core.h" + +extern double execution_time; + +Client_Timer_Handler::Client_Timer_Handler (unsigned long iterations, + unsigned long log_start, + const std::string & filename, + const ACE_Time_Value & period, + bool logging) + : period_ (period), + invocations_ (0), + logfile_ (filename), + max_iterations_ (iterations), + log_start_ (log_start), + logging_ (logging) +{ + timer_.calibrate (); +} + +Client_Timer_Handler::~Client_Timer_Handler () +{ + if (logging_) + { + std::ofstream out (logfile_.c_str ()); + + for (TimingList::iterator it = history_.begin (); + it != history_.end (); + ++it) + { + out << *it << std::endl; + } + + out.close (); + } +} + +void +Client_Timer_Handler::set_orb (CORBA::ORB_ptr orb) +{ + orb_ = CORBA::ORB::_duplicate (orb); +} + +void +Client_Timer_Handler::set_worker (DeCoRAM::Worker_ptr worker) +{ + worker_ = DeCoRAM::Worker::_duplicate (worker); +} + +int +Client_Timer_Handler::handle_timeout (const ACE_Time_Value &, + const void *) +{ + try + { + CORBA::ULong server_processing_time; + + timer_.start (); + + // we have to do some profiling first to see how we can achieve + // the correct execution time. + server_processing_time = worker_->run_task (execution_time); + + timer_.stop (); + + ++invocations_; + + ACE_DEBUG ((LM_EMERGENCY, "m(%s,%d,%d,%d) ", + logfile_.c_str (), + max_iterations_, + log_start_, + invocations_)); + + if (logging_ && (invocations_ >= log_start_)) + { + ACE_Time_Value rt; + timer_.elapsed_time (rt); + + history_.push_back (rt.msec () - server_processing_time); + } + } + catch (CORBA::SystemException & ex) + { + ACE_DEBUG ((LM_EMERGENCY, + ACE_TEXT ("Client_Timer_Handler::handle_timeout () -" + "caught: %s"), ex._info ().c_str ())); + + orb_->shutdown (); + + return 1; + } + + try + { + if ((max_iterations_ > 0) && (invocations_ >= max_iterations_)) + { + worker_->stop (); + + orb_->orb_core ()->reactor ()->cancel_timer (this); + + orb_->shutdown (); + } + } + catch (CORBA::Exception & ex) + { + ACE_DEBUG ((LM_WARNING, + "Client_Timer_Handler::handle_timeout () after run_task - " + "caught: %s", ex._info ().c_str ())); + } + + return 0; +} + +int +Client_Timer_Handler::handle_signal (int, siginfo_t *, ucontext_t *) +{ + orb_->orb_core ()->reactor ()->cancel_timer (this); + + orb_->shutdown (); + + return 0; +} diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.h b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.h new file mode 100644 index 00000000000..03b4ae40c47 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Client_Timer_Handler.h @@ -0,0 +1,57 @@ +// $Id$ + +#ifndef FTCLIENT_TIMER_HANDLER_H_ +#define FTCLIENT_TIMER_HANDLER_H_ + +#include <list> +#include "ace/Event_Handler.h" +#include "ace/High_Res_Timer.h" +#include "tao/ORB.h" +#include "WorkerC.h" + +class Client_Timer_Handler : public ACE_Event_Handler +{ + public: + Client_Timer_Handler (unsigned long iterations, + unsigned long log_start, + const std::string & filename, + const ACE_Time_Value & period, + bool logging = false); + + ~Client_Timer_Handler (); + + void set_orb (CORBA::ORB_ptr orb); + + // Hook method that is called by the reactor when a timer expires. + virtual int handle_timeout (const ACE_Time_Value &tv, + const void *); + + virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0); + + void set_worker (DeCoRAM::Worker_ptr worker); + + private: + CORBA::ORB_var orb_; + + DeCoRAM::Worker_var worker_; + + ACE_High_Res_Timer timer_; + + ACE_Time_Value period_; + + unsigned long invocations_; + + std::string logfile_; + + unsigned long max_iterations_; + + unsigned long log_start_; + + bool logging_; + + typedef std::list<CORBA::ULong> TimingList; + + TimingList history_; +}; + +#endif /* FTCLIENT_TIMER_HANDLER_H_ */ diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger.idl b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger.idl new file mode 100644 index 00000000000..03e6de9d37d --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger.idl @@ -0,0 +1,9 @@ +#ifndef TRIGGER_IDL_ +#define TRIGGER_IDL_ + +interface Trigger +{ + void start (in string prefix); +}; + +#endif /* TRIGGER_IDL_ */ diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.cpp b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.cpp new file mode 100644 index 00000000000..97eda886055 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.cpp @@ -0,0 +1,35 @@ +#include "Trigger_i.h" +#include "ace/Reactor.h" +#include "tao/ORB_Core.h" + +Trigger_i::Trigger_i (CORBA::ORB_ptr orb, + Client_Timer_Handler & handler, + unsigned long period) + : orb_ (CORBA::ORB::_duplicate (orb)), + timer_handler_ (handler), + started_ (false), + period_ (period) +{ +} + +Trigger_i::~Trigger_i () +{ +} + +void +Trigger_i::start (const char * /* prefix */) +{ + if (!started_) + { + // register the timer handler with the ORB reactor + ACE_Time_Value period; + period.msec (static_cast <long> (period_)); + + orb_->orb_core ()->reactor ()->schedule_timer (&timer_handler_, + 0, + ACE_Time_Value::zero, + period); + + started_ = true; + } +} diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.h b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.h new file mode 100644 index 00000000000..489e02f69a3 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Trigger_i.h @@ -0,0 +1,25 @@ +#ifndef TRIGGER_I_H_ +#define TRIGGER_I_H_ + +#include "TriggerS.h" +#include "Client_Timer_Handler.h" + +class Trigger_i : public POA_Trigger +{ + public: + Trigger_i (CORBA::ORB_ptr orb, + Client_Timer_Handler & handler, + unsigned long period); + + virtual ~Trigger_i (); + + void start (const char * prefix); + + private: + CORBA::ORB_var orb_; + Client_Timer_Handler & timer_handler_; + bool started_; + unsigned long period_; +}; + +#endif /* TRIGGER_I_H_ */ diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.idl b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.idl new file mode 100644 index 00000000000..295a0e60fc9 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.idl @@ -0,0 +1,11 @@ +#include "orbsvcs/orbsvcs/LWFT/ReplicatedApplication.idl" + +module DeCoRAM +{ + interface Worker : ReplicatedApplication + { + unsigned long run_task (in double execution_time); + + void stop (); + }; +}; diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.mpc b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.mpc new file mode 100644 index 00000000000..9a6055e4b40 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker.mpc @@ -0,0 +1,46 @@ +// -*- MPC -*- +// $Id$ + +project(*idl): taoidldefaults { + IDL_Files { + Worker.idl + } + + custom_only = 1 +} + +project(*Server): lwft_server, lwft_client, naming, rtcorba { + after += *idl + + libs += LWFT_StateSyncAgent + + Source_Files { + Worker_i.cpp + server.cpp + } + + Source_Files { + WorkerC.cpp + WorkerS.cpp + } + + IDL_Files { + } +} + +project(*Client): lwft_client, naming, rtcorba, portableserver { + after += *idl + + Source_Files { + client.cpp + Client_Timer_Handler.cpp + WorkerC.cpp + TriggerS.cpp + TriggerC.cpp + Trigger_i.cpp + } + + IDL_Files { + Trigger.idl + } +} diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.cpp b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.cpp new file mode 100644 index 00000000000..afdef051022 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.cpp @@ -0,0 +1,144 @@ +#include "Worker_i.h" + +Failure_Task::Failure_Task (CORBA::ORB_ptr orb, + long limit, + long & count) + : condition_ (lock_), + orb_ (CORBA::ORB::_duplicate (orb)), + limit_ (limit), + count_ (count), + stop_ (false) +{ +} + +int +Failure_Task::svc (void) +{ + ACE_Guard <ACE_Thread_Mutex> guard (lock_); + + while (((limit_ == 0) || (count_ <= limit_)) && !stop_) + { + condition_.wait (); + } + + orb_->shutdown (true); + + return 0; +} + +void +Failure_Task::signal (void) +{ + condition_.signal (); +} + +void +Failure_Task::stop (void) +{ + stop_ = true; + condition_.signal (); +} + +Worker_i::Worker_i (CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + const std::string & object_id, + StateSynchronizationAgent_ptr agent, + long invocations) + : orb_ (CORBA::ORB::_duplicate (orb)), + poa_ (PortableServer::POA::_duplicate (poa)), + object_id_ (object_id), + agent_ (StateSynchronizationAgent::_duplicate (agent)), + state_ (0), + suicidal_count_ (invocations), + task_ (orb_.in (), suicidal_count_, state_) +{ + timer_.calibrate (); + task_.activate (); +} + +CORBA::ULong +Worker_i::run_task (CORBA::Double execution_time) +{ + ACE_DEBUG ((LM_EMERGENCY, "x(%d) ", state_)); + + timer_.start (); + + /* do prime number calculation here */ + + ++state_; + + agent_->state_changed (object_id_.c_str ()); + + timer_.stop (); + + timer_.elapsed_time (last_execution_time_); + + ACE_DEBUG ((LM_TRACE, "x=%d ", last_execution_time_.msec ())); + + task_.signal (); + + return last_execution_time_.msec (); +} + +void Worker_i::stop () +{ + task_.stop (); +} + +void +Worker_i::set_state (const CORBA::Any & state_value) +{ + // extract value to an intermediate long variable since it's not possible + // to extract to a long & directly + CORBA::Long value; + + if (state_value >>= value) + state_ = value; + else + ACE_DEBUG ((LM_WARNING, + "(%P|%t) Worker_i::set_state () " + "could not extract state value from Any.")); + + ACE_DEBUG ((LM_TRACE, "Worker_i::set_state (%d) called.\n", value)); +} + +CORBA::Any * +Worker_i::get_state () +{ + // create new any object + CORBA::Any_var state (new CORBA::Any); + + // create intermediate object with the value + CORBA::Long value = state_; + + ACE_DEBUG ((LM_DEBUG, "(%P|%t) Worker_i::get_state returns %d.\n", value)); + + // insert value into the any object + *state <<= value; + + return state._retn (); +} + +StateSynchronizationAgent_ptr +Worker_i::agent (void) +{ + return StateSynchronizationAgent::_duplicate (agent_.in ()); +} + +void +Worker_i::agent (StateSynchronizationAgent_ptr agent) +{ + agent_ = agent; +} + +char * +Worker_i::object_id (void) +{ + return CORBA::string_dup (object_id_.c_str ()); +} + +void +Worker_i::object_id (const char * object_id) +{ + object_id_ = object_id; +} diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.h b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.h new file mode 100644 index 00000000000..56fe515e824 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/Worker_i.h @@ -0,0 +1,79 @@ +#ifndef WORKER_I_H_ +#define WORKER_I_H_ + +#include "WorkerS.h" +#include "ace/Task.h" +#include "ace/Condition_T.h" +#include "ace/High_Res_Timer.h" +#include "orbsvcs/orbsvcs/LWFT/StateSynchronizationAgentC.h" + +class Failure_Task : public ACE_Task_Base +{ + public: + Failure_Task (CORBA::ORB_ptr orb, + long limit, + long & count); + + int svc (void); + + void signal (void); + + void stop (void); + + private: + ACE_Thread_Mutex lock_; + ACE_Condition <ACE_Thread_Mutex> condition_; + CORBA::ORB_var orb_; + long limit_; + long & count_; + bool stop_; +}; + +class Worker_i : public POA_DeCoRAM::Worker +{ + public: + Worker_i (CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + const std::string & object_id, + StateSynchronizationAgent_ptr agent, + long invocations); + + virtual CORBA::ULong run_task (CORBA::Double execution_time); + + virtual void set_state (const CORBA::Any & state_value); + + virtual CORBA::Any *get_state (); + + virtual void stop (void); + + virtual StateSynchronizationAgent_ptr agent (void); + + virtual void agent (StateSynchronizationAgent_ptr agent); + + virtual char * object_id (void); + + virtual void object_id (const char * object_id); + + private: + CORBA::ORB_var orb_; + + PortableServer::POA_var poa_; + + std::string object_id_; + + StateSynchronizationAgent_var agent_; + + CORBA::Double load_; + + long state_; + + long suicidal_count_; + + Failure_Task task_; + + ACE_High_Res_Timer timer_; + + ACE_Time_Value last_execution_time_; +}; + +#endif /* WORKER_I_H_ */ diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/client.cpp b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/client.cpp new file mode 100644 index 00000000000..660c6069819 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/client.cpp @@ -0,0 +1,161 @@ +// $Id$ + +#include <sstream> +#include <fstream> +#include "WorkerC.h" +#include "ace/Get_Opt.h" +#include "orbsvcs/orbsvcs/LWFT/LWFT_Client_Init.h" +#include "Client_Timer_Handler.h" +#include "ace/Sig_Handler.h" +#include "ace/Reactor.h" +#include "tao/ORB_Core.h" +#include "Trigger_i.h" + +#include "tao/RTCORBA/RTCORBA.h" + +const ACE_TCHAR *ior1 = ACE_TEXT("file://test.ior"); +unsigned long iterations = 0; +unsigned long log_start = 0; +ACE_Time_Value period; +double execution_time = 100; +std::string server_id = "server"; +std::string prefix = ""; +bool logging = false; +bool kill_me = false; + +int +parse_args (int argc, ACE_TCHAR *argv[]) +{ + ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("f:n:p:r:e:s:i:lk")); + int c; + + while ((c = get_opts ()) != -1) + switch (c) + { + case 'f': + prefix = get_opts.opt_arg (); + break; + case 'n': + log_start = atoi (get_opts.opt_arg ()); + break; + case 'p': + ior1 = get_opts.opt_arg (); + break; + case 'r': + period.msec (atoi (get_opts.opt_arg ())); + break; + case 'e': + execution_time = atof (get_opts.opt_arg ()); + break; + case 's': + server_id = get_opts.opt_arg (); + break; + case 'i': + iterations = atoi (get_opts.opt_arg ()); + break; + case 'l': + logging = true; + break; + case 'k': + kill_me = true; + break; + case '?': + default: + ACE_ERROR_RETURN ((LM_ERROR, + "usage: %s " + "-p <ior> " + "-o <ior> " + "\n", + argv [0]), + -1); + } + + return 0; +} + +int +check_for_nil (CORBA::Object_ptr obj, const char *msg) +{ + if (CORBA::is_nil (obj)) + ACE_ERROR_RETURN ((LM_ERROR, + "ERROR: Object reference <%C> is nil\n", + msg), + -1); + else + return 0; +} + +int +ACE_TMAIN(int argc, ACE_TCHAR *argv[]) +{ + try + { + LWFT_Client_Init ci; + CORBA::ORB_var orb = ci.init (argc, argv); + + // Initialize the ORB, resolve references and parse arguments. + + // Parse arguments. + if (parse_args (argc, argv) != 0) + { + return -1; + } + + // Test object 1. + CORBA::Object_var object = + orb->string_to_object (ior1); + + DeCoRAM::Worker_var server1 = DeCoRAM::Worker::_narrow (object.in ()); + if (check_for_nil (server1.in (), "server1") == -1) + return -1; + + // Check that test objects are configured with SERVER_DECLARED + // PriorityModelPolicy, and get their server priorities. + + Client_Timer_Handler timeout_handler (iterations, + log_start, + prefix + server_id + "-client.txt", + period, + logging); + + timeout_handler.set_orb (orb.in ()); + timeout_handler.set_worker (server1.in ()); + + // add a the handler for the SIGINT signal here + ACE_Sig_Handler sig_handler; + sig_handler.register_handler (SIGINT, &timeout_handler); + + Trigger_i * trigger = new Trigger_i (orb.in (), + timeout_handler, + period.msec ()); + + PortableServer::ServantBase_var ownership_transfer (trigger); + + CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA"); + + PortableServer::POA_var poa = PortableServer::POA::_narrow (obj.in ()); + + PortableServer::POAManager_var poa_mgr = poa->the_POAManager (); + + poa_mgr->activate (); + + Trigger_var trig = trigger->_this (); + + std::string fname = server_id + "Client.ior"; + std::ofstream file (fname.c_str ()); + file << orb->object_to_string (trig.in ()); + file.close (); + + orb->run (); + + // orb->orb_core ()->reactor ()->cancel_timer (&timeout_handler); + } + catch (const CORBA::Exception& ex) + { + ex._tao_print_exception ( + "Unexpected exception in Server_Declared test client:"); + return -1; + } + + return 0; +} diff --git a/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/server.cpp b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/server.cpp new file mode 100644 index 00000000000..38e04889b32 --- /dev/null +++ b/TAO/orbsvcs/examples/FaultTolerance/FLARe/RTCORBA/ClientServer/server.cpp @@ -0,0 +1,288 @@ +// $Id$ + +#include <sstream> +#include "ace/Get_Opt.h" +#include "tao/ORB_Core.h" +#include "ace/Task.h" +#include "Worker_i.h" + +#include "orbsvcs/orbsvcs/LWFT/AppOptions.h" +#include "orbsvcs/orbsvcs/LWFT/AppSideMonitor_Thread.h" +#include "orbsvcs/orbsvcs/LWFT/StateSyncAgentTask.h" +#include "orbsvcs/orbsvcs/LWFT/ReplicationManagerC.h" +#include "orbsvcs/orbsvcs/LWFT/LWFT_Server_Init.h" +#include "orbsvcs/orbsvcs/LWFT/LWFT_Client_Init.h" +#include "orbsvcs/Naming/Naming_Client.h" + +#include "tao/RTCORBA/RTCORBA.h" + +std::string ior_output = "test1.ior"; +long invocations = 0; + +// Parse command-line arguments. +int +parse_args (int argc, ACE_TCHAR *argv[]) +{ + ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:i:")); + int c; + + while ((c = get_opts ()) != -1) + switch (c) + { + case 'i': + invocations = atoi (get_opts.opt_arg ()); + break; + case 'p': + ior_output = get_opts.opt_arg (); + break; + case '?': + default: + ACE_ERROR_RETURN ((LM_ERROR, + "usage: %s " + "-p <iorfile1> " + "-i <number of invocation before failure> " + "\n", + argv [0]), + -1); + } + + return 0; +} + +int +check_for_nil (CORBA::Object_ptr obj, const char *msg) +{ + if (CORBA::is_nil (obj)) + ACE_ERROR_RETURN ((LM_ERROR, + "ERROR: Object reference <%C> is nil\n", + msg), + -1); + else + return 0; +} + +int +create_object (PortableServer::POA_ptr poa, + CORBA::ORB_ptr orb, + Worker_i *server_impl, + const ACE_TCHAR *filename) +{ + // Register with poa. + PortableServer::ObjectId_var id = + PortableServer::string_to_ObjectId (AppOptions::instance ()->app_id ().c_str ()); + + poa->activate_object_with_id (id, + server_impl); + + CORBA::Object_var server = + poa->id_to_reference (id.in ()); + + // Print out the IOR. + CORBA::String_var ior = + orb->object_to_string (server.in ()); + + // Print ior to the file. + if (filename != 0) + { + FILE *output_file= ACE_OS::fopen (filename, "w"); + if (output_file == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "Cannot open output file for writing IOR: %s", + filename), + -1); + ACE_OS::fprintf (output_file, "%s", ior.in ()); + ACE_OS::fclose (output_file); + } + + return 0; +} + +class Task : public ACE_Task_Base +{ +public: + + Task (ACE_Thread_Manager &thread_manager, + CORBA::ORB_ptr orb, + ReplicationManager_ptr rm, + StateSynchronizationAgent_ptr agent); + + int svc (void); + + CORBA::ORB_var orb_; + + ReplicationManager_var rm_; + StateSynchronizationAgent_ptr agent_; +}; + +Task::Task (ACE_Thread_Manager &thread_manager, + CORBA::ORB_ptr orb, + ReplicationManager_ptr rm, + StateSynchronizationAgent_ptr agent) + : ACE_Task_Base (&thread_manager), + orb_ (CORBA::ORB::_duplicate (orb)), + rm_ (ReplicationManager::_duplicate (rm)), + agent_ (StateSynchronizationAgent::_duplicate (agent)) +{ +} + +int +Task::svc (void) +{ + try + { + // RootPOA. + CORBA::Object_var object = + this->orb_->resolve_initial_references("RootPOA"); + + PortableServer::POA_var root_poa = + PortableServer::POA::_narrow (object.in ()); + + if (check_for_nil (root_poa.in (), "RootPOA") == -1) + return -1; + + // POAManager. + PortableServer::POAManager_var poa_manager = + root_poa->the_POAManager (); + + // Servant. + Worker_i server_impl (this->orb_.in (), + root_poa.in (), + AppOptions::instance ()->app_id (), + agent_, + invocations); + + int result = create_object (root_poa.in (), + orb_.in (), + &server_impl, + ior_output.c_str ()); + + CORBA::Object_var obj = root_poa->servant_to_reference (&server_impl); + + if (result == -1) + return -1; + + // Activate POA manager. + poa_manager->activate (); + + rm_->register_application ( + AppOptions::instance ()->app_id ().c_str (), + AppOptions::instance ()->load (), + AppOptions::instance ()->host_id ().c_str (), + AppOptions::instance ()->process_id ().c_str (), + AppOptions::instance ()->role (), + obj.in ()); + + ReplicatedApplication_var app = ReplicatedApplication::_narrow (obj.in ()); + + agent_->register_application (AppOptions::instance ()->app_id ().c_str (), + app.in ()); + + // Start ORB event loop. + this->orb_->run (); + + ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n\n")); + } + catch (const CORBA::Exception& ex) + { + ex._tao_print_exception ( + "Unexpected exception caught in Server_Declared test server:"); + return -1; + } + + return 0; +} + +int +ACE_TMAIN(int argc, ACE_TCHAR *argv[]) +{ + try + { + // ORB. + LWFT_Client_Init client_initializer; + CORBA::ORB_var orb = + client_initializer.init (argc, argv); + + AppOptions::instance ()->parse_args (argc, argv); + AppOptions::instance ()->orb (CORBA::ORB::_duplicate (orb.in ())); + + int result = AppSideMonitor_Thread::instance ()->activate (); + + if (result != 0) + { + ACE_ERROR_RETURN ((LM_ERROR, + "AppSideReg::activate () returned %d\n", + result), + -1); + } + + // create task for state synchronization agent + result = StateSyncAgentTask::instance ()->activate (); + + if (result != 0) + { + ACE_ERROR_RETURN ((LM_ERROR, + "StateSyncAgentTask::activate () " + "returned %d, errno = %d\n", + result, + errno), + -1); + } + + ReplicationManager_var rm; + + TAO_Naming_Client naming_client; + naming_client.init (orb.in ()); + + CosNaming::NamingContextExt_var ns = + CosNaming::NamingContextExt::_narrow (naming_client.get_context ()); + + CORBA::Object_var rm_obj = ns->resolve_str ("ReplicationManager"); + + if (CORBA::is_nil (rm_obj.in ())) + { + ACE_ERROR ((LM_ERROR, + "ServerTask: Null RM objref from Naming Service\n")); + } + else + { + rm = ReplicationManager::_narrow (rm_obj.in ()); + } + + rm->register_state_synchronization_agent ( + AppOptions::instance ()->host_id ().c_str (), + AppOptions::instance ()->process_id ().c_str (), + StateSyncAgentTask::instance ()->agent_ref ()); + + // Parse arguments. + if (parse_args (argc, argv) != 0) + return -1; + + // Thread Manager for managing task. + ACE_Thread_Manager thread_manager; + + // Create task. + Task task (thread_manager, + orb.in (), + rm.in (), + StateSyncAgentTask::instance ()->agent_ref ()); + + // Task activation flags. + long flags = + THR_NEW_LWP | + THR_JOINABLE | + orb->orb_core ()->orb_params ()->thread_creation_flags (); + + // Activate task. + result = task.activate (flags); + + // Wait for task to exit. + result = thread_manager.wait (); + } + catch (const CORBA::Exception& ex) + { + ex._tao_print_exception ("Exception caught: "); + return -1; + } + + return 0; +} |