summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Forwarding/MyFooServant.cpp
blob: 6fa73995afb507327a0b963dfed95352d03afa8e (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
// $Id$

//==========================================================================
//
//
// = FILENAME
//     MyFooServant.cpp
//
// = DESCRIPTION
//     This is a simple foo servant implementation
//
// = AUTHOR
//     Irfan Pyarali
//
//==========================================================================

#include "tao/corba.h"
#include "MyFooServant.h"

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

// Constructor
MyFirstFooServant::MyFirstFooServant (CORBA::ORB_ptr orb_ptr,
                                      PortableServer::POA_ptr poa_ptr,
                                      CORBA::Long value,
                                      CORBA::Object_ptr forward_to_ptr)
  : orb_var_ (CORBA::ORB::_duplicate (orb_ptr)),
    poa_var_ (PortableServer::POA::_duplicate (poa_ptr)),
    value_ (value),
    forward_to_var_ (CORBA::Object::_duplicate (forward_to_ptr))
{
  if (CORBA::is_nil (this->forward_to_var_))
    ACE_DEBUG ((LM_DEBUG,
                "POA approach: Forward_to is nil!\n"));
}

// Destructor
MyFirstFooServant::~MyFirstFooServant (void)
{
}

// Return the Default POA of this Servant
PortableServer::POA_ptr
MyFirstFooServant::_default_POA (CORBA::Environment &/*env*/)
{
  return PortableServer::POA::_duplicate (this->poa_var_.in ());
}

// Return this->value
CORBA::Long
MyFirstFooServant::doit (CORBA::Environment &/*env*/)
{
  return this->value_++;
}

void
MyFirstFooServant::shutdown (CORBA::Environment &/*env*/)
{
  this->orb_var_->shutdown();
}


void 
MyFirstFooServant::forward (CORBA::Environment &env)
{
  ACE_DEBUG ((LM_DEBUG,
              "MyFirstFooServant::forward: being called\n"));
  if (!CORBA::is_nil (this->forward_to_var_.in ()))
    {
      PortableServer::ObjectId_var oid =
        this->poa_var_->servant_to_id (this, env);
      
      if (env.exception () != 0)
        return;

      PortableServer::Servant servant = this->poa_var_->_servant ();
      if (servant == 0)
        {
          CORBA::Exception *exception = new Foo::Cannot_Forward;
          env.exception (exception);
          return;
        }
      
      void *ptr = servant->_downcast ("IDL:PortableServer/POA:1.0");
      POA_PortableServer::POA *poa = (POA_PortableServer::POA *) ptr;
      TAO_POA *tao_poa = ACE_dynamic_cast (TAO_POA *, poa);
      
      tao_poa->forward_object (oid.in (),
                               this->forward_to_var_.in (),
                               env);
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "POA approach: Forward_to refenence is nil.\n"));
      CORBA::Exception *exception = new Foo::Cannot_Forward;
      env.exception (exception);
      return;
    }
}

// Second Foo

// Constructor
MySecondFooServant::MySecondFooServant (CORBA::ORB_ptr orb_ptr,
                                        MyFooServantLocator *locator_ptr,
                                        CORBA::Long value)
  : orb_var_ (CORBA::ORB::_duplicate (orb_ptr)),
    locator_ptr_ (locator_ptr),
    value_ (value)
{
}

// Destructor
MySecondFooServant::~MySecondFooServant (void)
{
}


// Return this->value
CORBA::Long
MySecondFooServant::doit (CORBA::Environment &/*env*/)
{
  return this->value_++;
}

void 
MySecondFooServant::forward (CORBA::Environment &env)
{
  // forward the forwarding request to the Servant Locator :-) This is
  // kind of a loop back, but it is correct only the IDL interface can
  // be assumed !!
  this->locator_ptr_->forward (env);  
}


void
MySecondFooServant::shutdown (CORBA::Environment &/*env*/)
{
  this->orb_var_->shutdown();
}