libzmq master
The Intelligent Transport Layer

mtrie.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2011 250bpm s.r.o.
00003     Copyright (c) 2011 Other contributors as noted in the AUTHORS file
00004 
00005     This file is part of 0MQ.
00006 
00007     0MQ is free software; you can redistribute it and/or modify it under
00008     the terms of the GNU Lesser General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     0MQ is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #ifndef __ZMQ_MTRIE_HPP_INCLUDED__
00022 #define __ZMQ_MTRIE_HPP_INCLUDED__
00023 
00024 #include <stddef.h>
00025 #include <set>
00026 
00027 #include "stdint.hpp"
00028 
00029 namespace zmq
00030 {
00031 
00032     //  Multi-trie. Each node in the trie is a set of pointers to pipes.
00033 
00034     class mtrie_t
00035     {
00036     public:
00037 
00038         mtrie_t ();
00039         ~mtrie_t ();
00040 
00041         //  Add key to the trie. Returns true if it's a new subscription
00042         //  rather than a duplicate.
00043         bool add (unsigned char *prefix_, size_t size_, class pipe_t *pipe_);
00044 
00045         //  Remove all subscriptions for a specific peer from the trie.
00046         //  If there are no subscriptions left on some topics, invoke the
00047         //  supplied callback function.
00048         void rm (class pipe_t *pipe_,
00049             void (*func_) (unsigned char *data_, size_t size_, void *arg_),
00050             void *arg_);
00051 
00052         //  Remove specific subscription from the trie. Return true is it was
00053         //  actually removed rather than de-duplicated.
00054         bool rm (unsigned char *prefix_, size_t size_, class pipe_t *pipe_);
00055 
00056         //  Signal all the matching pipes.
00057         void match (unsigned char *data_, size_t size_,
00058             void (*func_) (class pipe_t *pipe_, void *arg_), void *arg_);
00059 
00060     private:
00061 
00062         bool add_helper (unsigned char *prefix_, size_t size_,
00063             class pipe_t *pipe_);
00064         void rm_helper (class pipe_t *pipe_, unsigned char **buff_,
00065             size_t buffsize_, size_t maxbuffsize_,
00066             void (*func_) (unsigned char *data_, size_t size_, void *arg_),
00067             void *arg_);
00068         bool rm_helper (unsigned char *prefix_, size_t size_,
00069             class pipe_t *pipe_);
00070 
00071         typedef std::set <class pipe_t*> pipes_t;
00072         pipes_t pipes;
00073 
00074         unsigned char min;
00075         unsigned short count;
00076         union {
00077             class mtrie_t *node;
00078             class mtrie_t **table;
00079         } next;
00080 
00081         mtrie_t (const mtrie_t&);
00082         const mtrie_t &operator = (const mtrie_t&);
00083     };
00084 
00085 }
00086 
00087 #endif
00088 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines