summaryrefslogtreecommitdiff
path: root/Monitor/Local_Monitor/Local_Monitor.cpp
blob: c8fc549a441c55694bcd8302283feae3b4fd0e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "Local_Monitor.h"
#include "ace/Reactor.h"
#include "orbsvcs/CosNamingC.h"
#include "tao/ORB_Core.h"
namespace CIAO
{

  namespace RACE
  {

    Local_Monitor::Local_Monitor (const char *,
                                  CORBA::ORB_ptr orb)
      : handler_ (new Event_Handler ()),
        reactor_ (orb->orb_core ()->reactor ()),
        interval_ (10),
        start_time_ (10),
        initialized_ (false)
    {
      this->delays_.length (0);
      if (this->resolve_central_monitor (orb) == 0 &&
          this->register_timer () == 0)
        {
          ACE_DEBUG ((LM_DEBUG, "[CIAO::RACE] Local_Monitor object has been "
                      "successfully initialized.\n"));
          this->initialized_ = true;
        }
      else
        {
          ACE_ERROR ((LM_ERROR, "[CIAO::RACE] ERROR while initializing "
                      "Local_Monitor object\n"));
        }
    }

    int
    Local_Monitor::resolve_central_monitor (CORBA::ORB_ptr orb)
    {
      /// First try to get the object reference of the central monitor.
      try
        {
          /// Trying to get the object reference from the Naming Service.
          CORBA::Object_var naming_obj =
            orb->resolve_initial_references ("NameService");

          if (CORBA::is_nil (naming_obj.in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR, "[CIAO::RACE] Local_Monitor: "
                                 "Unable to resolve the Name Service.\n"),
                                -1);
            }

          CosNaming::NamingContext_var namingContext =
            CosNaming::NamingContext::_narrow (naming_obj.in ());

          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup ("RACE_QoS_Monitor");

          CORBA::Object_var component = namingContext->resolve (name);

          Central_Monitor_var central_monitor =
            Central_Monitor::_narrow (component.in ());

          if (CORBA::is_nil (central_monitor.in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR, "[CIAO::RACE] Local_Monitor: "
                                 "Object reference of the central "
                                 "monitor component is nil!\n") , -1);
            }


          CORBA::Object_var facet = central_monitor->provide_Monitor ();

          this->monitor_ = Execution_Time_Monitor::_narrow (facet.in ());

          if (CORBA::is_nil (this->monitor_.in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR, "[CIAO::RACE] Local_Monitor: "
                                 "Object reference of the Monitor facet of the "
                                 "central monitor component is nil!\n") , -1);
            }
        }

      catch (::CosNaming::NamingContext::NotFound &ex)
        {
          ACE_PRINT_EXCEPTION (ex, "[CIAO::RACE]: Exception caught in "
                               "Local_Monitor::init()");
          return -1;
        }

      catch (::CosNaming::NamingContext::InvalidName &ex)
        {
          ACE_PRINT_EXCEPTION (ex, "[CIAO::RACE}: Exception caught in "
                               "Local_Monitor::init()");
          return -1;
        }

      catch (::CORBA::Exception &ex)
        {
          ACE_PRINT_EXCEPTION (ex, "[CIAO::RACE}: Exception caught in "
                               "Local_Monitor::init()");
          return -1;
        }
      return 0;
    }

    int
    Local_Monitor::register_timer ()
    {
      /// Now set up the "periodic update task".
      this->reactor_->owner (ACE_Thread::self ());
      this->timer_id_ =
        this->reactor_->schedule_timer (this->handler_,
                                        this,
                                        this->start_time_,
                                        this->interval_);
      if (this->timer_id_ < 0)
        {
          ACE_ERROR ((LM_ERROR, "[CIAO::RACE] Local_Monitor: Could not "
                      "register periodic task with the reactor!\n"));
          return -1;
        }
      ACE_DEBUG ((LM_DEBUG, "[CIAO::RACE] Local_Monitor: Registered periodic "
                  "task with id %d.\n", this->timer_id_));
      return 0;
    }

    Local_Monitor::~Local_Monitor ()
    {
      this->reactor_->cancel_timer (this->timer_id_);
      delete this->handler_;
    }

    void
    Local_Monitor::start (const char *id)
    {
      if (!this->initialized_)
        {
          return;
        }
      if (!this->instance_id_.in ())
        {
          this->instance_id_ = CORBA::string_dup (id);
        }
      this->timer_.start ();
    }

    void
    Local_Monitor::stop ()
    {
      if (!this->initialized_)
        {
          return;
        }
	  this->timer_.stop ();
	  ACE_Time_Value time;
	  this->timer_.elapsed_time (time);
      
      this->mutex_.acquire ();

      this->delays_.length (this->delays_.length () + 1);
      this->delays_ [this->delays_.length () - 1] = time.msec ();

      this->mutex_.release ();
    }

    void
    Local_Monitor::dump ()
    {
      if (!this->initialized_)
        {
          return;
        }
      ACE_DEBUG ((LM_DEBUG, "In Trigger::dump %s.\n",
                  this->instance_id_.in ()));

      this->mutex_.acquire ();

      for (CORBA::ULong itr = 0; itr < this->delays_.length (); ++itr)
        {
          ACE_DEBUG ((LM_DEBUG, "%d\n", this->delays_[itr]));
        }

      this->mutex_.release ();
    }

    int
    Local_Monitor::push ()
    {
      if (!this->initialized_)
        {
          return -1;
        }
      try
        {
          this->mutex_.acquire ();
          this->monitor_->push_delays (this->instance_id_.in (),
                                       this->delays_);
          this->delays_.length (0);
          this->mutex_.release ();
          return 0;
        }

      catch (CORBA::Exception &ex)
        {
          ACE_PRINT_EXCEPTION (ex, "[CIAO::RACE] Local_Monitor:: "
                               "Exception caught in push ().\n");
          this->mutex_.release ();
          return -1;
        }
    }


    int
    Event_Handler::handle_timeout (const ACE_Time_Value &,
                                   const void *ref)
    {
      const Local_Monitor *const_monitor =
        reinterpret_cast <const Local_Monitor *> (ref);
      Local_Monitor *monitor = const_cast <Local_Monitor *> (const_monitor);

      return monitor->push ();
    }

  };
};