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

    This file is part of 0MQ.

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

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

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

#include "stdint.hpp"
25
#include "blob.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
26

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
{
    //  Base class for all objects that participate in inter-thread
    //  communication.

    class object_t
    {
    public:

Martin Sustrik's avatar
Martin Sustrik committed
36
        object_t (class ctx_t *ctx_, uint32_t tid_);
Martin Sustrik's avatar
Martin Sustrik committed
37
        object_t (object_t *parent_);
malosek's avatar
malosek committed
38
        virtual ~object_t ();
Martin Sustrik's avatar
Martin Sustrik committed
39

Martin Sustrik's avatar
Martin Sustrik committed
40
        uint32_t get_tid ();
41
        ctx_t *get_ctx ();
Martin Sustrik's avatar
Martin Sustrik committed
42 43 44 45
        void process_command (struct command_t &cmd_);

    protected:

46 47
        //  Using following function, socket is able to access global
        //  repository of inproc endpoints.
48
        int register_endpoint (const char *addr_, struct endpoint_t &endpoint_);
49
        void unregister_endpoints (class socket_base_t *socket_);
50
        struct endpoint_t find_endpoint (const char *addr_);
51
        void destroy_socket (class socket_base_t *socket_);
52

53
        //  Logs an message.
54
        void log (const char *format_, ...);
55

56
        //  Chooses least loaded I/O thread.
57
        class io_thread_t *choose_io_thread (uint64_t affinity_);
Martin Sustrik's avatar
Martin Sustrik committed
58 59 60 61

        //  Derived object can use these functions to send commands
        //  to other objects.
        void send_stop ();
62 63 64 65
        void send_plug (class own_t *destination_,
            bool inc_seqnum_ = true);
        void send_own (class own_t *destination_,
            class own_t *object_);
66
        void send_attach (class session_t *destination_,
67 68
             struct i_engine *engine_, const blob_t &peer_identity_,
             bool inc_seqnum_ = true);
69
        void send_bind (class own_t *destination_,
70
             class reader_t *in_pipe_, class writer_t *out_pipe_,
71
             const blob_t &peer_identity_, bool inc_seqnum_ = true);
72 73
        void send_activate_reader (class reader_t *destination_);
        void send_activate_writer (class writer_t *destination_,
Martin Hurton's avatar
Martin Hurton committed
74
             uint64_t msgs_read_);
Martin Sustrik's avatar
Martin Sustrik committed
75 76
        void send_pipe_term (class writer_t *destination_);
        void send_pipe_term_ack (class reader_t *destination_);
77 78
        void send_term_req (class own_t *destination_,
            class own_t *object_);
79
        void send_term (class own_t *destination_, int linger_);
80
        void send_term_ack (class own_t *destination_);
81
        void send_reap (class socket_base_t *socket_);
82
        void send_reaped ();
83
        void send_done ();
Martin Sustrik's avatar
Martin Sustrik committed
84 85 86 87

        //  These handlers can be overloaded by the derived objects. They are
        //  called when command arrives from another thread.
        virtual void process_stop ();
88
        virtual void process_plug ();
89
        virtual void process_own (class own_t *object_);
90
        virtual void process_attach (struct i_engine *engine_,
91
            const blob_t &peer_identity_);
92
        virtual void process_bind (class reader_t *in_pipe_,
93
            class writer_t *out_pipe_, const blob_t &peer_identity_);
94 95
        virtual void process_activate_reader ();
        virtual void process_activate_writer (uint64_t msgs_read_);
Martin Sustrik's avatar
Martin Sustrik committed
96 97
        virtual void process_pipe_term ();
        virtual void process_pipe_term_ack ();
98
        virtual void process_term_req (class own_t *object_);
99
        virtual void process_term (int linger_);
100
        virtual void process_term_ack ();
101
        virtual void process_reap (class socket_base_t *socket_);
102
        virtual void process_reaped ();
Martin Sustrik's avatar
Martin Sustrik committed
103

104 105 106 107 108
        //  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 ();

109 110
    private:

111 112
        //  Context provides access to the global state.
        class ctx_t *ctx;
Martin Sustrik's avatar
Martin Sustrik committed
113

Martin Sustrik's avatar
Martin Sustrik committed
114 115
        //  Thread ID of the thread the object belongs to.
        uint32_t tid;
Martin Sustrik's avatar
Martin Sustrik committed
116 117 118 119

        void send_command (command_t &cmd_);

        object_t (const object_t&);
120
        const object_t &operator = (const object_t&);
Martin Sustrik's avatar
Martin Sustrik committed
121 122 123 124 125
    };

}

#endif