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

    This file is part of 0MQ.

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

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

Martin Sustrik's avatar
Martin Sustrik committed
22 23
#ifndef __ZMQ_OBJECT_HPP_INCLUDED__
#define __ZMQ_OBJECT_HPP_INCLUDED__
Martin Sustrik's avatar
Martin Sustrik committed
24 25 26

#include "stdint.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
27
namespace zmq
Martin Sustrik's avatar
Martin Sustrik committed
28
{
29 30 31 32 33 34 35 36 37 38 39

    struct i_engine;
    struct endpoint_t;
    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
        ctx_t *get_ctx ();
53
        void process_command (zmq::command_t &cmd_);
Martin Sustrik's avatar
Martin Sustrik committed
54 55 56

    protected:

57 58
        //  Using following function, socket is able to access global
        //  repository of inproc endpoints.
59 60 61 62
        int register_endpoint (const char *addr_, zmq::endpoint_t &endpoint_);
        void unregister_endpoints (zmq::socket_base_t *socket_);
        zmq::endpoint_t find_endpoint (const char *addr_);
        void destroy_socket (zmq::socket_base_t *socket_);
63

64
        //  Logs an message.
65
        void log (const char *format_, ...);
66

67
        //  Chooses least loaded I/O thread.
68
        zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
Martin Sustrik's avatar
Martin Sustrik committed
69 70 71 72

        //  Derived object can use these functions to send commands
        //  to other objects.
        void send_stop ();
73
        void send_plug (zmq::own_t *destination_,
74
            bool inc_seqnum_ = true);
75 76 77 78 79
        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_bind (zmq::own_t *destination_, zmq::pipe_t *pipe_,
80
             bool inc_seqnum_ = true);
81 82
        void send_activate_read (zmq::pipe_t *destination_);
        void send_activate_write (zmq::pipe_t *destination_,
Martin Hurton's avatar
Martin Hurton committed
83
             uint64_t msgs_read_);
84 85 86 87 88 89 90 91
        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_);
92
        void send_reaped ();
93
        void send_done ();
Martin Sustrik's avatar
Martin Sustrik committed
94 95 96 97

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

113 114 115 116 117
        //  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 ();

118 119
    private:

120
        //  Context provides access to the global state.
121
        zmq::ctx_t *ctx;
Martin Sustrik's avatar
Martin Sustrik committed
122

Martin Sustrik's avatar
Martin Sustrik committed
123 124
        //  Thread ID of the thread the object belongs to.
        uint32_t tid;
Martin Sustrik's avatar
Martin Sustrik committed
125 126 127 128

        void send_command (command_t &cmd_);

        object_t (const object_t&);
129
        const object_t &operator = (const object_t&);
Martin Sustrik's avatar
Martin Sustrik committed
130 131 132 133 134
    };

}

#endif