summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/LoadBalancing/RPS_Monitor.cpp
blob: 834a34cb8bd417aa07619491dd89d49d973bca84 (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
#include "RPS_Monitor.h"
#include "ServerRequestInterceptor.h"
#include "ace/UUID.h"
#include "tao/ORB_Constants.h"
#include "ace/OS_NS_sys_time.h"

ACE_RCSID (LoadBalancing,
           RPS_Monitor,
           "$Id$")


RPS_Monitor::RPS_Monitor (ServerRequestInterceptor * interceptor)
  : location_ (1),
    interceptor_ (interceptor),
    last_time_ (ACE_OS::gettimeofday ()),
    lock_ ()
{
  this->location_.length (1);

  ACE_Utils::UUID_GENERATOR::instance ()->init ();

  ACE_Utils::UUID uuid;
  ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID (uuid);

  this->location_[0].id = CORBA::string_dup (uuid.to_string ()->c_str ());
  this->location_[0].kind = CORBA::string_dup ("UUID");
}

RPS_Monitor::~RPS_Monitor (void)
{
}

CosLoadBalancing::Location *
RPS_Monitor::the_location (void)
{
  CosLoadBalancing::Location * location;
  ACE_NEW_THROW_EX (location,
                    CosLoadBalancing::Location (this->location_),
                    CORBA::NO_MEMORY (
                      CORBA::SystemException::_tao_minor_code (
                        TAO::VMCID,
                        ENOMEM),
                      CORBA::COMPLETED_NO));

  return location;
}

CosLoadBalancing::LoadList *
RPS_Monitor::loads (void)
{
  const ACE_Time_Value current_time = ACE_OS::gettimeofday ();

  ACE_Time_Value elapsed_time;

  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, monitor, this->lock_, 0);

    elapsed_time = current_time - this->last_time_;
    this->last_time_ = current_time;
  }

  const CORBA::Long request_count = this->interceptor_->request_count ();

  CosLoadBalancing::LoadList * tmp;
  ACE_NEW_THROW_EX (tmp,
                    CosLoadBalancing::LoadList (1),
                    CORBA::NO_MEMORY (
                      CORBA::SystemException::_tao_minor_code (
                        TAO::VMCID,
                        ENOMEM),
                      CORBA::COMPLETED_NO));

  CosLoadBalancing::LoadList_var load_list = tmp;

  load_list->length (1);

  load_list[0].id = CosLoadBalancing::RequestsPerSecond;

  // VC 7.1 gives a warning without an explicit cast.
  load_list[0].value =
    static_cast<CORBA::Float> (request_count / elapsed_time.msec () * 1000);

  // Strictly for debugging or
  ACE_DEBUG ((LM_DEBUG, "%f\n", load_list[0].value));

  return load_list._retn ();
}