Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / TAO / DevGuideExamples / Multithreading / DynamicThreadPool / MessengerServer.cpp
blob40e5462adaf93dcca5236381d6ad1ef6d4821ec8
1 #include "started_pch.h"
3 #include "Messenger_i.h"
4 #include "tao/Dynamic_TP/DTP_POA_Strategy.h"
5 #include <iostream>
6 #include <fstream>
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-o <iorfile>"
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
39 try {
40 // Initialize the ORB.
41 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
43 if (parse_args (argc, argv) != 0)
44 return 1;
46 //Get reference to the RootPOA.
47 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
48 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
50 // Create a configuration structure and set the values
51 TAO_DTP_Definition tp_config;
53 tp_config.min_threads_ = 1; // Set low water mark to 1 thread.
54 tp_config.init_threads_ = 3; // Start 3 threads to start.
55 tp_config.max_threads_ = -1; // Create threads as needed (no limit).
56 tp_config.queue_depth_ = -1; // Allow infinite queue depth.
57 tp_config.stack_size_ = (64 * 1024); // Each thread with 64K stacksize.
58 tp_config.timeout_ = ACE_Time_Value(30,0); // Expire thread that is idle for 30 sec.
60 // Create the dynamic thread pool servant dispatching strategy object, and
61 // hold it in a (local) smart pointer variable.
62 TAO_Intrusive_Ref_Count_Handle<TAO_DTP_POA_Strategy> dtp_strategy =
63 new TAO_DTP_POA_Strategy(&tp_config, false);
65 // Tell the strategy to apply itself to the child poa.
66 if (dtp_strategy->apply_to(poa.in()) == false)
68 ACE_ERROR((LM_ERROR,
69 "Failed to apply CSD strategy to root poa.\n"));
70 return -1;
74 // Activate the POAManager.
75 PortableServer::POAManager_var mgr = poa->the_POAManager();
76 mgr->activate();
78 // Create a servant.
79 PortableServer::Servant_var<Messenger_i> servant = new Messenger_i();
81 // Register the servant with the RootPOA, obtain its object
82 // reference, stringify it, and write it to a file.
83 PortableServer::ObjectId_var oid = poa->activate_object( servant.in() );
84 obj = poa->id_to_reference( oid.in() );
85 CORBA::String_var str = orb->object_to_string( obj.in() );
86 std::ofstream iorFile( ACE_TEXT_ALWAYS_CHAR(ior_output_file) );
87 iorFile << str.in() << std::endl;
88 iorFile.close();
89 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
91 // Accept requests from clients.
92 orb->run();
93 orb->destroy();
95 return 0;
97 catch(const CORBA::Exception& ex) {
98 std::cerr << "CORBA exception: " << ex << std::endl;
101 return 1;