summaryrefslogtreecommitdiff
path: root/TAO/tao/Transport_Queueing_Strategies.cpp
blob: 4de33f760dd9f9d70c1fd0d8a60e624d8ba23ec9 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// $Id$

#include "tao/Transport_Queueing_Strategies.h"
#include "tao/Buffering_Constraint_Policy.h"
#include "tao/Stub.h"
#include "tao/debug.h"

#include "ace/Log_Msg.h"
#include "ace/OS_NS_sys_time.h"

ACE_RCSID (tao,
           Transport_Queueing_Strategies,
           "$Id$")


TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  Transport_Queueing_Strategy::~Transport_Queueing_Strategy (void)
  {
  }

// ****************************************************************

  bool
  Default_Transport_Queueing_Strategy::must_queue (bool) const
  {
    return false;
  }

  bool
  Default_Transport_Queueing_Strategy::buffering_constraints_reached (
    TAO_Stub *,
    size_t ,
    size_t ,
    bool &must_flush,
    const ACE_Time_Value &,
    bool &set_timer,
    ACE_Time_Value &) const
  {
    set_timer = false;
    must_flush = false;
    return true;
  }

// ****************************************************************

  bool
  Flush_Transport_Queueing_Strategy::must_queue (bool) const
  {
    return false;
  }

  bool
  Flush_Transport_Queueing_Strategy::buffering_constraints_reached (
    TAO_Stub *,
    size_t ,
    size_t ,
    bool &must_flush,
    const ACE_Time_Value &,
    bool &set_timer,
    ACE_Time_Value &) const
  {
    set_timer = false;
    must_flush = true;
    return true;
  }

// ****************************************************************

  #if (TAO_HAS_BUFFERING_CONSTRAINT_POLICY == 1)

  bool
  Eager_Transport_Queueing_Strategy::must_queue (bool) const
  {
    return true;
  }

  bool
  Eager_Transport_Queueing_Strategy::buffering_constraints_reached (
    TAO_Stub *stub,
    size_t msg_count,
    size_t total_bytes,
    bool &must_flush,
    const ACE_Time_Value &current_deadline,
    bool &set_timer,
    ACE_Time_Value &new_deadline) const
  {
    must_flush = false;
    set_timer = false;

    TAO_Buffering_Constraint_Policy *buffering_constraint_policy = 0;

    ACE_TRY_NEW_ENV
      {
        CORBA::Policy_var bcp_policy =
          stub->get_cached_policy (TAO_CACHED_POLICY_BUFFERING_CONSTRAINT
                                   ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        TAO::BufferingConstraintPolicy_var bcp =
          TAO::BufferingConstraintPolicy::_narrow (bcp_policy.in ()
                                                   ACE_ENV_ARG_PARAMETER);
        ACE_TRY_CHECK;

        buffering_constraint_policy =
          dynamic_cast<TAO_Buffering_Constraint_Policy *> (bcp.in ());

        if (buffering_constraint_policy == 0)
          {
            return true;
          }
      }
    ACE_CATCHANY
      {
        return true;
      }
    ACE_ENDTRY;

    TAO::BufferingConstraint buffering_constraint;
    buffering_constraint_policy->get_buffering_constraint (buffering_constraint);

    if (buffering_constraint.mode == TAO::BUFFER_FLUSH)
      {
        must_flush = true;
        return true;
      }

    bool constraints_reached = false;

    if (ACE_BIT_ENABLED (buffering_constraint.mode,
                         TAO::BUFFER_MESSAGE_COUNT)
        && msg_count >= buffering_constraint.message_count)
      {
        constraints_reached = true;
      }

    if (ACE_BIT_ENABLED (buffering_constraint.mode,
                         TAO::BUFFER_MESSAGE_BYTES)
        && total_bytes >= buffering_constraint.message_bytes)
      {
        constraints_reached = true;
      }

    if (this->timer_check (buffering_constraint,
                           current_deadline,
                           set_timer,
                           new_deadline))
      {
        constraints_reached = true;
      }

    return constraints_reached;
  }

  bool
  Eager_Transport_Queueing_Strategy::timer_check (
    const TAO::BufferingConstraint &buffering_constraint,
    const ACE_Time_Value &current_deadline,
    bool &set_timer,
    ACE_Time_Value &new_deadline) const
  {
    set_timer = false;

    if (!ACE_BIT_ENABLED (buffering_constraint.mode,
                          TAO::BUFFER_TIMEOUT))
      {
        return false;
      }

    // Compute the next deadline...
    ACE_Time_Value now = ACE_OS::gettimeofday ();
    ACE_Time_Value timeout =
      this->time_conversion (buffering_constraint.timeout);
    new_deadline = now + timeout;

    // Check if the new deadline is more stringent, or if the deadline
    // has expired and thus must be reset anyway.
    if (current_deadline > new_deadline
        || current_deadline < now)
      {
        set_timer = true;
      }

    // ... if there is no deadline we don't want to schedule output (the
    // deadline will be set because set_timer is set to 1 in that case).
    // If there is a deadline but but it has not been reached, we
    // don't want to schedule any output either...
    if (current_deadline == ACE_Time_Value::zero
        || current_deadline >= now)
      {
        return false;
      }

    if (TAO_debug_level > 6)
      {
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - TAO_Eager_Buffering_Sync_Strategy::timer_check, "
                    "Now = %u, Current = %u, New = %u\n",
                    now.msec (), current_deadline.msec (),
                    new_deadline.msec ()));
      }

    return true;
  }

  ACE_Time_Value
  Eager_Transport_Queueing_Strategy::time_conversion (
    const TimeBase::TimeT &time) const
  {
    TimeBase::TimeT seconds = time / 10000000u;
    TimeBase::TimeT microseconds = (time % 10000000u) / 10;
    return ACE_Time_Value (ACE_U64_TO_U32 (seconds),
                           ACE_U64_TO_U32 (microseconds));
  }

// ****************************************************************

  bool
  Delayed_Transport_Queueing_Strategy::must_queue (bool queue_empty) const
  {
    // If the queue is empty we want to send immediately
    return !queue_empty;
  }

  #endif /* TAO_HAS_BUFFERING_CONSTRAINT_POLICY == 1 */

}

TAO_END_VERSIONED_NAMESPACE_DECL