summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/LoadBalancing/server.cpp
blob: f47efd57aa636ee39055fff4c822bb6b0156cdcf (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
// -*- C++ -*-

// $Id$

#include "orbsvcs/orbsvcs/LoadBalancingC.h"
#include "Hash_ReplicaControl.h"
#include "ace/Get_Opt.h"

int
main (int argc, char *argv[])
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Initialize ORB.
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "", ACE_TRY_ENV);
      ACE_TRY_CHECK;

      const char *balancer_ior = "file://test.ior";
      const char *ior_output = "server.ior";

      // Parse the application options after the ORB has been
      // initialized.
      ACE_Get_Opt options (argc, argv, "k:");
      int c = 0;

      while ((c = options ()) != -1)
        switch (c)
          {
          case 'k':
            balancer_ior = options.optarg;
            break;

          case 'o':
            ior_output = options.optarg;
            break;
          default:
            ACE_ERROR_RETURN ((LM_ERROR,
                               "Usage: %s -k <Load Balancer IOR>\n",
                               argv[0]),
                              -1);
          }

      CORBA::Object_var poa_object =
        orb->resolve_initial_references ("RootPOA");
      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          1);

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      poa_manager->activate (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::Object_var obj =
        orb->string_to_object (balancer_ior, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      LoadBalancing::LoadBalancer_var load_balancer =
        LoadBalancing::LoadBalancer::_narrow (obj.in (),
                                              ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (load_balancer.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("Invalid Load Balancer IOR.\n")),
                          -1);

      CORBA::Object_var group =
        load_balancer->group_identity (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::String_var str =
        orb->object_to_string (group.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      FILE *ior = ACE_OS::fopen (ior_output, "w");
      ACE_OS::fprintf (ior, "%s", str.in ());
      ACE_OS::fclose (ior);

      Hash_ReplicaControl control;
      control.init (orb.in (),
                    load_balancer.in (),
                    ACE_TRY_ENV);
      ACE_TRY_CHECK;

#ifndef ACE_WIN32
      orb->run (ACE_TRY_ENV);
      ACE_TRY_CHECK;
#else
      while (1)
        {
          ACE_Time_Value tv (1, 0);
          orb->run (tv, ACE_TRY_ENV);
          ACE_TRY_CHECK;
          control.handle_timeout (tv, 0);
        }
#endif

      root_poa->destroy (1, 1, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      orb->destroy (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "SYS_EX");
    }
  ACE_ENDTRY;

  return 0;
}