summaryrefslogtreecommitdiff
path: root/protocols/ace/RMCast/Retransmit.cpp
blob: e04cec2f689d4d115c98bdd937a25fb1cfedb0e2 (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
// file      : ace/RMCast/Retransmit.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// cvs-id    : $Id$

#include <ace/OS.h>

#include <ace/RMCast/Retransmit.h>

namespace ACE_RMCast
{
  ACE_Time_Value const tick (0, 50000);

  Retransmit::
  Retransmit ()
  {
  }

  void Retransmit::
  out_start (Out_Element* out)
  {
    Element::out_start (out);

    tracker_mgr_.spawn (track_thunk, this);
  }

  void Retransmit::
  out_stop ()
  {
    tracker_mgr_.cancel_all (1);
    tracker_mgr_.wait ();

    Element::out_stop ();
  }

  void Retransmit::
  send (Message_ptr m)
  {
    if (Data const* data = static_cast<Data const*> (m->find (Data::id)))
    {
      u64 sn (static_cast<SN const*> (m->find (SN::id))->num ());

      Lock l (mutex_);

      queue_.bind (sn, Descr (new Data (*data)));
    }

    out_->send (m);
  }

  void Retransmit::
  recv (Message_ptr m)
  {
    if (NAK const* nak = static_cast<NAK const*> (m->find (NAK::id)))
    {
      Address to (static_cast<To const*> (m->find (To::id))->address ());

      if (nak->address () == to)
      {
        Lock l (mutex_);

        for (NAK::iterator j (const_cast<NAK*> (nak)->begin ());
             !j.done ();
             j.advance ())
        {
          u64* psn;
          j.next (psn);


          Queue::ENTRY* pair;

          if (queue_.find (*psn, pair) == 0)
          {
            //cerr << 5 << "PRTM " << to << " " << pair->ext_id_ << endl;

            Message_ptr m (new Message);

            m->add (Profile_ptr (new SN (pair->ext_id_)));
            m->add (pair->int_id_.data ());

            pair->int_id_.reset ();

            out_->send (m);
          }
          else
          {
            //@@ TODO: message aging
            //
            //cerr << 4 << "message " << *psn << " not available" << endl;
          }
        }
      }
    }
    else
    {
      in_->recv (m);
    }
  }

  ACE_THR_FUNC_RETURN Retransmit::
  track_thunk (void* obj)
  {
    reinterpret_cast<Retransmit*> (obj)->track ();
    return 0;
  }

  void Retransmit::
  track ()
  {
    while (true)
    {
      {
        Lock l (mutex_);

        for (Queue::iterator i (queue_); !i.done ();)
        {
          if ((*i).int_id_.inc () >= 60)
          {
            u64 sn ((*i).ext_id_);
            i.advance ();
            queue_.unbind (sn);
          }
          else
          {
            i.advance ();
          }
        }
      }

      ACE_OS::sleep (tick);
    }
  }
}