summaryrefslogtreecommitdiff
path: root/TAO/DevGuideExamples/Multithreading/ThreadPool
diff options
context:
space:
mode:
authorAbdullah Sowayan <sowayan@users.noreply.github.com>2008-10-21 20:47:43 +0000
committerAbdullah Sowayan <sowayan@users.noreply.github.com>2008-10-21 20:47:43 +0000
commitd2911d5b9eb897d3da7d458ebf5ba8b998bc7763 (patch)
tree3158327d3787df5b439329fac177f20a12857c62 /TAO/DevGuideExamples/Multithreading/ThreadPool
parentf8ea2bc5a4d98525f6f290d8272663e46aa1de74 (diff)
downloadATCD-d2911d5b9eb897d3da7d458ebf5ba8b998bc7763.tar.gz
Tue Oct 21 19:10:21 UTC 2008 Abdullah Sowayan <abdullah.sowayan@lmco.com>
Diffstat (limited to 'TAO/DevGuideExamples/Multithreading/ThreadPool')
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger.idl8
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerClient.cpp36
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerServer.cpp73
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.cpp41
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.h39
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/README74
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/ThreadPool.mpc13
-rw-r--r--TAO/DevGuideExamples/Multithreading/ThreadPool/run_test.pl113
8 files changed, 397 insertions, 0 deletions
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger.idl b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger.idl
new file mode 100644
index 00000000000..335d899f058
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger.idl
@@ -0,0 +1,8 @@
+// Messenger.idl
+
+interface Messenger
+{
+ boolean send_message(in string user_name,
+ in string subject,
+ inout string message);
+};
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerClient.cpp b/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerClient.cpp
new file mode 100644
index 00000000000..d3eac221e2c
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerClient.cpp
@@ -0,0 +1,36 @@
+#include "MessengerC.h"
+#include <iostream>
+int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
+{
+ try {
+ // Initialize the ORB.
+ CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
+
+ // Read and destringify the Messenger object's IOR.
+ CORBA::Object_var obj = orb->string_to_object( "file://Messenger.ior" );
+ if( CORBA::is_nil( obj.in() ) ) {
+ std::cerr << "Could not get Messenger IOR." << std::endl;
+ return 1;
+ }
+
+ // Narrow the IOR to a Messenger object reference.
+ Messenger_var messenger = Messenger::_narrow( obj.in() );
+ if( CORBA::is_nil( messenger.in() ) ) {
+ std::cerr << "IOR was not a Messenger object reference." << std::endl;
+ return 1;
+ }
+
+ // Send a message the the Messenger object.
+ CORBA::String_var message = CORBA::string_dup( "Hello!" );
+ messenger->send_message( "TAO User", "TAO Test", message.inout() );
+
+ // Print the Messenger's reply.
+ std::cout << "Reply: " << message.in() << std::endl;
+ }
+ catch(const CORBA::Exception& ex) {
+ std::cerr << "CORBA exception: " << ex << std::endl;
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerServer.cpp b/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerServer.cpp
new file mode 100644
index 00000000000..af34d103c0d
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/MessengerServer.cpp
@@ -0,0 +1,73 @@
+#include "Messenger_i.h"
+#include <iostream>
+#include <fstream>
+// 1. Define a "task" class for implenting the thread pool threads.
+#include <ace/Task.h>
+
+class ORB_Task : public ACE_Task_Base
+{
+public:
+ ORB_Task (CORBA::ORB_ptr orb)
+ : orb_(CORBA::ORB::_duplicate(orb)) { }
+ virtual ~ORB_Task () { }
+ virtual int svc ()
+ {
+ this->orb_->run();
+ return 0;
+ }
+private:
+ CORBA::ORB_var orb_;
+};
+
+// 2. Establish the number of threads.
+static const int nthreads = 4;
+
+int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
+{
+ try {
+ // Initialize the ORB.
+ CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
+
+ //Get reference to the RootPOA.
+ CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
+ PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
+
+ // Activate the POAManager.
+ PortableServer::POAManager_var mgr = poa->the_POAManager();
+ mgr->activate();
+
+ // Create a servant.
+ Messenger_i messenger_servant;
+
+ // Register the servant with the RootPOA, obtain its object
+ // reference, stringify it, and write it to a file.
+ PortableServer::ObjectId_var oid =
+ poa->activate_object( &messenger_servant );
+ CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
+ CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
+ std::ofstream iorFile( "Messenger.ior" );
+ iorFile << str.in() << std::endl;
+ iorFile.close();
+ std::cout << "IOR written to file Messenger.ior" << std::endl;
+
+ // 3. Create and activate threads for the thread pool.
+ ORB_Task task (orb.in());
+ int retval = task.activate (THR_NEW_LWP | THR_JOINABLE, nthreads);
+ if (retval != 0) {
+ std::cerr << "Failed to activate " << nthreads << " threads." << std::endl;
+ return 1;
+ }
+
+ // 4. Wait for threads to finish.
+ task.wait();
+
+ // Clean up.
+ orb->destroy();
+ }
+ catch(const CORBA::Exception& ex) {
+ std::cerr << "CORBA exception: " << ex << std::endl;
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.cpp b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.cpp
new file mode 100644
index 00000000000..1ef3f40cf89
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.cpp
@@ -0,0 +1,41 @@
+/* -*- C++ -*- $Id$ */
+
+// ****** Code generated by the The ACE ORB (TAO) IDL Compiler *******
+// TAO and the TAO IDL Compiler have been developed by the Center for
+// Distributed Object Computing at Washington University, St. Louis.
+//
+// Information about TAO is available at:
+// http://www.cs.wustl.edu/~schmidt/TAO.html
+
+#include "Messenger_i.h"
+#include <ace/Thread.h>
+#include <iostream>
+
+#include <sstream>
+
+// Implementation skeleton constructor
+Messenger_i::Messenger_i (void)
+{
+}
+
+// Implementation skeleton destructor
+Messenger_i::~Messenger_i (void)
+{
+}
+
+CORBA::Boolean Messenger_i::send_message (
+ const char* /*user_name*/,
+ const char* /*subject*/,
+ char *& message
+ )
+ throw(CORBA::SystemException)
+
+{
+ CORBA::string_free(message);
+
+ std::ostringstream ostr;
+ ostr << "Message handled on thread " << ACE_Thread::self();
+ message = CORBA::string_dup(ostr.str().c_str());
+ return 1;
+}
+
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.h b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.h
new file mode 100644
index 00000000000..af12e09474a
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/Messenger_i.h
@@ -0,0 +1,39 @@
+/* -*- C++ -*- $Id$ */
+
+// ****** Code generated by the The ACE ORB (TAO) IDL Compiler *******
+// TAO and the TAO IDL Compiler have been developed by the Center for
+// Distributed Object Computing at Washington University, St. Louis.
+//
+// Information about TAO is available at:
+// http://www.cs.wustl.edu/~schmidt/TAO.html
+
+#ifndef MESSENGER_I_H_
+#define MESSENGER_I_H_
+
+#include "MessengerS.h"
+
+#if !defined (ACE_LACKS_PRAGMA_ONCE)
+#pragma once
+#endif /* ACE_LACKS_PRAGMA_ONCE */
+
+//Class Messenger_i
+class Messenger_i : public virtual POA_Messenger
+{
+public:
+ //Constructor
+ Messenger_i (void);
+
+ //Destructor
+ virtual ~Messenger_i (void);
+
+virtual CORBA::Boolean send_message (
+ const char * user_name,
+ const char * subject,
+ char *& message
+ )
+ throw(CORBA::SystemException);
+
+};
+
+
+#endif /* MESSENGERI_H_ */
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/README b/TAO/DevGuideExamples/Multithreading/ThreadPool/README
new file mode 100644
index 00000000000..1c63df9bc76
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/README
@@ -0,0 +1,74 @@
+File: DevGuideExamples/Multithreading/ThreadPool/README
+
+
+This directory contains a CORBA example illustrating a simple client
+and a server with an interface Messenger. This example is based on the
+Getting Started example, but adds concurrent request processing
+capabilities to the server using the thread pool concurrency model.
+The server creates 4 threads for the thread pool.
+
+
+How to Run
+----------
+
+To start the server :
+------------------
+./MessengerServer
+
+
+To start the client:
+------------------
+./MessengerClient
+
+
+Tip:
+----
+Run several clients simultaneously against the server. Some client
+requests should be handled by separate threads in the server. To
+verify this, the server returns the thread ID that handled the request
+in the reply message that is printed by the client. You may see the
+same thread used to handle multiple requests, but it is unlikely that
+the same thread will be used to handle all of the requests.
+
+
+Exeuction via Perl Script
+-------------------------
+
+A Perl script has been created to automate the steps shown
+above. This script can be run via the following command:
+
+./run_test.pl
+
+Here is sample output from the Perl script:
+
+Starting MessengerServer
+IOR written to file Messenger.ior
+
+
+Starting 9 MessengerClients.
+The server should use different threads to handle requests.
+
+Reply: Message handled on thread 1620
+Reply: Message handled on thread 1620
+Reply: Message handled on thread 1620
+Reply: Message handled on thread 1868
+Reply: Message handled on thread 1592
+Reply: Message handled on thread 1868
+Reply: Message handled on thread 1300
+Reply: Message handled on thread 1620
+Reply: Message handled on thread 1620
+
+NOTE:
+
+ Since the Perl script starts several clients simultaneously, output
+ may become garbled since each client writes its output to stdout.
+
+NOTE:
+
+ If you run on Windows platform, go to Debug or Release directory to run the
+ script via following command:
+
+ perl ../run_test.pl
+
+
+
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/ThreadPool.mpc b/TAO/DevGuideExamples/Multithreading/ThreadPool/ThreadPool.mpc
new file mode 100644
index 00000000000..f2b2ee0a993
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/ThreadPool.mpc
@@ -0,0 +1,13 @@
+project(Multithreading*Server): taoexe, portableserver, avoids_minimum_corba {
+ Source_Files {
+ Messenger_i.cpp
+ MessengerServer.cpp
+ }
+}
+
+project(Multithreading*Client): taoexe, anytypecode, avoids_minimum_corba {
+ Source_Files {
+ MessengerC.cpp
+ MessengerClient.cpp
+ }
+}
diff --git a/TAO/DevGuideExamples/Multithreading/ThreadPool/run_test.pl b/TAO/DevGuideExamples/Multithreading/ThreadPool/run_test.pl
new file mode 100644
index 00000000000..d81c189697f
--- /dev/null
+++ b/TAO/DevGuideExamples/Multithreading/ThreadPool/run_test.pl
@@ -0,0 +1,113 @@
+eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
+ & eval 'exec perl -S $0 $argv:q'
+ if 0;
+
+use Env (ACE_ROOT);
+use lib "$ACE_ROOT/bin";
+use PerlACE::Run_Test;
+
+$ior = PerlACE::LocalFile ("Messenger.ior");
+unlink $ior;
+
+# start MessengerServer
+
+print STDOUT "Starting MessengerServer\n";
+
+$S = new PerlACE::Process("MessengerServer", "-ORBEndpoint iiop://localhost");
+$S->Spawn();
+
+if (PerlACE::waitforfile_timed ($ior, 5) == -1) {
+ print STDERR "ERROR: cannot find file <$ior>\n";
+ $S->Kill();
+ unlink $ior;
+ exit 1;
+}
+
+# start several MessengerClients
+
+print STDOUT "\n\nStarting 9 MessengerClients.\n";
+print STDOUT "The server should use different threads to handle requests.\n\n";
+
+$C1 = new PerlACE::Process("MessengerClient");
+$C2 = new PerlACE::Process("MessengerClient");
+$C3 = new PerlACE::Process("MessengerClient");
+$C4 = new PerlACE::Process("MessengerClient");
+$C5 = new PerlACE::Process("MessengerClient");
+$C6 = new PerlACE::Process("MessengerClient");
+$C7 = new PerlACE::Process("MessengerClient");
+$C8 = new PerlACE::Process("MessengerClient");
+$C9 = new PerlACE::Process("MessengerClient");
+$C1->Spawn();
+$C2->Spawn();
+$C3->Spawn();
+$C4->Spawn();
+$C5->Spawn();
+$C6->Spawn();
+$C7->Spawn();
+$C8->Spawn();
+$C9->Spawn();
+
+$C1RET = $C1->WaitKill(15);
+$C2RET = $C2->WaitKill(15);
+$C3RET = $C3->WaitKill(15);
+$C4RET = $C4->WaitKill(15);
+$C5RET = $C5->WaitKill(15);
+$C6RET = $C6->WaitKill(15);
+$C7RET = $C7->WaitKill(15);
+$C8RET = $C8->WaitKill(15);
+$C9RET = $C9->WaitKill(15);
+$S->Kill();
+
+# clean-up
+
+unlink $ior;
+
+if ($C1RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C1RET>\n";
+ exit 1 ;
+}
+
+if ($C2RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C2RET>\n";
+ exit 1 ;
+}
+
+if ($C3RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C3RET>\n";
+ exit 1 ;
+}
+
+if ($C4RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C4RET>\n";
+ exit 1 ;
+}
+
+if ($C5RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C5RET>\n";
+ exit 1 ;
+}
+
+if ($C6RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C6RET>\n";
+ exit 1 ;
+}
+
+if ($C7RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C7RET>\n";
+ exit 1 ;
+}
+
+if ($C8RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C8RET>\n";
+ exit 1 ;
+}
+
+if ($C9RET != 0) {
+ print STDERR "ERROR: Client 1 returned <$C9RET>\n";
+ exit 1 ;
+}
+
+exit 0;
+
+
+