object.hpp 4.91 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
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
Martin Sustrik's avatar
Martin Sustrik committed
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.
Martin Sustrik's avatar
Martin Sustrik committed
15

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

Martin Sustrik's avatar
Martin Sustrik committed
20 21
#ifndef __ZMQ_OBJECT_HPP_INCLUDED__
#define __ZMQ_OBJECT_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
22

Martin Hurton's avatar
Martin Hurton committed
23
#include <string>
Martin Sustrik's avatar
Martin Sustrik committed
24 25
#include "stdint.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
26
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
27
{
28 29 30

    struct i_engine;
    struct endpoint_t;
31
    struct pending_connection_t;
32 33 34 35 36 37 38 39
    struct command_t;
    class ctx_t;
    class pipe_t;
    class socket_base_t;
    class session_base_t;
    class io_thread_t;
    class own_t;

Martin Sustrik's avatar
Martin Sustrik committed
40 41 42 43 44 45 46
    //  Base class for all objects that participate in inter-thread
    //  communication.

    class object_t
    {
    public:

47
        object_t (zmq::ctx_t *ctx_, uint32_t tid_);
Martin Sustrik's avatar
Martin Sustrik committed
48
        object_t (object_t *parent_);
malosek's avatar
malosek committed
49
        virtual ~object_t ();
Martin Sustrik's avatar
Martin Sustrik committed
50

Martin Sustrik's avatar
Martin Sustrik committed
51
        uint32_t get_tid ();
52
        void set_tid(uint32_t id);
53
        ctx_t *get_ctx ();
54
        void process_command (zmq::command_t &cmd_);
55
        void send_inproc_connected (zmq::socket_base_t *socket_);
56
        void send_bind (zmq::own_t *destination_, zmq::pipe_t *pipe_, bool inc_seqnum_ = true);
Martin Sustrik's avatar
Martin Sustrik committed
57 58 59

    protected:

60 61
        //  Using following function, socket is able to access global
        //  repository of inproc endpoints.
62 63
        int register_endpoint (const char *addr_,
                const zmq::endpoint_t &endpoint_);
64 65
        void unregister_endpoints (zmq::socket_base_t *socket_);
        zmq::endpoint_t find_endpoint (const char *addr_);
Martin Hurton's avatar
Martin Hurton committed
66 67
        void pend_connection (const std::string &addr_,
                const endpoint_t &endpoint, pipe_t **pipes_);
68
        void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
69

70
        void destroy_socket (zmq::socket_base_t *socket_);
71

72
        //  Logs an message.
73
        void log (const char *format_, ...);
74

75
        //  Chooses least loaded I/O thread.
76
        zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
Martin Sustrik's avatar
Martin Sustrik committed
77 78 79 80

        //  Derived object can use these functions to send commands
        //  to other objects.
        void send_stop ();
81
        void send_plug (zmq::own_t *destination_,
82
            bool inc_seqnum_ = true);
83 84 85 86 87 88
        void send_own (zmq::own_t *destination_,
            zmq::own_t *object_);
        void send_attach (zmq::session_base_t *destination_,
             zmq::i_engine *engine_, bool inc_seqnum_ = true);
        void send_activate_read (zmq::pipe_t *destination_);
        void send_activate_write (zmq::pipe_t *destination_,
Martin Hurton's avatar
Martin Hurton committed
89
             uint64_t msgs_read_);
90 91 92 93 94 95 96 97
        void send_hiccup (zmq::pipe_t *destination_, void *pipe_);
        void send_pipe_term (zmq::pipe_t *destination_);
        void send_pipe_term_ack (zmq::pipe_t *destination_);
        void send_term_req (zmq::own_t *destination_,
            zmq::own_t *object_);
        void send_term (zmq::own_t *destination_, int linger_);
        void send_term_ack (zmq::own_t *destination_);
        void send_reap (zmq::socket_base_t *socket_);
98
        void send_reaped ();
99
        void send_done ();
Martin Sustrik's avatar
Martin Sustrik committed
100

101
        //  These handlers can be overrided by the derived objects. They are
Martin Sustrik's avatar
Martin Sustrik committed
102 103
        //  called when command arrives from another thread.
        virtual void process_stop ();
104
        virtual void process_plug ();
105 106 107
        virtual void process_own (zmq::own_t *object_);
        virtual void process_attach (zmq::i_engine *engine_);
        virtual void process_bind (zmq::pipe_t *pipe_);
108 109
        virtual void process_activate_read ();
        virtual void process_activate_write (uint64_t msgs_read_);
110
        virtual void process_hiccup (void *pipe_);
Martin Sustrik's avatar
Martin Sustrik committed
111 112
        virtual void process_pipe_term ();
        virtual void process_pipe_term_ack ();
113
        virtual void process_term_req (zmq::own_t *object_);
114
        virtual void process_term (int linger_);
115
        virtual void process_term_ack ();
116
        virtual void process_reap (zmq::socket_base_t *socket_);
117
        virtual void process_reaped ();
Martin Sustrik's avatar
Martin Sustrik committed
118

119 120 121 122 123
        //  Special handler called after a command that requires a seqnum
        //  was processed. The implementation should catch up with its counter
        //  of processed commands here.
        virtual void process_seqnum ();

124 125
    private:

126
        //  Context provides access to the global state.
127
        zmq::ctx_t *ctx;
Martin Sustrik's avatar
Martin Sustrik committed
128

Martin Sustrik's avatar
Martin Sustrik committed
129 130
        //  Thread ID of the thread the object belongs to.
        uint32_t tid;
Martin Sustrik's avatar
Martin Sustrik committed
131 132 133 134

        void send_command (command_t &cmd_);

        object_t (const object_t&);
135
        const object_t &operator = (const object_t&);
Martin Sustrik's avatar
Martin Sustrik committed
136 137 138 139 140
    };

}

#endif