router.hpp 3.73 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
15

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

20 21
#ifndef __ZMQ_ROUTER_HPP_INCLUDED__
#define __ZMQ_ROUTER_HPP_INCLUDED__
22

23 24
#include <map>

25
#include "socket_base.hpp"
26
#include "session_base.hpp"
27
#include "stdint.hpp"
28
#include "blob.hpp"
29
#include "msg.hpp"
30
#include "fq.hpp"
31 32 33 34

namespace zmq
{

35 36 37
    class ctx_t;
    class pipe_t;

38
    //  TODO: This class uses O(n) scheduling. Rewrite it to use O(1) algorithm.
39
    class router_t :
40
        public socket_base_t
41 42 43
    {
    public:

44 45
        router_t (zmq::ctx_t *parent_, uint32_t tid_, int sid);
        ~router_t ();
46 47

        //  Overloads of functions from socket_base_t.
48
        void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
49
        int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
50 51
        int xsend (zmq::msg_t *msg_);
        int xrecv (zmq::msg_t *msg_);
52 53
        bool xhas_in ();
        bool xhas_out ();
54 55
        void xread_activated (zmq::pipe_t *pipe_);
        void xwrite_activated (zmq::pipe_t *pipe_);
56
        void xpipe_terminated (zmq::pipe_t *pipe_);
57

58 59 60 61 62
    protected:

        //  Rollback any message parts that were sent but not yet flushed.
        int rollback ();

63 64
    private:

65 66 67
        //  Receive peer id and update lookup map
        bool identify_peer (pipe_t *pipe_);

68 69
        //  Fair queueing object for inbound pipes.
        fq_t fq;
70

71 72 73 74 75 76
        //  True iff there is a message held in the pre-fetch buffer.
        bool prefetched;

        //  If true, the receiver got the message part with
        //  the peer's identity.
        bool identity_sent;
77 78

        //  Holds the prefetched identity.
79
        msg_t prefetched_id;
80 81

        //  Holds the prefetched message.
82
        msg_t prefetched_msg;
83

84 85 86 87 88
        //  If true, more incoming message parts are expected.
        bool more_in;

        struct outpipe_t
        {
89
            zmq::pipe_t *pipe;
90 91
            bool active;
        };
92

93 94 95
        //  We keep a set of pipes that have not been identified yet.
        std::set <pipe_t*> anonymous_pipes;

96
        //  Outbound pipes indexed by the peer IDs.
97
        typedef std::map <blob_t, outpipe_t> outpipes_t;
98 99
        outpipes_t outpipes;

100
        //  The pipe we are currently writing to.
101
        zmq::pipe_t *current_out;
102 103 104 105

        //  If true, more outgoing message parts are expected.
        bool more_out;

106 107 108 109
        //  Peer ID are generated. It's a simple increment and wrap-over
        //  algorithm. This value is the next ID to use (if not used already).
        uint32_t next_peer_id;

110 111
        // If true, report EAGAIN to the caller instead of silently dropping 
        // the message targeting an unknown peer.
112
        bool mandatory;
113
        bool raw_sock;
114

115 116
        // if true, send an empty message to every connected router peer
        bool probe_router;
117

118 119 120
        // If true, the router will reassign an identity upon encountering a
        // name collision. The new pipe will take the identity, the old pipe
        // will be terminated.
121
        bool handover;
122

123 124
        router_t (const router_t&);
        const router_t &operator = (const router_t&);
125 126 127 128 129
    };

}

#endif