router.hpp 3.77 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
        //  Overrides 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_);
Stoian Ivanov's avatar
Stoian Ivanov committed
57

58 59 60 61
    protected:

        //  Rollback any message parts that were sent but not yet flushed.
        int rollback ();
62
        blob_t get_credential () const;
63

64 65
    private:

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

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

72 73 74 75 76 77
        //  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;
78 79

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

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

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

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

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

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

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

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

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

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

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

119 120 121
        // 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.
122
        bool handover;
123

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

}

#endif