MicoleBus.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #ifndef MicoleBus_H
00024 #define MicoleBus_H
00025 #include "micolelib.h"
00026 
00027 #include "micolebus.h"
00028 
00029 //#include "MicoleBusWriteLock.h"
00030 
00031 #include <string>
00032 #include <list>
00033 #include <queue>
00034 
00035 #include <boost/regex.hpp>
00036 
00037 using namespace boost;
00038 using namespace std;
00039 
00040 #include <lo2kThread.h>
00041 
00042 #include <process.h>
00043 #include <windows.h>
00044 
00045 #define MAXREADERS 12
00046 
00047 //define MicoleCallBack class defined later
00048 class MicoleCallback;
00049 
00053 struct MicoleBusRegexp
00054 {
00055         regex _regexp;
00056         list <MicoleCallback *> _cbList;
00057 };
00058 
00059 
00071 class MICOLELIB_API MicoleBus : lo2k::Thread
00072 {
00073         private:
00074                 bool _sync; 
00076                 HANDLE _isWriting;
00077 
00078                 HANDLE _semLock;
00079 
00080         protected:
00081                 int _regexpNumber; 
00082                 int _bindNumber;
00083                 bool _run;
00084 
00085                 virtual void run();
00086 
00087                 map<string, MicoleBusRegexp> _regexpList; 
00088                 queue<string> _buffer;
00089 
00090 
00091         public:
00096                 MicoleBus(bool sync);
00097                 MicoleBus(const MicoleBus &mb);
00098 
00099                 virtual ~MicoleBus();
00100                 
00104                 void stop();
00105 
00109                 void interpret(string);
00110                 
00115                 void sendMsg(string s);
00116 
00123                 int bindMsg(string regexp, MicoleCallback * cb);
00124 
00125                 void lockSlot() { WaitForSingleObject( _semLock, INFINITE ); }
00126 
00127                 void lockAllSlots()
00128                 {
00129                         WaitForSingleObject( _isWriting, INFINITE );
00130                         for (int i = 0; i< MAXREADERS-1; i++)
00131                                 lockSlot();
00132                 }
00133 
00134                 void unlockSlot() { ReleaseSemaphore(_semLock,1,NULL); }
00135 
00136                 void unlockAllSlots()
00137                 {
00138                         ReleaseSemaphore( _isWriting, 1, NULL );
00139                         for (int i = 0; i< MAXREADERS-1; i++)
00140                                 unlockSlot();
00141                 }
00142 
00146                 void unbindMsg(MicoleCallback * cb);
00147 };
00148 
00153 class MicoleCallback
00154 {
00155         public:
00159                 MicoleCallback() {};
00160 
00164                 virtual ~MicoleCallback() {};
00165 
00172                 virtual void execute (MicoleBus *bus, int argc, const char **argv) = 0;
00173 };
00174 
00175 
00176 class MicoleAgent;
00177 
00183 template <class T>  class MicoleCallbackOf: public MicoleCallback
00184 {
00185         public:
00186                 typedef void (T::*MicoleAgentMemberFn) (MicoleBus * bus, int argc , const char ** argv);
00187 
00188                 MicoleAgentMemberFn _memberFn;
00189                 T * _obj;
00190 
00191                 MicoleCallbackOf(T * obj, MicoleAgentMemberFn fn)
00192                 :_memberFn(fn),_obj(obj)
00193                 {
00194                 }
00195 
00196                 MicoleCallbackOf(const MicoleCallbackOf &b)
00197                 :_memberFn(b._memberFn),_obj(b._obj)
00198                 {
00199                 }
00200 
00201                 void execute (MicoleBus *bus, int argc, const char **argv)
00202                 {
00203                         (_obj->*_memberFn) (bus, argc, argv);
00204                 }
00205 };
00206  // end core
00208 
00209 #define BUS_CALLBACK_OF( cl, m ) new MicoleCallbackOf<cl>( this, (&cl::m) )
00210 
00211 #endif

Generated on Tue Oct 16 17:10:42 2007 for Micole by  doxygen 1.4.7