object.hpp 5.01 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_);
Martin Hurton's avatar
Martin Hurton committed
64 65
        int unregister_endpoint (
                const std::string &addr_, socket_base_t *socket_);
66 67
        void unregister_endpoints (zmq::socket_base_t *socket_);
        zmq::endpoint_t find_endpoint (const char *addr_);
Martin Hurton's avatar
Martin Hurton committed
68 69
        void pend_connection (const std::string &addr_,
                const endpoint_t &endpoint, pipe_t **pipes_);
70
        void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
71

72
        void destroy_socket (zmq::socket_base_t *socket_);
73

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

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

        //  Derived object can use these functions to send commands
        //  to other objects.
        void send_stop ();
83
        void send_plug (zmq::own_t *destination_,
84
            bool inc_seqnum_ = true);
85 86 87 88 89 90
        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
91
             uint64_t msgs_read_);
92 93 94 95 96 97 98 99
        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_);
100
        void send_reaped ();
101
        void send_done ();
Martin Sustrik's avatar
Martin Sustrik committed
102

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

121 122 123 124 125
        //  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 ();

126 127
    private:

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

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

        void send_command (command_t &cmd_);

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

}

#endif