INET Framework for OMNeT++/OMNEST
Blackboard.h
Go to the documentation of this file.
00001 // -*- mode:c++ -*-
00002 //
00003 // Copyright (C) 2004 Andras Varga
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 
00020 #ifndef __INET_BLACKBOARD_H
00021 #define __INET_BLACKBOARD_H
00022 
00023 #include <map>
00024 #include <vector>
00025 #include <omnetpp.h>
00026 #include "INETDefs.h"
00027 
00028 
00029 class BlackboardAccess;
00030 
00031 
00162 class INET_API Blackboard : public cSimpleModule
00163 {
00164   public:
00165     typedef std::vector<BlackboardAccess *> SubscriberVector;
00166 
00170     class BBItem
00171     {
00172       private:
00173         friend class Blackboard;
00174         cPolymorphic *_item;
00175         std::string _label;
00176         SubscriberVector subscribers;
00177       public:
00179         const char *getLabel()  {return _label.c_str();}
00181         cPolymorphic *getData()  {return _item;}
00183         const cPolymorphic *getData() const  {return _item;}
00184     };
00185 
00186   protected:
00188     bool coreDebug;
00189 
00190     class Iterator;
00191     friend class Iterator;
00192 
00193     // blackboard items, with their subscriber lists
00194     typedef std::map<std::string, BBItem*> ContentsMap;
00195     ContentsMap contents;
00196 
00197     // those who have subscribed to publish()/withdraw() requests
00198     SubscriberVector registeredClients;
00199 
00200   public:
00206     typedef BBItem *BBItemRef;
00207 
00212     class iterator
00213     {
00214       private:
00215         ContentsMap::iterator it;
00216       public:
00217         iterator(ContentsMap::iterator it0) {it==it0;}
00218         BBItemRef operator*()  {return (*it).second;}
00219         iterator& operator++()  {++it; return *this;}
00220         iterator operator++(int)  {iterator x=iterator(it); ++it; return x;}
00221         iterator& operator--()  {--it; return *this;}
00222         iterator operator--(int)  {iterator x=iterator(it); --it; return x;}
00223         bool operator==(const iterator& i2)  {return it==i2.it;}
00224         bool operator!=(const iterator& i2)  {return it!=i2.it;}
00225     };
00226 
00227   public:
00228     Blackboard();
00229     virtual ~Blackboard();
00230 
00231   protected:
00235     virtual void initialize();
00236 
00240     virtual void handleMessage(cMessage *msg);
00241 
00242   public:
00248     virtual BBItemRef publish(const char *label, cPolymorphic *item);
00249 
00253     virtual void withdraw(BBItemRef bbItem);
00254 
00260     virtual void changed(BBItemRef bbItem, cPolymorphic *item=NULL);
00262 
00268     virtual BBItemRef subscribe(BlackboardAccess *bbClient, const char *label);
00269 
00273     virtual BBItemRef find(const char *label);
00274 
00278     virtual BBItemRef subscribe(BlackboardAccess *bbClient, BBItemRef bbItem);
00279 
00283     virtual void unsubscribe(BlackboardAccess *bbClient, BBItemRef bbItem);
00284 
00289     virtual void registerClient(BlackboardAccess *bbClient);
00290 
00294     virtual void removeClient(BlackboardAccess *bbClient);
00295 
00302     virtual void getBlackboardContent(BlackboardAccess *bbClient);
00303 
00307     iterator begin()  {return iterator(contents.begin());}
00308 
00312     iterator end()  {return iterator(contents.end());}
00314 };
00315 
00316 typedef Blackboard::BBItemRef BBItemRef;
00317 
00318 
00325 class INET_API BlackboardAccess
00326 {
00327   protected:
00328     Blackboard *bb;
00329 
00330   public:
00331     BlackboardAccess() {bb=NULL;}
00332     virtual ~BlackboardAccess() {}
00333 
00335     virtual Blackboard *getBlackboard();
00336 
00340     virtual bool blackboardItemChanged(BBItemRef item) = 0;
00342     virtual bool blackboardItemPublished(BBItemRef item) = 0;
00344     virtual bool blackboardItemWithdrawn(BBItemRef item) = 0;
00346 };
00347 
00348 #endif
00349 
00350 
00351 
00352