summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/LoadBalancing/LoadMonitor/CPU/client.cpp
blob: 286f7e8cb1c4a9713a99dae599957d10b3de792f (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
// $Id$

#include "orbsvcs/CosLoadBalancingC.h"
#include "orbsvcs/PortableGroup/PG_Operators.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_unistd.h"

ACE_RCSID (CPU,
	   client,
	   "$Id$")


const char * location = "MyLocation";

const int MAX_RETRIES = 10;
const CosLoadBalancing::LoadId LOAD_ID = CosLoadBalancing::LoadAverage;

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "l:");

  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'l':
        location = get_opts.opt_arg ();
        break;

      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Usage:  %s -l <ior>\n",
                           argv[0]),
                          -1);
      }

  return 0;
}

void
check_loads (const CosLoadBalancing::LoadList & loads
             ACE_ENV_ARG_DECL)
{
  if (loads.length () != 1)
    {
      ACE_ERROR ((LM_ERROR,
                  "ERROR: Load list length is not equal to one.\n"));

      ACE_THROW (CORBA::INTERNAL ());
    }

  if (loads[0].id != LOAD_ID
      || loads[0].value < 0)
    {
      ACE_ERROR ((LM_ERROR,
                  "ERROR: Returned load is not a CPU load.\n"));

      ACE_THROW (CORBA::INTERNAL ());
    }
}

int
main (int argc, char *argv[])
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      CORBA::ORB_var orb =
	CORBA::ORB_init (argc,
                         argv,
                         ""
                         ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Obtain a reference to the CodecFactory.
      CORBA::Object_var obj =
	orb->resolve_initial_references ("LoadManager"
					 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CosLoadBalancing::LoadManager_var lm =
	CosLoadBalancing::LoadManager::_narrow (obj.in ()
                                                ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CosLoadBalancing::Location the_location (1);
      the_location.length (1);

      the_location[0].id = CORBA::string_dup (location);

      // Attempt to get loads from the LoadManager.
      ACE_DEBUG ((LM_INFO,
                  "\n"
                  "Retrieving loads from LoadManager ..."));

      CosLoadBalancing::LoadList_var loads;

      CORBA::Boolean retrieved_load = 0;

      // Try a few times until a load is capable of being retrieved.
      for (int i = 0; i < MAX_RETRIES && retrieved_load == 0; ++i)
        {
          ACE_TRY_EX (foo)
            {
              loads = lm->get_loads (the_location
                                     ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK_EX (foo);

              retrieved_load = 1;
            }
          ACE_CATCHANY
            {
              ACE_DEBUG ((LM_DEBUG, ".")); // Show some progress.

              // Give some time for loads to be reported to the
              // LoadManager.
              ACE_OS::sleep (2);
            }
          ACE_ENDTRY;
          ACE_TRY_CHECK;
        }

      if (retrieved_load == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "\nERROR: Unable to retrieve loads "
                           "from LoadManager.\n"),
                          -1);
      else
        ACE_DEBUG ((LM_INFO,
                    " DONE\n"));

      ::check_loads (loads.in ()
                     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Attempt to get loads directly from the LoadMonitor.
      CosLoadBalancing::LoadMonitor_var monitor =
        lm->get_load_monitor (the_location
                              ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      CosLoadBalancing::Location_var cpu_mon_location =
        monitor->the_location (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (cpu_mon_location.in () != the_location)
        {
          ACE_ERROR ((LM_ERROR,
                      "ERROR: Mismatched CPU load monitor location\n"));

          ACE_TRY_THROW (CORBA::INTERNAL ());
        }

      ACE_DEBUG ((LM_INFO,
                  "Retrieving loads directly from LoadMonitor ..."));

      loads = monitor->loads (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_INFO,
                  " DONE\n"));

      ::check_loads (loads.in ()
                     ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
			   "CPU Load Monitor test:");
      return -1;
    }
  ACE_ENDTRY;

  ACE_DEBUG ((LM_INFO, "CPU Load Monitor test passed.\n\n"));

  return 0;
}