summaryrefslogtreecommitdiff
path: root/src/fair_aggregator.cpp
blob: 65bfac00dd0cb6d09db608e8578011bb25e6dae2 (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
/*
    Copyright (c) 2007-2009 FastMQ Inc.

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../include/zs.h"

#include "fair_aggregator.hpp"
#include "err.hpp"
#include "pipe_reader.hpp"
#include "session.hpp"

//  Swaps pipes at specified indices. 
#define swap_pipes(i1_, i2_) \
    std::swap (pipes [i1_], pipes [i2_]);\
    pipes [i1_]->set_index (i1_);\
    pipes [i2_]->set_index (i2_);

zs::fair_aggregator_t::fair_aggregator_t () :
    session (NULL),
    active (0),
    current (0)
{
}

void zs::fair_aggregator_t::set_session (session_t *session_)
{
    zs_assert (!session);
    session = session_;
}

void zs::fair_aggregator_t::shutdown ()
{
    //  No need to deallocate pipes here. They'll be deallocated during the
    //  shutdown of the dispatcher.
    delete this;
}

void zs::fair_aggregator_t::terminate ()
{
    //  Pipe unregisters itself during the call to terminate, so the pipes
    //  list shinks by one in each iteration.
    while (!pipes.empty ())
        pipes [0]->terminate ();

   delete this;
}

zs::fair_aggregator_t::~fair_aggregator_t ()
{
}

void zs::fair_aggregator_t::attach_pipe (pipe_reader_t *pipe_)
{
    //  Associate new pipe with the mux object.
    pipe_->set_mux (this);
    pipes.push_back (pipe_);
    active++;
    if (pipes.size () > active)
        swap_pipes (pipes.size () - 1, active - 1);
    if (active == 1)
        session->revive ();
}

void zs::fair_aggregator_t::detach_pipe (pipe_reader_t *pipe_)
{
    //  Move the pipe from the list of active pipes to the list of idle pipes.
    deactivate (pipe_);
            
    //  Move the pipe to the end of the idle list and remove it.
    swap_pipes (pipe_->get_index (), pipes.size () - 1);
    pipes.pop_back ();
}

bool zs::fair_aggregator_t::empty ()
{
    return pipes.empty ();
}

bool zs::fair_aggregator_t::recv (zs_msg *msg_)
{
    //  Deallocate old content of the message.
    zs_msg_close (msg_);

    //  O(1) fair queueing. Round-robin over the active pipes to get
    //  next message.
    for (pipes_t::size_type i = active; i != 0; i--) {

        //  Update current.
        current = (current + 1) % active;
        
        //  Try to read from current.
        if (pipes [current]->read (msg_))
            return true;
    }

    //  No message is available. Initialise the output parameter
    //  to be a 0-byte message.
    zs_msg_init (msg_);
    return false;
}

void zs::fair_aggregator_t::deactivate (pipe_reader_t *pipe_)
{
    int index = pipe_->get_index ();

    //  Suspend an active pipe.
    swap_pipes (index, active - 1);
    active--;

    //  If the deactiveted pipe is the current one, shift the current one pipe
    //  backwards so that the pipe that replaced the deactiveted one will be
    //  processed immediately rather than skipped.
    if (index == (int) current) {
        index--;
        if (index == -1)
            index = active - 1;
        current = index;
    }
}

void zs::fair_aggregator_t::reactivate (pipe_reader_t *pipe_)
{
    //  Revive an idle pipe.
    swap_pipes (pipe_->get_index (), active);
    active++;
    if (active == 1)
        session->revive ();
}