Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / DevGuideExamples / Multithreading / ThreadPool / MessengerServer.cpp
bloba3446d21fac6e6dd96af9b1861994b12f2d458fe
1 #include "Messenger_i.h"
2 #include "ace/Get_Opt.h"
3 #include <iostream>
4 #include <fstream>
5 // 1. Define a "task" class for implenting the thread pool threads.
6 #include "ace/Task.h"
8 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'o':
20 ior_output_file = get_opts.opt_arg ();
21 break;
23 case '?':
24 default:
25 ACE_ERROR_RETURN ((LM_ERROR,
26 "usage: %s "
27 "-o <iorfile>"
28 "\n",
29 argv [0]),
30 -1);
32 // Indicates successful parsing of the command line
33 return 0;
36 class ORB_Task : public ACE_Task_Base
38 public:
39 ORB_Task (CORBA::ORB_ptr orb)
40 : orb_(CORBA::ORB::_duplicate(orb)) { }
41 virtual ~ORB_Task () { }
42 virtual int svc ()
44 this->orb_->run();
45 return 0;
47 private:
48 CORBA::ORB_var orb_;
51 // 2. Establish the number of threads.
52 static const int nthreads = 4;
54 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
56 try {
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();
66 mgr->activate();
68 if (parse_args (argc, argv) != 0)
69 return 1;
71 // Create a servant.
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;
82 iorFile.close();
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);
89 if (retval != 0) {
90 std::cerr << "Failed to activate " << nthreads << " threads." << std::endl;
91 return 1;
94 // 4. Wait for threads to finish.
95 task.wait();
97 // Clean up.
98 orb->destroy();
100 catch(const CORBA::Exception& ex) {
101 std::cerr << "CORBA exception: " << ex << std::endl;
102 return 1;
105 return 0;