basicmuxchannel.h

Go to the documentation of this file.
00001 /*******************************************************************\
00002 
00003                    SESAME project software license
00004 
00005           Copyright (C) 2002, 2003 University of Amsterdam
00006 
00007     This program is free software; you can redistribute it and/or
00008      modify it under the terms of the GNU General Public License
00009     as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012    This program 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 General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018      along with this program; if not, write to the Free Software
00019       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00020                            02111-1307, USA.
00021 
00022       For information regarding the SESAME software project see
00023               http://sesamesim.sourceforge.net or email
00024                     jcofflan@users.sourceforge.net
00025 
00026 \*******************************************************************/
00027 
00028 #ifndef BASICMUXCHANNEL_H
00029 #define BASICMUXCHANNEL_H
00030 
00031 #include <BasicUtils/BasicCondition.h>
00032 #include "basicmuxmessage.h"
00033 
00034 #include <list>
00035 
00036 class BasicMsgQueueMUX;
00037 
00038 class BasicMUXChannelBase {
00039   long key;
00040   long msgSize;
00041   long qLength;
00042 
00043   BasicMsgQueueMUX *parent;
00044   long filled;
00045 
00046   typedef std::list<BasicMUXMessageBase *> queue_t;
00047   queue_t readQ;
00048   queue_t writeQ;
00049 
00050   BasicCondition readLock;
00051   BasicCondition writeLock;
00052 
00053  public:
00054   BasicMUXChannelBase(long key, long msgSize, long qLength);
00055   virtual ~BasicMUXChannelBase();
00056 
00057   long getKey() {return key;}
00058 
00059   BasicMUXMessageBase *read(bool blocking = true);
00060 
00061   bool write(BasicMUXMessageBase *msg, bool blocking = true);
00062   void flush();
00063 
00064   virtual BasicMUXMessageBase *allocateMsg()
00065   {return new BasicMUXMessageBase(msgSize);}
00066 
00067  protected:
00068   void setParent(BasicMsgQueueMUX *parent) {this->parent = parent;}
00069   void enqueue(BasicMUXMessageBase *msg);
00070   BasicMUXMessageBase *dequeue();
00071   bool writeQueueIsEmpty();
00072   void msgCleared();
00073 
00074   friend class BasicMsgQueueMUX;
00075 };
00076 
00077 template <class T>
00078 class BasicMUXChannel : public BasicMUXChannelBase {
00079  public:
00080   BasicMUXChannel(long key, long qLength) :
00081     BasicMUXChannelBase(key, sizeof(T), qLength) {
00082   }
00083 
00084   T readT(bool blocking = true) {
00085     T data;
00086     BasicMUXMessage<T> *msg = (BasicMUXMessage<T> *)read(blocking);
00087 
00088     if (msg) {
00089       data = msg->getDataRef();
00090       delete msg;
00091     } else memset(&data, 0, sizeof(T));
00092 
00093     return data;
00094   }
00095 
00096   bool writeT(T &data, bool blocking = true) {
00097     return write(new BasicMUXMessage<T>(data), blocking);
00098   }
00099 
00100   virtual BasicMUXMessageBase *allocateMsg() {return new BasicMUXMessage<T>;}
00101 };
00102 #endif

Generated on Wed Apr 5 20:57:44 2006 for Sesame by  doxygen 1.4.6