diff options
Diffstat (limited to 'TAO/examples/CSD_Strategy/ThreadPool2')
16 files changed, 1110 insertions, 0 deletions
diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/CSD_Test_ThreadPool2.mpc b/TAO/examples/CSD_Strategy/ThreadPool2/CSD_Test_ThreadPool2.mpc new file mode 100644 index 00000000000..c14935e056d --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/CSD_Test_ThreadPool2.mpc @@ -0,0 +1,25 @@ +// -*- MPC -*- +// $Id$ + +project(*Server): csd_threadpool, taoexe, portableserver { + exename = server_main + + Source_Files { + Foo_i.cpp + FooServantList.cpp + ServerApp.cpp + server_main.cpp + OrbShutdownTask.cpp + } +} + +project(*Client): taoexe, anytypecode { + exename = client_main + + Source_Files { + FooC.cpp + ClientApp.cpp + client_main.cpp + } +} + diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.cpp new file mode 100644 index 00000000000..14e52e52804 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.cpp @@ -0,0 +1,143 @@ +// $Id$ +#include "ClientApp.h" +#include "FooC.h" +#include "ace/Get_Opt.h" +#include "ace/Log_Msg.h" + + +ClientApp::ClientApp() +{ +} + + +ClientApp::~ClientApp() +{ +} + + +int +ClientApp::run(int argc, char* argv[] ACE_ENV_ARG_DECL) +{ + CORBA::ORB_var orb + = CORBA::ORB_init(argc, argv, "" ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + // Parse the command-line args for this application. + // * Raises -1 if problems are encountered. + // * Returns 1 if the usage statement was explicitly requested. + // * Returns 0 otherwise. + int result = this->parse_args(argc, argv); + if (result != 0) + { + return result; + } + + CORBA::Object_var obj + = orb->string_to_object(this->ior_.c_str() ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + if (CORBA::is_nil(obj.in())) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Failed to convert IOR string to obj ref.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + + Foo_var foo = Foo::_narrow(obj.in() ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + if (CORBA::is_nil(foo.in())) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Failed to narrow obj ref to Foo interface.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + + for (CORBA::Long i = 1; i <= 100; i++) + { + foo->op1(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + foo->op2(i ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + CORBA::Long value = foo->op3(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ===> Value retrieved from op3() == %d\n", + value)); + + for (CORBA::Long j = 1; j <= 5; j++) + { + foo->op4(495 + (i * 5) + j ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + } + + ACE_TRY_NEW_ENV + { + foo->op5(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_TRY_CHECK; + } + ACE_CATCH (FooException, ex) + { + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ===> Caught FooException - as expected.\n")); + + } + ACE_ENDTRY; + } + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ===> Tell server that we are done().\n")); + + foo->done(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ===> Back from done().\n")); + + return 0; +} + + + +int +ClientApp::parse_args(int argc, char* argv[]) +{ + this->exe_name_ = argv[0]; + + ACE_Get_Opt get_opts(argc, argv, "i:"); + + int c; + + while ((c = get_opts()) != -1) + { + switch (c) + { + case 'i': + this->ior_ = get_opts.opt_arg(); + break; + + case '?': + this->usage_statement(); + return 1; + default: + this->usage_statement(); + return -1; + } + } + + return 0; +} + + +void +ClientApp::usage_statement() +{ + ACE_ERROR((LM_ERROR, + "Usage: %s [options]\n\n" + "OPTIONS:\n\n" + "\t[-i <ior>]\n" + "\t[-?]\n\n", + this->exe_name_.c_str())); +} + diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.h b/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.h new file mode 100644 index 00000000000..7dc57bb78dc --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/ClientApp.h @@ -0,0 +1,30 @@ +// $Id$ +#ifndef CLIENTAPP_H +#define CLIENTAPP_H + +#include "ace/SString.h" +#include "ace/CORBA_macros.h" +#include "tao/Environment.h" + + +class ClientApp +{ + public: + + ClientApp(); + ~ClientApp(); + + int run(int argc, char* argv[] ACE_ENV_ARG_DECL); + + + private: + + int parse_args(int argc, char* argv[]); + void usage_statement(); + + ACE_CString ior_; + + ACE_CString exe_name_; +}; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/Foo.idl b/TAO/examples/CSD_Strategy/ThreadPool2/Foo.idl new file mode 100644 index 00000000000..f4a756a31b1 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/Foo.idl @@ -0,0 +1,29 @@ +// $Id$ +#ifndef FOO_IDL +#define FOO_IDL + +exception FooException {}; +exception TestException {}; + +interface Foo +{ + /// void return-type, no arguments + void op1(); + + /// void return-type, 1 "in" argument + void op2(in long value); + + /// long return-type, no arguments + long op3(); + + /// one-way version of op2 + oneway void op4(in long value); + + /// Operation that always raises an exception. + void op5() raises (FooException); + + /// Client calls this last. It will shutdown the server. + void done(); +}; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.cpp new file mode 100644 index 00000000000..1c1fc13eaf6 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.cpp @@ -0,0 +1,99 @@ +// $Id$ +#include "FooServantList.h" +#include "Foo_i.h" +#include "OrbShutdownTask.h" +#include "ace/OS.h" + + +FooServantList::FooServantList(const char* prefix, + unsigned num_servants, + unsigned num_clients, + CORBA::ORB_ptr orb) + : prefix_(prefix), + num_servants_(num_servants), + num_clients_(num_clients), + orb_ (CORBA::ORB::_duplicate(orb)) +{ + this->servants_ = new PortableServer::ServantBase_var[num_servants]; +} + + +FooServantList::~FooServantList() +{ + delete [] this->servants_; +} + + +void +FooServantList::create_and_activate(PortableServer::POA_ptr poa + ACE_ENV_ARG_DECL) +{ + for (unsigned i = 0; i < this->num_servants_; i++) + { + char buf[32]; + ACE_OS::sprintf(buf, "%02d", i + 1); + ACE_CString servant_name = this->prefix_ + "_" + buf; + + this->servants_[i] = new Foo_i(servant_name.c_str(),this); + + PortableServer::ObjectId_var id = + PortableServer::string_to_ObjectId(servant_name.c_str()); + + poa->activate_object_with_id(id.in(), + this->servants_[i].in() + ACE_ENV_ARG_PARAMETER); + ACE_CHECK; + + CORBA::Object_var obj = poa->id_to_reference(id.in() + ACE_ENV_ARG_PARAMETER); + ACE_CHECK; + + if (CORBA::is_nil(obj.in())) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Failed to activate servant (%s).\n", + servant_name.c_str())); + ACE_THROW (TestException()); + } + + CORBA::String_var ior + = this->orb_->object_to_string(obj.in() + ACE_ENV_ARG_PARAMETER); + ACE_CHECK; + + ACE_CString filename = servant_name + ".ior"; + FILE* ior_file = ACE_OS::fopen(filename.c_str(), "w"); + + if (ior_file == 0) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Cannot open output file (%s) for writing IOR.", + filename.c_str())); + ACE_THROW (TestException()); + } + + ACE_OS::fprintf(ior_file, "%s", ior.in()); + ACE_OS::fclose(ior_file); + } +} + + +void +FooServantList::client_done(ACE_ENV_SINGLE_ARG_DECL) +{ + unsigned num_left; + + { + GuardType guard(this->num_clients_lock_); + num_left = --this->num_clients_; + } + + if (num_left == 0) + { + if (TheOrbShutdownTask::instance()->open(0) != 0) + { + ACE_ERROR((LM_ERROR, "(%P|%t)FooServantList::client_done: " + "failed to create orb shutdown thread.\n")); + } + } +} diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.h b/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.h new file mode 100644 index 00000000000..d6e35ff894a --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.h @@ -0,0 +1,42 @@ +// $Id$ +#ifndef FOOSERVANTLIST_H +#define FOOSERVANTLIST_H + +#include "tao/ORB.h" +#include "tao/PortableServer/PortableServer.h" +#include "tao/PortableServer/Servant_Base.h" +#include "ace/SString.h" + + +class FooServantList +{ + public: + + FooServantList(const char* prefix, + unsigned num_servants, + unsigned num_clients, + CORBA::ORB_ptr orb); + ~FooServantList(); + + void create_and_activate(PortableServer::POA_ptr poa + ACE_ENV_ARG_DECL); + + void client_done(ACE_ENV_SINGLE_ARG_DECL); + + + private: + + typedef ACE_SYNCH_MUTEX LockType; + typedef ACE_Guard<LockType> GuardType; + + PortableServer::ServantBase_var* servants_; + ACE_CString prefix_; + unsigned num_servants_; + + LockType num_clients_lock_; + unsigned num_clients_; + + CORBA::ORB_var orb_; +}; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.cpp new file mode 100644 index 00000000000..5923bb5b022 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.cpp @@ -0,0 +1,86 @@ +// $Id$ +#include "Foo_i.h" +#include "FooServantList.h" +#include "ace/OS.h" + +Foo_i::Foo_i(const char* servant_name,FooServantList* mgr) + : value_(0), + count_op1_(0), + count_op2_(0), + count_op3_(0), + count_op4_(0), + count_op5_(0), + servant_name_(servant_name), + mgr_(mgr) +{ +} + + +Foo_i::~Foo_i() +{ +} + + +void +Foo_i::op1(ACE_ENV_SINGLE_ARG_DECL_NOT_USED) + ACE_THROW_SPEC((CORBA::SystemException)) +{ + ++this->count_op1_; + // Sleep for 10 milliseconds (10,000 microseconds) + //ACE_OS::sleep(ACE_Time_Value(0,10000)); +} + + +void +Foo_i::op2(CORBA::Long value ACE_ENV_ARG_DECL_NOT_USED) + ACE_THROW_SPEC((CORBA::SystemException)) +{ + ++this->count_op2_; + this->value_ = value; +} + + +CORBA::Long +Foo_i::op3(ACE_ENV_SINGLE_ARG_DECL_NOT_USED) + ACE_THROW_SPEC((CORBA::SystemException)) +{ + ++this->count_op3_; + return this->value_; +} + + +void +Foo_i::op4(CORBA::Long value ACE_ENV_ARG_DECL_NOT_USED) + ACE_THROW_SPEC((CORBA::SystemException)) +{ + ++this->count_op4_; + this->value_ = value; + + if (this->count_op4_ % 100 == 0) + { + ACE_DEBUG((LM_DEBUG, + "(%P|%t) op4() has been called %d times now. value == %d\n", + this->count_op4_, this->value_)); + } + + // Sleep for 10 milliseconds (10,000 microseconds) + //ACE_OS::sleep(ACE_Time_Value(0,10000)); +} + + +void +Foo_i::op5(ACE_ENV_SINGLE_ARG_DECL) + ACE_THROW_SPEC((CORBA::SystemException, FooException)) +{ + ++this->count_op5_; + ACE_THROW_SPEC (FooException()); +} + + +void +Foo_i::done(ACE_ENV_SINGLE_ARG_DECL) + ACE_THROW_SPEC((CORBA::SystemException)) +{ + this->mgr_->client_done(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK; +} diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.h b/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.h new file mode 100644 index 00000000000..5cc2a45e467 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/Foo_i.h @@ -0,0 +1,53 @@ +// $Id$ +#ifndef FOO_I_H +#define FOO_I_H + +#include "FooS.h" +#include "ace/SString.h" +#include "ace/CORBA_macros.h" +#include "tao/Environment.h" + +class FooServantList; + +class Foo_i : public virtual POA_Foo, + public virtual PortableServer::RefCountServantBase +{ + public: + + Foo_i(const char* servant_name, FooServantList* mgr); + virtual ~Foo_i(); + + virtual void op1(ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException)); + + virtual void op2(CORBA::Long value ACE_ENV_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException)); + + virtual CORBA::Long op3(ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException)); + + virtual void op4(CORBA::Long value ACE_ENV_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException)); + + virtual void op5(ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException, FooException)); + + virtual void done(ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS) + ACE_THROW_SPEC((CORBA::SystemException)); + + + private: + + CORBA::Long value_; + + unsigned count_op1_; + unsigned count_op2_; + unsigned count_op3_; + unsigned count_op4_; + unsigned count_op5_; + + ACE_CString servant_name_; + FooServantList* mgr_; +}; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.cpp new file mode 100644 index 00000000000..f9d551e8aee --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.cpp @@ -0,0 +1,77 @@ +// This may look like C, but it's really -*- C++ -*- + +//============================================================================= +/** + * @file OrbShutdownTask.cpp + * + * $Id$ + * + * @author Tim Bradley <bradley_t@ociweb.com> + */ +//============================================================================= + +#include "OrbShutdownTask.h" +#include "ace/CORBA_macros.h" +#include "ace/OS_NS_unistd.h" + + +OrbShutdownTask::OrbShutdownTask() +{ +} + + +OrbShutdownTask::~OrbShutdownTask() +{ +} + + +void +OrbShutdownTask::orb(CORBA::ORB_ptr orb) +{ + this->orb_ = CORBA::ORB::_duplicate (orb); +} + + +int +OrbShutdownTask::open(void*) +{ + if (this->activate(THR_NEW_LWP | THR_JOINABLE, 1) != 0) + { + // Assumes that when activate returns non-zero return code that + // no threads were activated. + ACE_ERROR_RETURN((LM_ERROR, + "(%P|%t) OrbShutdownTask failed to open().\n"), + -1); + } + + return 0; +} + + +int +OrbShutdownTask::svc() +{ + ACE_OS::sleep (2); + ACE_DEBUG ((LM_DEBUG, "(%P|%t)OrbShutdownTask::svc shutdown orb \n")); + ACE_TRY_NEW_ENV + { + this->orb_->shutdown(0 ACE_ENV_ARG_PARAMETER); + ACE_TRY_CHECK; + } + ACE_CATCHALL + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Exception raised by ORB::shutdown() call " + "in OrbShutdownTask::svc().\n")); + } + ACE_ENDTRY; + + return 0; +} + + +int +OrbShutdownTask::close(u_long) +{ + return 0; +} diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.h b/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.h new file mode 100644 index 00000000000..3795d12c3c9 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/OrbShutdownTask.h @@ -0,0 +1,41 @@ +// This may look like C, but it's really -*- C++ -*- + +//============================================================================= +/** + * @file OrbShutdownTask.h + * + * $Id$ + * + * @author Tim Bradley <bradley_t@ociweb.com> + */ +//============================================================================= + +#ifndef ORB_SHUTDOWN_TASK_H +#define ORB_SHUTDOWN_TASK_H + +#include "ace/Task.h" +#include "tao/ORB.h" + + +class OrbShutdownTask : public ACE_Task_Base +{ + public: + + OrbShutdownTask(); + virtual ~OrbShutdownTask(); + + void orb(CORBA::ORB_ptr orb); + + virtual int open(void*); + virtual int svc(); + virtual int close(u_long); + + + private: + + CORBA::ORB_var orb_; +}; + +typedef ACE_Singleton<OrbShutdownTask, ACE_Thread_Mutex> TheOrbShutdownTask; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/README b/TAO/examples/CSD_Strategy/ThreadPool2/README new file mode 100644 index 00000000000..4c770475642 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/README @@ -0,0 +1,61 @@ +# $Id$ + +Description: + +This is another test showing the use of a Custom Servant Dispatching +(CSD) Strategy. This test uses the reference implementation, known as the +Thread Pool CSD Strategy. This tests some simple remote two-way CORBA +requests and a simple remote one-way request. + +The server application creates 10 servant object, runs the ORB event +loop using the main thread, and uses 10 worker thread for the +Thread Pool CSD Strategy. + +The client application invokes simple two-way calls on the object +reference. There is also a simple one-way call that is also made on +the object reference. + +The run_test.pl script will launch 1 sever process, and 40 client processes. +Since the server process contains 10 distinct servants, the clients are +evenly divided such that each servant "serves" 4 distinct clients. + + +Executables: + + * client_main + + This is the client test executable. + + Command-line arguments: + + -? : Prints the executable's available command-line options, and + then exits. + + -i <ior string> : Provide the IOR to the client (ie, file://some.ior). + + * server_main + + This is the server test executable. + + Command-line arguments: + + -? : Prints the executable's available command-line options, and + then exits. + + -p <ior filename prefix> : Common prefix used in all ior filenames + that are output. + + -s <num servants> : The number of servant objects in the server. + + -c <num clients> : The number of clients that will use the server. + + * run_test.pl + + This perl script will run an automated test using 40 client processes + and 1 server process. The script returns 0 if the test was successful. + + No command-line options are supported. Simply execute the perl script + as follows: + + $ ./run_test.pl + diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.cpp new file mode 100644 index 00000000000..a9986df9ec5 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.cpp @@ -0,0 +1,229 @@ +// $Id$ +#include "ServerApp.h" +#include "FooServantList.h" +#include "FooC.h" +#include "OrbShutdownTask.h" +#include "ace/Get_Opt.h" +#include "tao/CSD_ThreadPool/CSD_TP_Strategy.h" +#include "tao/Intrusive_Ref_Count_Handle_T.h" + + +ServerApp::ServerApp() + : ior_filename_("foo"), + num_servants_(1), + num_clients_(1) +{ +} + + +ServerApp::~ServerApp() +{ +} + + +int +ServerApp::run(int argc, char* argv[] ACE_ENV_ARG_DECL) +{ + CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "" ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + // Parse the command-line args for this application. + // * Raises -1 if problems are encountered. + // * Returns 1 if the usage statement was explicitly requested. + // * Returns 0 otherwise. + int result = this->parse_args(argc, argv); + if (result != 0) + { + return result; + } + + TheOrbShutdownTask::instance()->orb (orb.in ()); + + CORBA::Object_var obj + = orb->resolve_initial_references("RootPOA" ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + if (CORBA::is_nil(obj.in())) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Failed to resolve initial ref for 'RootPOA'.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + + PortableServer::POA_var root_poa + = PortableServer::POA::_narrow(obj.in() ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + if (CORBA::is_nil(root_poa.in())) + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Failed to narrow obj ref to POA interface.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + + PortableServer::POAManager_var poa_manager + = root_poa->the_POAManager(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + // Create the child POA. + CORBA::PolicyList policies(1); + policies.length(1); + + policies[0] = root_poa->create_id_assignment_policy(PortableServer::USER_ID + ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + PortableServer::POA_var child_poa + = root_poa->create_POA("ChildPoa", + poa_manager.in(), + policies + ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + if (CORBA::is_nil(child_poa.in())) + { + ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: " + "Failed to create the child POA.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + + policies[0]->destroy (ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + // Create the thread pool servant dispatching strategy object, and + // hold it in a (local) smart pointer variable. + TAO_Intrusive_Ref_Count_Handle<TAO::CSD::TP_Strategy> csd_tp_strategy = + new TAO::CSD::TP_Strategy(); + + csd_tp_strategy->set_num_threads(this->num_servants_); + + // Tell the strategy to apply itself to the child poa. + if (csd_tp_strategy->apply_to(child_poa.in() ACE_ENV_ARG_PARAMETER) == false) + { + ACE_ERROR((LM_ERROR, "(%P|%t) ERROR [ServerApp::run()]: " + "Failed to apply custom dispatching strategy to child poa.\n")); + ACE_THROW_RETURN (TestException(), -1); + } + ACE_CHECK_RETURN (-1); + + FooServantList servants(this->ior_filename_.c_str(), + this->num_servants_, + this->num_clients_, + orb.in()); + + servants.create_and_activate(child_poa.in() ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + // Activate the POA Manager + poa_manager->activate(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp is ready. Running the ORB event loop.\n")); + + // Run the ORB event loop. + orb->run(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp ORB has stopped running. " + "Stop the CSD strategy.\n")); + + // Sleep for 2 second to let the done() two-way call complete + // before cleanup. + ACE_OS::sleep (2); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp is waiting for OrbShutdownTask.\n")); + TheOrbShutdownTask::instance()->wait (); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp is destroying the Root POA.\n")); + + // Tear-down the root poa and orb. + root_poa->destroy(1, 1 ACE_ENV_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp is destroying the ORB.\n")); + + orb->destroy(ACE_ENV_SINGLE_ARG_PARAMETER); + ACE_CHECK_RETURN (-1); + + ACE_DEBUG((LM_DEBUG, + "(%P|%t) ServerApp has completed running successfully.\n")); + + return 0; +} + + +int +ServerApp::parse_args(int argc, char* argv[]) +{ + this->exe_name_ = argv[0]; + + ACE_Get_Opt get_opts(argc, argv, "p:s:c:"); + + int c; + int tmp; + + while ((c = get_opts()) != -1) + { + switch (c) + { + case 'p': + this->ior_filename_ = get_opts.opt_arg(); + break; + + case 's': + tmp = ACE_OS::atoi(get_opts.opt_arg()); + if (tmp < 1) + { + this->usage_statement(); + return -1; + } + + this->num_servants_ = tmp; + break; + + case 'c': + tmp = ACE_OS::atoi(get_opts.opt_arg()); + if (tmp < 1) + { + this->usage_statement(); + return -1; + } + + this->num_clients_ = tmp; + break; + + case '?': + this->usage_statement(); + return 1; + + default: + this->usage_statement(); + return -1; + } + } + + return 0; +} + + +void +ServerApp::usage_statement() +{ + ACE_ERROR((LM_ERROR, + "Usage: %s [options]\n\n" + "OPTIONS:\n\n" + "\t[-p <ior_filename_prefix>]\n" + "\t[-s <num_servants>]\n" + "\t[-c <num_clients>]\n" + "\t[-?]\n", + "Default ior_filename_prefix is 'foo'.\n" + "Default num_servants is 1.\n" + "Default num_clients is 1.\n\n", + this->exe_name_.c_str())); +} + diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.h b/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.h new file mode 100644 index 00000000000..7556826f1e2 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/ServerApp.h @@ -0,0 +1,30 @@ +// $Id$ +#ifndef SERVERAPP_H +#define SERVERAPP_H + +#include "ace/SString.h" +#include "ace/CORBA_macros.h" +#include "tao/Environment.h" + +class ServerApp +{ + public: + + ServerApp(); + ~ServerApp(); + + int run(int argc, char* argv[] ACE_ENV_ARG_DECL); + + + private: + + int parse_args(int argc, char* argv[]); + void usage_statement(); + + ACE_CString exe_name_; + ACE_CString ior_filename_; + unsigned num_servants_; + unsigned num_clients_; +}; + +#endif diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/client_main.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/client_main.cpp new file mode 100644 index 00000000000..545d6b52a9a --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/client_main.cpp @@ -0,0 +1,44 @@ +// $Id$ +#include "ClientApp.h" +#include "ace/Log_Msg.h" +#include "ace/SString.h" +#include "tao/Exception.h" + +int +main(int argc, char* argv[]) +{ + ACE_LOG_MSG->priority_mask(LM_TRACE | + LM_DEBUG | + LM_INFO | + LM_NOTICE | + LM_WARNING | + LM_ERROR | + LM_CRITICAL | + LM_ALERT | + LM_EMERGENCY, + ACE_Log_Msg::PROCESS); + + + ClientApp app; + + ACE_TRY_NEW_ENV + { + int ret = app.run(argc,argv ACE_ENV_ARG_PARAMETER); + ACE_TRY_CHECK; + return ret == 1 ? 0 : ret; + } + ACE_CATCHANY + { + ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, + "Caught exception:"); + } + ACE_CATCHALL + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Unknown (...) exception caught in main() " + "for ClientApp\n")); + } + ACE_ENDTRY; + + return 1; +} diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/run_test.pl b/TAO/examples/CSD_Strategy/ThreadPool2/run_test.pl new file mode 100755 index 00000000000..60be2077f11 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/run_test.pl @@ -0,0 +1,77 @@ +eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' + & eval 'exec perl -S $0 $argv:q' + if 0; + +# $Id$ +# -*- perl -*- + +use lib '../../../../bin'; +use PerlACE::Run_Test; + +$status = 0; + +$iorfname_prefix = "server"; +$num_servants=10; +$num_clients_per_servant=4; +$num_clients=$num_servants * $num_clients_per_servant; + +#Delete old ior files. +for (my $i = 0; $i < $num_servants; $i++) { + $servant_id = sprintf("%02d", ($i + 1)); + $iorfile[$i] = PerlACE::LocalFile($iorfname_prefix . "_$servant_id.ior"); + + unlink $iorfile[$i]; +} + +$SV = new PerlACE::Process ("server_main", "-p $iorfname_prefix -s $num_servants -c $num_clients"); + +$SV->Spawn (); + +# Wait for the servant ior files created by server. +for (my $i = 0; $i < $num_servants; $i++) { + $servant_id = sprintf("%02d", ($i + 1)); + $iorfile[$i] = PerlACE::LocalFile($iorfname_prefix . "_$servant_id.ior"); + + if (PerlACE::waitforfile_timed ($iorfile[$i], + $PerlACE::wait_interval_for_process_creation) == -1) { + print STDERR "ERROR: cannot find file <$iorfile[$i]>\n"; + $SV->Kill (); $SV->TimedWait (1); + exit 1; + } +} + +$count = 0; + +for (my $i = 0; $i < $num_servants; $i++) { + for ($j = 0; $j < $num_clients_per_servant; $j++) { + $CLS[$count] = new PerlACE::Process ("client_main", " -i file://$iorfile[$i]"); + $CLS[$count]->Spawn (); + $count ++; + } +} + +for (my $i = 0; $i < $num_clients; $i++) { + $client = $CLS[$i]->WaitKill (60); + + if ($client != 0) { + print STDERR "ERROR: client $i returned $client\n"; + $status = 1; + } +} + +$server = $SV->WaitKill (60); + +if ($server != 0) { + print STDERR "ERROR: server returned $server\n"; + $status = 1; +} + +#Delete ior files generated by this run. +for (my $i = 0; $i < $num_servants; $i++) { + $servant_id = sprintf("%02d", ($i + 1)); + $iorfile[$i] = PerlACE::LocalFile($iorfname_prefix . "_$servant_id.ior"); + + unlink $iorfile[$i]; +} + +exit $status; diff --git a/TAO/examples/CSD_Strategy/ThreadPool2/server_main.cpp b/TAO/examples/CSD_Strategy/ThreadPool2/server_main.cpp new file mode 100644 index 00000000000..bb14220d3c7 --- /dev/null +++ b/TAO/examples/CSD_Strategy/ThreadPool2/server_main.cpp @@ -0,0 +1,44 @@ +// $Id$ +#include "ServerApp.h" +#include "ace/Log_Msg.h" +#include "tao/Exception.h" +#include "tao/Environment.h" + + +int +main(int argc, char* argv[]) +{ + ACE_LOG_MSG->priority_mask(LM_TRACE | + LM_DEBUG | + LM_INFO | + LM_NOTICE | + LM_WARNING | + LM_ERROR | + LM_CRITICAL | + LM_ALERT | + LM_EMERGENCY, + ACE_Log_Msg::PROCESS); + + ServerApp app; + + ACE_TRY_NEW_ENV + { + int ret = app.run(argc,argv ACE_ENV_ARG_PARAMETER); + ACE_TRY_CHECK; + return ret == 1 ? 0 : ret; + } + ACE_CATCHANY + { + ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, + "Caught exception:"); + } + ACE_CATCHALL + { + ACE_ERROR((LM_ERROR, + "(%P|%t) Unknown (...) exception caught in main() " + "for ServerApp\n")); + } + ACE_ENDTRY; + + return 1; +} |