summaryrefslogtreecommitdiff
path: root/protocols/ace/RMCast/Socket.cpp
blob: f5b3c577f025aea03d9f49c1c5306f592cbf7e4e (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// file      : ace/RMCast/Socket.cpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// cvs-id    : $Id$

#include "ace/OS_Memory.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"

#include "ace/Unbounded_Queue.h"

#include "ace/Pipe.h"

#include "Stack.h"
#include "Protocol.h"
#include "Bits.h"

#include "Link.h"
#include "Simulator.h"
#include "Retransmit.h"
#include "Acknowledge.h"


#include "Socket.h"

namespace ACE_RMCast
{
  class Socket_Impl : protected Element
  {
  public:
    ~Socket_Impl ();

    Socket_Impl (Address const& a, bool loop, bool simulator);

  public:
    void
    send_ (void const* buf, size_t s);

    ssize_t
    recv_ (void* buf, size_t s);

    ssize_t
    size_ ();

    ACE_HANDLE
    get_handle_ () const;

  private:
    virtual void
    recv (Message_ptr m);

  private:
    bool loop_;

    u64 sn_; //@@ lock?

    Mutex mutex_;
    Condition cond_;

    ACE_Unbounded_Queue<Message_ptr> queue_;

    ACE_Pipe signal_pipe_;

    ACE_Auto_Ptr<Acknowledge> acknowledge_;
    ACE_Auto_Ptr<Retransmit> retransmit_;
    ACE_Auto_Ptr<Simulator> simulator_;
    ACE_Auto_Ptr<Link> link_;
  };


  Socket_Impl::
  Socket_Impl (Address const& a, bool loop, bool simulator)
      : loop_ (loop),
        sn_ (1),
        cond_ (mutex_)
  {
    signal_pipe_.open ();

    acknowledge_.reset (new Acknowledge ());
    retransmit_.reset (new Retransmit ());
    simulator_.reset (new Simulator ());
    link_.reset (new Link (a, simulator));

    // Start IN stack from top to bottom.
    //
    in_start (0);
    acknowledge_->in_start (this);
    retransmit_->in_start (acknowledge_.get ());
    simulator_->in_start (retransmit_.get ());
    link_->in_start (simulator_.get ());

    // Start OUT stack from bottom up.
    //
    link_->out_start (0);
    simulator_->out_start (link_.get ());
    retransmit_->out_start (simulator_.get ());
    acknowledge_->out_start (retransmit_.get ());
    out_start (acknowledge_.get ());
  }

  Socket_Impl::
  ~Socket_Impl ()
  {
    // Stop OUT stack from top to bottom.
    //
    out_stop ();
    acknowledge_->out_stop ();
    retransmit_->out_stop ();
    simulator_->out_stop ();
    link_->out_stop ();

    // Stop IN stack from bottom up.
    //
    link_->in_stop ();
    simulator_->in_stop ();
    retransmit_->in_stop ();
    acknowledge_->in_stop ();
    in_stop ();
  }


  void Socket_Impl::
  send_ (void const* buf, size_t s)
  {
    Message_ptr m (new Message);

    m->add (Profile_ptr (new SN (sn_++)));
    m->add (Profile_ptr (new Data (buf, s)));

    // Qualification is for VC6 and VxWorks.
    //
    Element::send (m);
  }

  ssize_t Socket_Impl::
  recv_ (void* buf, size_t s)
  {
    Lock l (mutex_);

    while (queue_.is_empty ())
      cond_.wait ();

    Message_ptr m;

    if (queue_.dequeue_head (m) == -1)
      abort ();


    if (queue_.is_empty ())
    {
      // Remove data from the pipe.
      //
      char c;

      if (ACE_OS::read (signal_pipe_.read_handle (), &c, 1) != 1)
      {
        perror ("read: ");
        abort ();
      }
    }


    if (m->find (NoData::id) != 0)
    {
      errno = ENOENT;
      return -1;
    }

    Data const* d = static_cast<Data const*>(m->find (Data::id));

    ssize_t r (static_cast<ssize_t> (d->size () < s ? d->size () : s));

    ACE_OS::memcpy (buf, d->buf (), r);

    return r;
  }

  ssize_t Socket_Impl::
  size_ ()
  {
    Lock l (mutex_);

    while (queue_.is_empty ())
      cond_.wait ();

    // I can't get the head of the queue without actually dequeuing
    // the element.
    //
    Message_ptr m;

    if (queue_.dequeue_head (m) == -1)
      abort ();

    if (queue_.enqueue_head (m) == -1)
      abort ();

    if (m->find (NoData::id) != 0)
    {
      errno = ENOENT;
      return -1;
    }

    Data const* d = static_cast<Data const*>(m->find (Data::id));

    return static_cast<ssize_t> (d->size ());
  }

  ACE_HANDLE Socket_Impl::
  get_handle_ () const
  {
    return signal_pipe_.read_handle ();
  }


  void Socket_Impl::
  recv (Message_ptr m)
  {
    if (m->find (Data::id) != 0 || m->find (NoData::id) != 0)
    {
      if (!loop_)
      {
        Address to (static_cast<To const*> (m->find (To::id))->address ());

        Address from (
          static_cast<From const*> (m->find (From::id))->address ());

        if (to == from)
          return;
      }

      Lock l (mutex_);

      bool signal (queue_.is_empty ());

      queue_.enqueue_tail (m);

      if (signal)
      {
        // Also write to the pipe.
        //
        char c;

        if (ACE_OS::write (signal_pipe_.write_handle (), &c, 1) != 1)
        {
          perror ("write: ");
          abort ();
        }

        cond_.signal ();
      }

    }
  }


  // Socket
  //
  //
  Socket::
  ~Socket ()
  {
  }

  Socket::
  Socket (Address const& a, bool loop, bool simulator)
      : impl_ (new Socket_Impl (a, loop, simulator))
  {
  }

  void Socket::
  send (void const* buf, size_t s)
  {
    impl_->send_ (buf, s);
  }

  ssize_t Socket::
  recv (void* buf, size_t s)
  {
    return impl_->recv_ (buf, s);
  }

  ssize_t Socket::
  size ()
  {
    return impl_->size_ ();
  }

  ACE_HANDLE Socket::
  get_handle () const
  {
    return impl_->get_handle_ ();
  }
}