summaryrefslogtreecommitdiff
path: root/TAO/tao/Exclusive_TMS.cpp
blob: f7c3e9c936fe18bd315e5d4a84fa3e627f9110e9 (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
#include "tao/Exclusive_TMS.h"
#include "tao/Reply_Dispatcher.h"
#include "tao/debug.h"
#include "tao/Transport.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_Exclusive_TMS::TAO_Exclusive_TMS (TAO_Transport *transport)
  : TAO_Transport_Mux_Strategy (transport),
    request_id_generator_ (0),
    request_id_ (0),
    rd_ (nullptr)
{
}

TAO_Exclusive_TMS::~TAO_Exclusive_TMS ()
{
}

// Generate and return an unique request id for the current
// invocation. We can actually return a predecided ULong, since we
// allow only one invocation over this connection at a time.
CORBA::ULong
TAO_Exclusive_TMS::request_id ()
{
  ++this->request_id_generator_;

  // if TAO_Transport::bidirectional_flag_
  //  ==  1 --> originating side
  //  ==  0 --> other side
  //  == -1 --> no bi-directional connection was negotiated
  // The originating side must have an even request ID, and the other
  // side must have an odd request ID.  Make sure that is the case.
  int const bidir_flag =
    this->transport_->bidirectional_flag ();

  if ((bidir_flag == 1 && ACE_ODD (this->request_id_generator_))
       || (bidir_flag == 0 && ACE_EVEN (this->request_id_generator_)))
    ++this->request_id_generator_;

  if (TAO_debug_level > 4)
    TAOLIB_DEBUG ((LM_DEBUG,
                ACE_TEXT ("TAO (%P|%t) - Exclusive_TMS::request_id - [%d]\n"),
                this->request_id_generator_));

  return this->request_id_generator_;
}

// Bind the handler with the request id.
int
TAO_Exclusive_TMS::bind_dispatcher (CORBA::ULong request_id,
                                    ACE_Intrusive_Auto_Ptr<TAO_Reply_Dispatcher> rd)
{
  this->request_id_ = request_id;
  this->rd_ = rd.get ();

  return 0;
}

bool
TAO_Exclusive_TMS::has_request ()
{
  return this->rd_ != nullptr;
}

int
TAO_Exclusive_TMS::unbind_dispatcher (CORBA::ULong request_id)
{
  if (!this->rd_ || this->request_id_ != request_id)
    return -1;

  this->rd_.release ();

  return 0;
}

int
TAO_Exclusive_TMS::dispatch_reply (TAO_Pluggable_Reply_Params &params)
{
  // Check the ids.
  if (!this->rd_ || this->request_id_ != params.request_id_)
    {
      if (TAO_debug_level > 0)
        TAOLIB_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("TAO (%P|%t) - Exclusive_TMS::dispatch_reply - [%d] != [%d]\n"),
                    this->request_id_, params.request_id_));

      // The return value 0 informs the transport that the mux strategy
      // did not find the right reply handler.
      return 0;
    }

  ACE_Intrusive_Auto_Ptr<TAO_Reply_Dispatcher> rd (this->rd_.get ());
  this->request_id_ = 0; // @@ What is a good value???
  this->rd_.release ();

  // Dispatch the reply.
  // Returns 1 on success, -1 on failure.
  return rd->dispatch_reply (params);
}

int
TAO_Exclusive_TMS::reply_timed_out (CORBA::ULong request_id)
{
  // Check the ids.
  if (!this->rd_ || this->request_id_ != request_id)
    {
      if (TAO_debug_level > 0)
        TAOLIB_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("TAO (%P|%t) - Exclusive_TMS::reply_timed_out - [%d] != [%d]\n"),
                    this->request_id_, request_id));

      // The return value 0 informs the transport that the mux strategy
      // did not find the right reply handler.
      return 0;
    }

  ACE_Intrusive_Auto_Ptr<TAO_Reply_Dispatcher> rd (this->rd_.get ());
  this->request_id_ = 0; // @@ What is a good value???
  this->rd_.release ();

  rd->reply_timed_out ();

  return 0;
}

bool
TAO_Exclusive_TMS::idle_after_send ()
{
  // if there is no reply dispatcher (possible in case of AMI requests)
  // release the transport now
  if (this->rd_ != nullptr)
    {
      return false;
    }
  else
    {
      if (this->transport_ != nullptr)
        {
          // let WS know we're finished
          this->transport_->wait_strategy ()->finished_request ();

          (void) this->transport_->make_idle ();
        }
      return true;
    }
}

bool
TAO_Exclusive_TMS::idle_after_reply ()
{
  // Irrespective of whether we are successful or not we need to
  // return true. If *this* class is not successful in idling the
  // transport no one can.
  if (this->transport_ != nullptr)
  {
    // let WS know we're finished
    this->transport_->wait_strategy ()->finished_request ();

    (void) this->transport_->make_idle ();
  }

  return true;
}

void
TAO_Exclusive_TMS::connection_closed ()
{
  if (this->rd_ != nullptr)
    this->rd_->connection_closed ();
}

TAO_END_VERSIONED_NAMESPACE_DECL