1 #include "Messenger_i.h"
2 #include "ace/Get_Opt.h"
5 // 1. Define a "task" class for implenting the thread pool threads.
8 const ACE_TCHAR
*ior_output_file
= ACE_TEXT ("test.ior");
11 parse_args (int argc
, ACE_TCHAR
*argv
[])
13 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:"));
16 while ((c
= get_opts ()) != -1)
20 ior_output_file
= get_opts
.opt_arg ();
25 ACE_ERROR_RETURN ((LM_ERROR
,
32 // Indicates successful parsing of the command line
36 class ORB_Task
: public ACE_Task_Base
39 ORB_Task (CORBA::ORB_ptr orb
)
40 : orb_(CORBA::ORB::_duplicate(orb
)) { }
41 virtual ~ORB_Task () { }
51 // 2. Establish the number of threads.
52 static const int nthreads
= 4;
54 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
57 // Initialize the ORB.
58 CORBA::ORB_var orb
= CORBA::ORB_init( argc
, argv
);
60 //Get reference to the RootPOA.
61 CORBA::Object_var obj
= orb
->resolve_initial_references( "RootPOA" );
62 PortableServer::POA_var poa
= PortableServer::POA::_narrow( obj
.in() );
64 // Activate the POAManager.
65 PortableServer::POAManager_var mgr
= poa
->the_POAManager();
68 if (parse_args (argc
, argv
) != 0)
72 PortableServer::Servant_var
<Messenger_i
> messenger_servant
= new Messenger_i
;
74 // Register the servant with the RootPOA, obtain its object
75 // reference, stringify it, and write it to a file.
76 PortableServer::ObjectId_var oid
=
77 poa
->activate_object( messenger_servant
.in() );
78 CORBA::Object_var messenger_obj
= poa
->id_to_reference( oid
.in() );
79 CORBA::String_var str
= orb
->object_to_string( messenger_obj
.in() );
80 std::ofstream
iorFile(ACE_TEXT_ALWAYS_CHAR (ior_output_file
));
81 iorFile
<< str
.in() << std::endl
;
83 std::cout
<< "IOR written to file " <<
84 ACE_TEXT_ALWAYS_CHAR (ior_output_file
) << std::endl
;
86 // 3. Create and activate threads for the thread pool.
87 ORB_Task
task (orb
.in());
88 int retval
= task
.activate (THR_NEW_LWP
| THR_JOINABLE
, nthreads
);
90 std::cerr
<< "Failed to activate " << nthreads
<< " threads." << std::endl
;
94 // 4. Wait for threads to finish.
100 catch(const CORBA::Exception
& ex
) {
101 std::cerr
<< "CORBA exception: " << ex
<< std::endl
;