Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Explicit_Event_Loop / server.cpp
blobd8e42eeb1ca85ae9fd7d518042c31c3afe95fbff
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * @author Source code used in TAO has been modified and adapted from thecode provided in the book
7 * @author "Advanced CORBA Programming with C++"by Michi Henning and Steve Vinoski. Copyright1999. Addison-Wesley
8 * @author Reading
9 * @author MA. Used with permission ofAddison-Wesley.Modified for TAO by Mike Moran <mm4@cs.wustl.edu>
11 //=============================================================================
14 #include "server.h"
15 #include "tao/debug.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/OS_NS_stdio.h"
18 #include "ace/OS_NS_unistd.h"
19 #include "ace/OS_NS_time.h"
21 const ACE_TCHAR *ior_output_file = ACE_TEXT("server.ior");
22 int done = 0;
24 int
25 parse_args (int argc, ACE_TCHAR *argv[])
27 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
28 int c;
30 while ((c = get_opts ()) != -1)
31 switch (c)
33 case 'o':
34 ior_output_file = get_opts.opt_arg ();
35 break;
36 case 'd':
37 TAO_debug_level++;
38 break;
39 case '?':
40 default:
41 ACE_ERROR_RETURN ((LM_ERROR,
42 "usage: %s "
43 "-o <iorfile>"
44 "\n",
45 argv [0]),
46 -1);
48 // Indicates successful parsing of the command line
49 return 0;
53 TimeOfDay
54 Time_impl::
55 get_gmt ()
57 time_t time_now = ACE_OS::time (0);
58 struct tm *time_p = ACE_OS::gmtime (&time_now);
60 TimeOfDay tod;
61 tod.hour = time_p->tm_hour;
62 tod.minute = time_p->tm_min;
63 tod.second = time_p->tm_sec;
65 done = 1;
67 return tod;
70 void do_something_else()
72 // Sleep a bit so we don't eat up
73 // a ton of cpu
74 ACE_OS::sleep(3);
77 int
78 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
80 try
82 // Initialize orb
83 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
85 if (parse_args (argc, argv) != 0)
87 return 1;
90 // Get reference to Root POA.
91 CORBA::Object_var obj
92 = orb->resolve_initial_references ("RootPOA");
94 PortableServer::POA_var poa
95 = PortableServer::POA::_narrow (obj.in ());
97 // Activate POA manager.
98 PortableServer::POAManager_var mgr
99 = poa->the_POAManager ();
101 mgr->activate ();
103 // Create an object.
104 Time_impl time_servant;
106 // Write its stringified reference to stdout.
107 PortableServer::ObjectId_var id =
108 poa->activate_object (&time_servant);
110 CORBA::Object_var object = poa->id_to_reference (id.in ());
112 Time_var tm = Time::_narrow (object.in ());
114 CORBA::String_var str = orb->object_to_string (tm.in ());
116 ACE_DEBUG ((LM_DEBUG,
117 "%s\n",
118 str.in ()));
120 if (ior_output_file != 0)
122 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
124 if (output_file == 0)
126 ACE_ERROR_RETURN ((
127 LM_ERROR,
128 "Cannot open output file for writing IOR: %s",
129 ior_output_file
134 ACE_OS::fprintf (output_file,
135 "%s",
136 str.in ());
137 ACE_OS::fclose (output_file);
140 // Explicit Event Loop.
141 while (!done)
143 CORBA::Boolean pending =
144 orb->work_pending ();
146 if (pending)
148 orb->perform_work ();
150 do_something_else ();
153 orb->shutdown ();
154 orb->destroy ();
157 catch (const CORBA::Exception& ex)
159 ex._tao_print_exception ("server: a CORBA exception occurred");
160 return 1;
162 catch (...)
164 ACE_ERROR_RETURN ((LM_ERROR,
165 "%s\n",
166 "client: an unknown exception was caught\n"),
170 return 0;