Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / Two_Objects / server.cpp
blobb4f0aad2f6f77ee4d70322ed64716dd201e1828f
1 #include "worker.h"
2 #include "Object_Factory_i.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Task.h"
6 int msglen = 100; //default length of reply message is 100 bytes
7 int nthreads = 2;
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:n:l:t:"));
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 'l':
24 msglen = ACE_OS::atoi( get_opts.opt_arg ());
25 break;
27 case 'n':
28 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
31 case '?':
32 default:
33 ACE_ERROR_RETURN ((LM_ERROR,
34 "usage: %s "
35 "-o <iorfile>"
36 " -n <#of threads>"
37 " -l <size of message in bytes>"
38 "\n",
39 argv [0]),
40 -1);
42 // Indicates successful parsing of the command line
43 return 0;
46 int
47 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
49 try
51 // Initialize the ORB
52 CORBA::ORB_var orb =
53 CORBA::ORB_init (argc, argv);
55 // Get initial reference to the Root POA
56 CORBA::Object_var poa_object =
57 orb->resolve_initial_references("RootPOA");
59 // Narrow down to the appropriate type
60 PortableServer::POA_var root_poa =
61 PortableServer::POA::_narrow (poa_object.in ());
63 if (CORBA::is_nil (root_poa.in ()))
64 ACE_ERROR_RETURN ((LM_ERROR,
65 " (%P|%t) Panic: nil RootPOA\n"),
66 1);
68 // Get referencee to the POA manager
69 PortableServer::POAManager_var poa_manager =
70 root_poa->the_POAManager ();
72 // Parse the arguments
73 if (parse_args (argc, argv) != 0)
74 return 1;
76 ACE_DEBUG(( LM_DEBUG, "ior file = %s\t#threads = %d\t"
77 "msglen = %d\n",
78 ior_output_file, nthreads, msglen));
80 // Create the factory servant
81 Object_Factory_i *factory_impl = 0;
82 ACE_NEW_THROW_EX (factory_impl,
83 Object_Factory_i (orb.in (), msglen),
84 CORBA::NO_MEMORY ());
85 PortableServer::ServantBase_var safe (factory_impl);
87 // _this method registers the object withe the POA and returns
88 // an object reference
89 PortableServer::ObjectId_var id =
90 root_poa->activate_object (factory_impl);
92 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
94 Two_Objects_Test::Object_Factory_var factory =
95 Two_Objects_Test::Object_Factory::_narrow (object_act.in ());
97 // Convert the object reference to a string so that it can
98 // be saved in a file and used by clinet programs later
99 CORBA::String_var ior =
100 orb->object_to_string (factory.in ());
102 // If the ior_output_file exists, output the ior to it
103 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
105 if (output_file == 0)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "Cannot open output file for writing IOR: %s",
108 ior_output_file),
111 ACE_OS::fprintf (output_file, "%s", ior.in ());
112 ACE_OS::fclose (output_file);
114 // Activate the POA manager
115 poa_manager->activate ();
117 // Instantiate the specified # of worker threads
118 Worker worker (orb.in ());
120 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
121 nthreads) != 0)
122 ACE_ERROR_RETURN ((LM_ERROR,
123 "Cannot activate server threads\n"),
126 // Wait for all threads to get done
127 worker.thr_mgr ()->wait ();
129 ACE_DEBUG ((LM_DEBUG, "(%P|%t) event loop finished\n"));
131 root_poa->destroy (true, true);
133 orb->destroy ();
135 catch (const CORBA::Exception& ex)
137 ex._tao_print_exception ("Exception caught:");
138 return 1;
141 return 0;