summaryrefslogtreecommitdiff
path: root/protocols/ace/RMCast/RMCast_Reordering.cpp
blob: 1aebf27d8ee68d78aea200cdafcb5876ef89eafa (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
//
// $Id$
//

#include "RMCast_Reordering.h"
#include "RMCast_Proxy.h"
#include "ace/Guard_T.h"
#include "ace/Message_Block.h"

#if !defined (__ACE_INLINE__)
# include "RMCast_Reordering.i"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID(RMCast, RMCast_Reordering, "$Id$")

ACE_RMCast_Reordering::~ACE_RMCast_Reordering (void)
{
}

int
ACE_RMCast_Reordering::close (void)
{
  Messages_Iterator i = this->messages_.begin ();
  Messages_Iterator end = this->messages_.end ();

  while (i != end)
    {
      ACE_Message_Block::release ((*i).item ().payload);
      this->messages_.unbind ((*i).key ());
      i = this->messages_.begin ();
    }
  return this->ACE_RMCast_Module::close ();
}

int
ACE_RMCast_Reordering::data (ACE_RMCast::Data &data)
{
  int must_ack = 0;
  int result = 0;
  ACE_RMCast::Ack ack;

  //ACE_DEBUG ((LM_DEBUG, "Received message (%d)\n", data.sequence_number));
  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->mutex_, -1);

    if (data.sequence_number < this->next_expected_)
      {
        // Old message.  Ack with the current status (look at the end
        // of this block).
        must_ack = 1;

        //ACE_DEBUG ((LM_DEBUG, ".... old message is ignored\n"));
      }

    else if (data.sequence_number == this->next_expected_)
      {
        //ACE_DEBUG ((LM_DEBUG, ".... message is in order, received\n"));

        // Accept the message, the current thread will dispatch it, so
        // it is marked as accepted (using the <next_expected> field).
        // Any other thread will not push that message because now it
        // is "old".

        this->next_expected_++;

        // Right message, process as many messages as possible from
        // the queue, then ack the right level...

        // NOTE: we cannot release the mutex while dispatching
        // events, otherwise: how do we stop other threads from
        // delivering messages out of order?  I.E. what if the
        // next thread receives the next message?
        if (this->next () != 0)
          {
            result = this->next ()->data (data);
          }

        // After delivering one message there may be more messages
        // pending
        if (result == 0)
          result = this->push_queued_messages ();

        //@@ This should be strategized, for example, only Ack if
        //   there is a message out of order or something, otherwise
        //   continue with happiness.  That works well for "optimistic
        //   models".
        must_ack = 1;
      }

    else
      {
        //ACE_DEBUG ((LM_DEBUG, ".... message out of sequence, saved\n"));

        // Out of sequence.
        if (this->highest_received_ < data.sequence_number)
          {
            this->highest_received_ = data.sequence_number;
          }
        ACE_RMCast::Data new_data = data;
        new_data.payload = ACE_Message_Block::duplicate (data.payload);
        (void) this->messages_.bind (data.sequence_number, new_data);
        // re-ack, otherwise save it and ack.
      }

    ack.next_expected = this->next_expected_;
    ack.highest_received = this->highest_received_;
  }

  if (must_ack && data.source != 0)
    (void) data.source->reply_ack (ack);

  return result;
}

int
ACE_RMCast_Reordering::ack_join (ACE_RMCast::Ack_Join &ack_join)
{
  //ACE_DEBUG ((LM_DEBUG, "RMCast_Reordering::ack_join - <%d,%d>\n",
  //            this->next_expected_,
  //            ack_join.next_sequence_number));

  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
    if (this->next_expected_ >= ack_join.next_sequence_number)
      {
        // Nothing to do in this case...
        return 0;
      }

    Messages_Iterator i = this->messages_.begin ();
    Messages_Iterator end = this->messages_.end ();

    while (i != end
           && (*i).key () < ack_join.next_sequence_number)
      {
        ACE_Message_Block::release ((*i).item ().payload);
        this->messages_.unbind ((*i).key ());
        i = this->messages_.begin ();
      }

    this->next_expected_ = ack_join.next_sequence_number;
    if (this->highest_received_ < ack_join.next_sequence_number)
      this->highest_received_ = ack_join.next_sequence_number;

    this->push_queued_messages ();
  }

  return 0;
}

int
ACE_RMCast_Reordering::push_queued_messages (void)
{
  Messages_Iterator i = this->messages_.begin ();
  Messages_Iterator end = this->messages_.end ();

  while (i != end
         && (*i).key () == this->next_expected_)
    {
      int r = 0;
      if (this->next () != 0)
        {
          ACE_RMCast::Data data = (*i).item ();
          r = this->next ()->data (data);
        }

      ACE_Message_Block::release ((*i).item ().payload);
      this->messages_.unbind ((*i).key ());
      i = this->messages_.begin ();
      this->next_expected_++;
      if (r != 0)
        return r;
    }
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */