summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Non_Retain_System_Id/test.cpp
blob: 6a5c163120ab6ef309f2ab6fc94b70ce91e0351f (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

//=============================================================================
/**
 *  @file     test.cpp
 *
 *  $Id$
 *
 *   This program verifies that a POA using the NON_RETAIN policy creates
 *   unique system IDs, also that objects created with a USER id policy is
 *   not given a bo
 *
 *  @author  Phil Mesnier
 */
//=============================================================================


#include "testS.h"
#include "ace/SString.h"
#include "tao/PortableServer/POA_Current.h"

class ID_Check_i : public POA_ID_Check
{
public:
  ID_Check_i (int num);
  ~ID_Check_i (void);

  void set_id (int index, PortableServer::ObjectId_var oid);

  CORBA::Boolean check_servant_id (CORBA::Short index);

private:
  int count_;
  PortableServer::ObjectId_var * oids_;
  PortableServer::Current_var current_;
};

ID_Check_i::ID_Check_i (int num)
  : count_ (num),
    oids_ (0),
    current_ ()
{
  int argc = 0;
  char **argv = 0;
  CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
  CORBA::Object_var obj = orb->resolve_initial_references ("POACurrent");
  current_ = PortableServer::Current::_narrow (obj.in());

  oids_ = new PortableServer::ObjectId_var[num];
}

ID_Check_i::~ID_Check_i (void)
{
  delete [] oids_;
}

void
ID_Check_i::set_id (int index, PortableServer::ObjectId_var oid)
{
  this->oids_[index] = oid;
}

CORBA::Boolean
ID_Check_i::check_servant_id (CORBA::Short index)
{
  if (index < 0 || index >= this->count_)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("check_servant_id: requested index outside of range\n")));
      return false;
    }
  PortableServer::ObjectId_var oid = this->current_->get_object_id();

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("check_servant_id: index = %d current objectid ")
              ACE_TEXT ("len %d octets: "),
              index, oid->length()));
  for (size_t i = 0; i < oid->length () && i < 16; i++)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT("%02x "), oid[i]));
  ACE_DEBUG ((LM_DEBUG,ACE_TEXT ("\n")));

  if (oid->length() != this->oids_[index]->length())
    return false;
  for (size_t i = 0; i < oid->length(); i++)
    {
      if (oid[i] != this->oids_[index][i])
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("check_servant_id: id[%d] mismatch at position %d, ")
                      ACE_TEXT ("got %d, expected %d\n"),
                      index, i, oid[i], this->oids_[index][i]));
          return false;
        }
    }
  return true;
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      // Initialize the ORB.
      CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

      // Obtain the RootPOA.
      CORBA::Object_var object =
        orb->resolve_initial_references ("RootPOA");

      // Narrow to POA.
      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (object.in ());

      // Get the POAManager of the RootPOA.
      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager ();

      // Policies for the new POA.
      CORBA::PolicyList policies (4);
      policies.length (3);

      // Request Processing Policy.
      policies[0] =
        root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);

      // Id Uniqueness Policy.
      policies[1] =
        root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);

      // Servant Retention Policy.
      policies[2] =
        root_poa->create_servant_retention_policy (PortableServer::NON_RETAIN);

      // Create POA to host default servant.
      ACE_CString name = "System IDs";
      PortableServer::POA_var sys_id_poa =
        root_poa->create_POA (name.c_str (),
                              poa_manager.in (),
                              policies);

      policies.length (4);
      policies[3] =
        root_poa->create_id_assignment_policy (PortableServer::USER_ID);

      name = "User IDs";
      PortableServer::POA_var user_id_poa =
        root_poa->create_POA (name.c_str (),
                              poa_manager.in (),
                              policies);

      // Destroy policies.
      for (CORBA::ULong i = 0; i < policies.length (); ++i)
        {
          CORBA::Policy_ptr policy = policies[i];
          policy->destroy ();
        }

      int objcount = 30;

      ID_Check_i *servant = new ID_Check_i (objcount);
      sys_id_poa->set_servant (servant);
      user_id_poa->set_servant (servant);

      // Activate POA manager.
      poa_manager->activate ();

      // Create object array.
      ID_Check_var *refs = new ID_Check_var[objcount];
      PortableServer::ObjectId_var obj_id;
      char *user_id_str = new char[10];

      for (int i = 0; i < objcount; i++)
        {
          if (i < objcount / 2)
            {
              object = sys_id_poa->create_reference ("IDL:ID_Check:1.0");
              refs[i] = ID_Check::_narrow (object.in());
              obj_id = sys_id_poa->reference_to_id (object.in());
              servant->set_id (i, obj_id);
            }
          else
            {
              ACE_OS::sprintf (user_id_str, "id %d", i);
              obj_id = PortableServer::string_to_ObjectId (user_id_str);
              object = user_id_poa->create_reference_with_id (obj_id.in(),
                                                              "IDL:ID_Check:1.0");
              refs[i] = ID_Check::_narrow (object.in());
              servant->set_id (i, obj_id);
            }
        }

      int successes = 0;
      for (int i = 0; i < objcount; i++)
        {
          successes += refs[i]->check_servant_id (i) ? 1 : 0;
        }

      orb->destroy ();

      if (successes != objcount)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("FAILURE: only got %d ids correct out of %d\n"),
                      successes, objcount));
          return -1;
        }
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Success: All ids match\n")));
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught");
      return -1;
    }

  return 0;
}