router.hpp 3.94 KB
Newer Older
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2011 iMatix Corporation
4
    Copyright (c) 2011 VMware, Inc.
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6 7 8 9

    This file is part of 0MQ.

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

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

23 24
#ifndef __ZMQ_ROUTER_HPP_INCLUDED__
#define __ZMQ_ROUTER_HPP_INCLUDED__
25

26 27
#include <map>

28
#include "socket_base.hpp"
29
#include "session_base.hpp"
30
#include "stdint.hpp"
31
#include "blob.hpp"
32
#include "msg.hpp"
33
#include "fq.hpp"
34 35 36 37

namespace zmq
{

38 39 40
    class ctx_t;
    class pipe_t;

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

47 48
        router_t (zmq::ctx_t *parent_, uint32_t tid_, int sid);
        ~router_t ();
49 50

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

61 62 63 64 65
    protected:

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

66 67
    private:

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

71 72
        //  Fair queueing object for inbound pipes.
        fq_t fq;
73

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

        //  Holds the prefetched identity.
82
        msg_t prefetched_id;
83 84

        //  Holds the prefetched message.
85
        msg_t prefetched_msg;
86

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

        struct outpipe_t
        {
92
            zmq::pipe_t *pipe;
93 94
            bool active;
        };
95

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

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

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

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

109 110 111 112
        //  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;

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

118 119
        router_t (const router_t&);
        const router_t &operator = (const router_t&);
120 121
    };

122
    class router_session_t : public session_base_t
123 124 125
    {
    public:

126
        router_session_t (zmq::io_thread_t *io_thread_, bool connect_,
127
            socket_base_t *socket_, const options_t &options_,
128
            const address_t *addr_);
129
        ~router_session_t ();
130 131 132

    private:

133 134
        router_session_t (const router_session_t&);
        const router_session_t &operator = (const router_session_t&);
135 136
    };

137 138 139
}

#endif