NodeRepository.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __NodeRepository_hpp__
00041 #define __NodeRepository_hpp__
00042
00043 #include <map>
00044 #include <list>
00045 #include <ctime>
00046 #include "boost/noncopyable.hpp"
00047 #include "boost/shared_ptr.hpp"
00048 #include "boost/shared_container_iterator.hpp"
00049 #include <boost/mpl/for_each.hpp>
00050 #include "mw/common/Subject.h"
00051 #include "mw/nl/awds/MAC.h"
00052 #include "mw/nl/awds/Node.h"
00053 #include "mw/nl/awds/Attributes.h"
00054 #include "mw/nl/awds/ComparableAttributeSet.h"
00055
00056 namespace famouso {
00057 namespace mw {
00058 namespace nl {
00059 namespace awds {
00060
00062 typedef ComparableAttributeSet<AWDSAttributeSet::type> Attributes;
00063
00065 typedef detail::Node<AWDSAttributeSet::type> Node;
00066
00072 class NodeRepository: boost::noncopyable {
00073
00074 public:
00075
00077 typedef NodeRepository type;
00078
00079 typedef boost::shared_container_iterator<std::list<Node::type> > NodeIterator;
00080
00083 typedef famouso::mw::Subject SNN;
00084
00085 private:
00086
00089 class Subscriber: boost::noncopyable {
00090
00091 clock_t _time;
00093 Subscriber() :
00094 _time(std::clock()) {
00095 }
00096
00097 public:
00098
00101 typedef boost::shared_ptr<Subscriber> type;
00102
00110 static type Create(Attributes::type a, FlowId fId = 0) {
00111 type res = type(new Subscriber());
00112 res->attribs = a;
00113 res->flowId = fId;
00114 return res;
00115 }
00116
00121 int elapsed() const {
00122 return static_cast<int> (std::clock() - _time);
00123 }
00124
00125 Attributes::type attribs;
00126 FlowId flowId;
00127 };
00128
00135 template< class AttrSet >
00136 class attr_finder {
00138 typedef typename AttrSet::sequence Seq;
00139
00140 AttrSet &_res;
00141 Attributes::type &_pub,
00142 &_sub;
00150 attr_finder(AttrSet &res, Attributes::type &pub, Attributes::type &sub) :
00151 _res(res), _pub(pub), _sub(sub) {
00152 }
00153
00154 public:
00155
00162 static void apply(AttrSet &res, Attributes::type &pub, Attributes::type &sub) {
00163 boost::mpl::for_each<Seq>(attr_finder(res, pub, sub));
00164 }
00165
00170 template< class Attrib >
00171 void operator()(Attrib a) {
00173 typedef typename Attrib::comparator cmp;
00174
00175 if (!Attrib::highDensity)
00176 return;
00177
00178
00179 Attrib *r = _res.template find_rt<Attrib> ();
00180
00181 Attrib *p = _pub->template find<Attrib> ();
00182
00183 Attrib *s = _sub->template find<Attrib> ();
00184
00185 if (p) {
00186
00187 if (s && cmp::apply_runtime(p->getValue(), s->getValue()))
00188
00189 r->setValue(s->getValue());
00190 else
00191
00192 r->setValue(p->getValue());
00193 } else if (s) {
00194
00195 r->setValue(s->getValue());
00196 } else
00197 log::emit<AWDS>() << "Missing attribute with id " << (int) Attrib::id << log::endl;
00198 }
00199 };
00200
00202 typedef std::list<Node::type> NodeList;
00203
00206 typedef std::map<Node::type, Subscriber::type, Node::comp> NodeSubscriberMap;
00207
00210 typedef std::map<SNN, boost::shared_ptr<NodeSubscriberMap> > SubscriberMap;
00211
00214 typedef std::map<SNN, Attributes::type> PublisherMap;
00215
00218
00219
00221 NodeRepository();
00222
00223 Subscriber::type findSub(Node::type &node, SNN subject);
00224
00225 public:
00226
00231 static type& getInstance();
00232
00239 Node::type find(MAC &mac);
00240
00249 std::pair<NodeIterator, NodeIterator> find(SNN subject);
00250
00257 FlowId flowid(Node::type &node, SNN subject);
00258
00265 int elapsed(Node::type &node, SNN subject);
00266
00275 template< class AttrSet >
00276 void find(Node::type &node, SNN subject, AttrSet &cas) {
00277 Attributes::type subAttr, pubAttr = _snnAttribs[subject];
00278
00279
00280 Subscriber::type s = findSub(node, subject);
00281 if (s)
00282 subAttr = s->attribs;
00283
00284 if (pubAttr && subAttr)
00285 attr_finder<AttrSet>::apply(cas, pubAttr, subAttr);
00286 else
00287 log::emit<AWDS>() << "Missing attributes of subscriber or publisher." << log::endl;
00288 }
00289
00295 void remove(Node::type &node);
00296
00303 void remove(SNN &subject);
00304
00313 void reg(Node::type &node, SNN &subject, Attributes::type &attribs);
00314
00320 void reg(SNN subject, Attributes::type &attribs);
00321
00326 void unreg(Node::type &node);
00327
00333 void unreg(Node::type &node, SNN subject);
00334
00341 void update(Node::type &node, SNN subject, FlowId fId);
00342
00343 private:
00344 SubscriberMap _snnmap;
00345 NodeList _nodes;
00346 PublisherMap _snnAttribs;
00347 };
00348
00349 }
00350 }
00351 }
00352 }
00353 #endif