Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / DevGuideExamples / Multithreading / ThreadPerConnection / MessengerServer.cpp
blobc5b1690a0385100b112648eee04aa52b5a513adc
1 #include "Messenger_i.h"
2 #include "ace/Get_Opt.h"
3 #include <iostream>
4 #include <fstream>
6 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
8 int
9 parse_args (int argc, ACE_TCHAR *argv[])
11 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
12 int c;
14 while ((c = get_opts ()) != -1)
15 switch (c)
17 case 'o':
18 ior_output_file = get_opts.opt_arg ();
19 break;
21 case '?':
22 default:
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "usage: %s "
25 "-o <iorfile>"
26 "\n",
27 argv [0]),
28 -1);
30 // Indicates successful parsing of the command line
31 return 0;
34 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
36 try {
37 // Initialize the ORB.
38 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
40 //Get reference to the RootPOA.
41 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
42 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
44 // Activate the POAManager.
45 PortableServer::POAManager_var mgr = poa->the_POAManager();
46 mgr->activate();
48 if (parse_args (argc, argv) != 0)
49 return 1;
51 // Create a servant.
52 PortableServer::Servant_var<Messenger_i> messenger_servant = new Messenger_i;
54 // Register the servant with the RootPOA, obtain its object
55 // reference, stringify it, and write it to a file.
56 PortableServer::ObjectId_var oid =
57 poa->activate_object( messenger_servant.in() );
58 CORBA::Object_var messenger_obj = poa->id_to_reference( oid.in() );
59 CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
61 // Output the IOR to the <ior_output_file>
62 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
63 if (output_file == 0)
64 ACE_ERROR_RETURN ((LM_ERROR,
65 "Cannot open output file for writing IOR: %s\n",
66 ior_output_file),
67 1);
68 ACE_OS::fprintf (output_file, "%s", str.in ());
69 ACE_OS::fclose (output_file);
70 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR (ior_output_file) << std::endl;
72 // Accept requests from clients.
73 orb->run();
74 orb->destroy();
76 catch(const CORBA::Exception& ex) {
77 std::cerr << "CORBA exception: " << ex << std::endl;
78 return 1;
81 return 0;