summaryrefslogtreecommitdiff
path: root/tests/Portable_Interceptors/ForwardRequest/Server_Request_Interceptor.cpp
blob: 5a82e33c4d54f4cf118c2c88b92cb5212b7875ad (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
// -*- C++ -*-
// $Id$

#include "tao/AnyTypeCode/TypeCode.h"
#include "Server_Request_Interceptor.h"
#include "tao/ORB_Constants.h"
#include "ace/Log_Msg.h"

Server_Request_Interceptor::Server_Request_Interceptor (void)
  : request_count_ (0)
{
  this->obj_[0] = CORBA::Object::_nil ();
  this->obj_[1] = CORBA::Object::_nil ();
}

Server_Request_Interceptor::~Server_Request_Interceptor (void)
{
}

void
Server_Request_Interceptor::forward_references (
  CORBA::Object_ptr obj1,
  CORBA::Object_ptr obj2)
{
  if (CORBA::is_nil (obj1) || CORBA::is_nil (obj2))
    throw CORBA::INV_OBJREF (
      CORBA::SystemException::_tao_minor_code (
        TAO::VMCID,
        EINVAL),
      CORBA::COMPLETED_NO);

  this->obj_[0] = CORBA::Object::_duplicate (obj1);
  this->obj_[1] = CORBA::Object::_duplicate (obj2);
}

char *
Server_Request_Interceptor::name (void)
{
  return CORBA::string_dup ("Server_Request_Interceptor");
}

void
Server_Request_Interceptor::destroy (void)
{
  CORBA::release (this->obj_[0]);
  CORBA::release (this->obj_[1]);
}

void
Server_Request_Interceptor::receive_request_service_contexts (
    PortableInterceptor::ServerRequestInfo_ptr ri)
{
  this->request_count_++;

  CORBA::Boolean response_expected =
    ri->response_expected ();

  if (!response_expected)   // A one-way request.
    return;

  // Request 1 -- non-forwarded
  // Request 2 -- forwarded by client request interceptor
  // Request 3 -- forwarded by this interception point

  if (this->request_count_ == 3)
    {
      // The client request interceptor should have already forwarded
      // the request to obj_[1], so we re-forward the request back to
      // obj_[0].

      ACE_DEBUG ((LM_DEBUG,
                  "SERVER (%P|%t) Request %d will be forwarded "
                  "to object 1\n"        // "object 1" as in "obj_[0]"
                  "SERVER (%P|%t) via "
                  "receive_request_service_contexts().\n",
                  this->request_count_));

      throw PortableInterceptor::ForwardRequest (this->obj_[0]);
    }
}

void
Server_Request_Interceptor::receive_request (
    PortableInterceptor::ServerRequestInfo_ptr ri)
{
  CORBA::Boolean response_expected =
    ri->response_expected ();

  if (!response_expected)   // A one-way request.
    return;

  // Request 1 -- non-forwarded
  // Request 2 -- forwarded by client request interceptor
  // Request 3 -- forwarded by receive_request_service_contexts()
  // Request 4 -- non-forwarded (give client chance to print result)
  // Request 5 -- forwarded by this interception point
  // Request 6 -- non-forwarded (request 5 gets forwarded here)
  // Request 7 -- throw exception to initiate forwarding from sent_exception

  if (this->request_count_ == 5)
    {
      // This interceptor should have already forwarded the request to
      // obj_[0] so re-forward it to obj_[1].  This will be the last
      // location forward.


      ACE_DEBUG ((LM_DEBUG,
                  "SERVER (%P|%t) Request %d will be forwarded "
                  "to object 2\n"  // "object 2" as in "obj_[1]"
                  "SERVER (%P|%t) via receive_request().\n",
                  this->request_count_ - 1));
      // "request_count_ - 1" is used above since there was a location
      // forward.

      throw PortableInterceptor::ForwardRequest (this->obj_[1]);
    }

  if (this->request_count_ == 7)
    {
      // Throw an exception to force the invocation of send_exception.
      ACE_DEBUG ((LM_DEBUG,
                  "SERVER (%P|%t) OBJ_NOT_EXIST exception thrown for request %d\n"
                  "SERVER (%P|%t) via receive_request().\n",
                  this->request_count_ - 2));

      throw CORBA::OBJECT_NOT_EXIST (
        CORBA::SystemException::_tao_minor_code (
          TAO::VMCID,
          EINVAL),
        CORBA::COMPLETED_NO);
    }
}

void
Server_Request_Interceptor::send_reply (
    PortableInterceptor::ServerRequestInfo_ptr)
{
}

void
Server_Request_Interceptor::send_exception (
    PortableInterceptor::ServerRequestInfo_ptr ri)
{
  CORBA::Any_var exception = ri->sending_exception ();
  CORBA::TypeCode_var tc = exception->type ();
  const char *id = tc->id ();

  CORBA::OBJECT_NOT_EXIST nonexist_exception;

  if (ACE_OS::strcmp (id, nonexist_exception._rep_id ()) == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "SERVER (%P|%t) OBJ_NOT_EXIST exception caught for request %d\n"
                  "SERVER (%P|%t) will be forwarded to object 1\n"
                  "SERVER (%P|%t) via send_exception().\n",
                  this->request_count_ - 2));

      throw PortableInterceptor::ForwardRequest (this->obj_[0]);

    }

}

void
Server_Request_Interceptor::send_other (
    PortableInterceptor::ServerRequestInfo_ptr ri)
{
  CORBA::Boolean response_expected =
    ri->response_expected ();

  if (!response_expected)   // A one-way request.
    return;

  // If we get this far then we should have received a
  // LOCATION_FORWARD reply.

  // This will throw an exception if a location forward has not
  // occured.  If an exception is thrown then something is wrong with
  // the PortableInterceptor::ForwardRequest support.
  CORBA::Object_var forward = ri->forward_reference ();

  if (CORBA::is_nil (forward.in ()))
    throw CORBA::INTERNAL ();
}