summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Forwarding/Servant_Locator.cpp
blob: 74db96f4f2f38b323794f020237bdfcebd6d5f5c (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
// $Id$

// ============================================================================
//
// = LIBRARY
//     TAO/tests/POA/Forwarding/Servant_Locator
//
// = FILENAME
//     Servant_Locator.cpp
//
// = DESCRIPTION
//     Implementation of MyFooServantLocator class , used with a POA
//     having a NON_RETAIN policy.
//
// = AUTHOR
//     Irfan Pyarali
//     Michael Kircher
//
// ============================================================================

#include "Servant_Locator.h"
#include "MyFooServant.h"

ACE_RCSID(Forwarding, Servant_Locator, "$Id$")

MyFooServantLocator::MyFooServantLocator (CORBA::ORB_ptr orb_ptr,
                                          CORBA::Object_ptr forward_to_ptr)
  : orb_var_ (CORBA::ORB::_duplicate (orb_ptr)),
    counter_ (0),
    servant_ptr_ (0),
    forward_to_var_ (CORBA::Object::_duplicate (forward_to_ptr)),
    forwarding_ (0)  // by default do not forward
{
}

MyFooServantLocator::~MyFooServantLocator ()
{
  delete servant_ptr_;
}


PortableServer::Servant
MyFooServantLocator::preinvoke (const PortableServer::ObjectId &oid,
                                PortableServer::POA_ptr poa_ptr,
                                const char *operation,
                                PortableServer::ServantLocator::Cookie &cookie,
                                CORBA::Environment &env)
{
  ACE_UNUSED_ARG (operation);

  if (this->forwarding_ == 0) // do not forward
    {

      // Convert ObjectID to String.

      CORBA::String_var s = PortableServer::ObjectId_to_string (oid);

      // If ObjectID string has a Foo Substring create and return a
      // MyFooServant.

      this->counter_++;

      if (ACE_OS::strstr (s.in (), "Foo") != 0)
        {
          if (this->servant_ptr_ == 0)
            {
              this->servant_ptr_ = new MySecondFooServant (orb_var_.in(),
                                                           this,
                                                           127);

              // Return the servant as the cookie , used as a check when
              // postinvoke is called on this MyFooServantLocator.
              // cookie = servant;
            }
          // reuse the old servant

          return this->servant_ptr_;
        }
      else
        {
          CORBA::Exception *exception = new CORBA::OBJECT_NOT_EXIST (CORBA::COMPLETED_NO);
          env.exception (exception);
          return 0;
        }
    }
  else // now forward, in throwing the ForwardRequest Exception
  {
    // Throw forward exception
    env.exception (new PortableServer::ForwardRequest (this->forward_to_var_.in ()));

    ACE_DEBUG ((LM_DEBUG,"MyFooServantLocator::preinvoke: Threw the ForwardRequest exception.\n"));
    return 0;
  }
}

void
MyFooServantLocator::postinvoke (const PortableServer::ObjectId &oid,
                                 PortableServer::POA_ptr poa,
                                 const char *operation,
                                 PortableServer::ServantLocator::Cookie cookie,
                                 PortableServer::Servant p_servant,
                                 CORBA::Environment &env)
{
  ACE_UNUSED_ARG (oid);
  ACE_UNUSED_ARG (poa);
  ACE_UNUSED_ARG (operation);
  ACE_UNUSED_ARG (cookie);
  ACE_UNUSED_ARG (p_servant);
  ACE_UNUSED_ARG (env);
}

void
MyFooServantLocator::forward (CORBA::Environment &env)
{
  this->forwarding_ = 1;
}