Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / AMH / Sink_Server / AMH_Servant.cpp
blobfa2aaabfe7fa17f40a1aa9d3389591c66f28851a
2 #include "AMH_Servant.h"
3 #include "Timer_Handler.h"
5 #include "ace/Reactor.h"
6 #include "ace/Get_Opt.h"
9 AMH_Servant::AMH_Servant (CORBA::ORB_ptr orb)
10 : sleep_time_ (0)
12 // @@ Mayur, why do you obtain the reactor each time this method is
13 // called? Why not just cache it once in the constructor.
14 // Furthermore, you don't appear to need the ORB pseudo-reference
15 // anything other than to obtain the Reactor. Why not just
16 // remove the cached ORB pseudo-reference altogether?
18 // Mayur: Good point. Never crossed my mind :)
19 this->reactor_ = orb->orb_core ()->reactor ();
22 AMH_Servant::~AMH_Servant (void)
26 int
27 AMH_Servant::parse_args (int &argc, ACE_TCHAR **argv)
29 // *** To get correct behaviour, SET POSIXLY_CORECT=1 on Linux
30 // systems!!! ***
31 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("s:"));
32 int c;
34 int count_argv = 0;
35 while ((c = get_opts ()) != -1)
37 ++count_argv;
38 switch (c)
40 case 's':
42 int time = ACE_OS::atoi (get_opts.opt_arg ());
43 if (time >= 0)
44 this->sleep_time_ = time;
46 // Remove the option '-s' from argv []
47 // to avoid any confusion that might result.
49 for (int i = count_argv; i < argc; i++)
50 argv [i] = argv [i+2];
53 // Decrement the value of this->argc_ to reflect the removal
54 // of '-s' option.
55 argc = argc - 2;
57 return 1;
60 case '?':
61 default:
62 // Don't do anything.
63 break;
66 return 0;
69 // Note: Only the following three methods will be called in the event
70 // loop. If we make sure these three methods are thread safe, the
71 // entire class is thread-safe.
73 void
74 AMH_Servant::test_method (Test::AMH_RoundtripResponseHandler_ptr _tao_rh,
75 Test::Timestamp send_time)
77 try
79 // @@ Mayur, the below Timer_Handler instance will leak if your
80 // schedule_timer() call below fails (which you do not check
81 // for). If the schedule_timer() call fails, then you should
82 // delete() the Timer_Handler instance.
84 // Done.
86 // Handler will 'kill' itself when it is done.
87 Timer_Handler *handler = new Timer_Handler (_tao_rh,
88 send_time);
90 // We assume the schedule_timer method is thread-safe
91 long l = this->reactor_->schedule_timer (handler,
92 (void *) 0,
93 ACE_Time_Value (0, this->sleep_time_));
95 if (l == -1) //schedule timer failed
97 // free memory
98 delete handler;
100 // send a no-op to the client
101 _tao_rh->test_method (static_cast<Test::Timestamp> (0));
103 // just in case we add code later on after this if stmt
104 return;
107 catch (const CORBA::Exception& ex)
109 ex._tao_print_exception ("Exception in start_test\n");
112 // Footnote: This method is thread-safe:
113 // - We have no common state (and don't alter any).
114 // - We do things only through function calls which means we are using
115 // the thread-specific stack for that function call.
118 void
119 AMH_Servant::start_test (Test::AMH_RoundtripResponseHandler_ptr _tao_rh)
123 _tao_rh->start_test ();
125 catch (const CORBA::Exception& ex)
127 ex._tao_print_exception ("Exception in start_test\n");
131 void
132 AMH_Servant::end_test (Test::AMH_RoundtripResponseHandler_ptr _tao_rh)
136 _tao_rh->end_test ();
138 catch (const CORBA::Exception& ex)
140 ex._tao_print_exception ("Exception in end_test\n");
145 //Global Footnote: This whole class seems to be thread-safe (and look ma, no locks! :)