arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Messenger.h

Go to the documentation of this file.
00001 //
00002 //------------------------------------------------------------------------
00003 //
00004 // File   : Messenger.h
00005 // Author : Patrick McCormick (pat@acl.lanl.gov)
00006 // 
00007 // $Date: 2003/08/01 22:10:47 $
00008 // $Revision: 1.4 $
00009 //
00010 // This class handles the overall messaging with active message support
00011 // (known as callbacks in this case).  This class works well in situations
00012 // where there are master-slave situations in which the slaves wait to 
00013 // receive a message from a master as to what task to perform.  The client
00014 // code should use a series of calls to AddCallback() to install handlers 
00015 // (callbacks) for messages of interest. 
00016 // 
00017 // TODO: All the methods in this code are extremely short -- perhaps we 
00018 //       should consider making everything inline?  Will it really make
00019 //       that much difference. 
00020 //
00021 //------------------------------------------------------------------------
00022 //
00023 #ifndef KOKO_MESSENGER_H_
00024 #define KOKO_MESSENGER_H_
00025 
00026 #include "koko.h"
00027 
00028 #include "CallbackMap.h"
00029 #include "KokoBuffer.h"
00030 
00031 class Communicator;
00032 
00033 class Messenger 
00034 {
00035   public:
00036 
00037     Messenger(Communicator *comm, unsigned int rbufsize);
00038     ~Messenger(void);
00039 
00040     ///========================================================================
00041     /// Callback installation. 
00042     inline bool AddCallback(KokoTag tag, 
00043                                void (*funcp)(KokoBuffer &databuf))
00044     {
00045       Function *func = new Function(funcp);
00046       return(_cbmap.Add(tag, func));
00047     };
00048 
00049     template <class Type>
00050     bool AddCallback(KokoTag tag, Type &obj, 
00051                         void (Type::* meth)(KokoBuffer &databuf))
00052     {
00053       Method<Type> *methp = new Method<Type>(&obj, meth);
00054       return(_cbmap.Add(tag, methp));
00055     };
00056          
00057     template <class Type>
00058     bool AddCallback(KokoTag tag, Type *obj, 
00059                         void (Type::* meth)(KokoBuffer &databuf))
00060     {
00061       Method<Type> *methp = new Method<Type>(obj, meth);
00062       return (_cbmap.Add(tag, methp));
00063     };
00064 
00065     // Callback removal.
00066     inline bool RemoveCallback(KokoTag id)
00067     {
00068       return _cbmap.Remove(id);
00069     };
00070 
00071     ///=======================================================================
00072     /// Internal message buffer management.
00073     inline unsigned int BufferSize(void)
00074     { return _kbuf.TotalSize(); };
00075 
00076     inline void ResizeBuffer(unsigned int new_size)
00077     { _kbuf.Resize(new_size); };
00078 
00079 
00080     ///=======================================================================
00081     /// Message handling...
00082 
00083     void WaitForMessage(void);
00084 
00085     void WaitForBroadcast(void);
00086 
00087   private:
00088     Communicator     *_com;    // The communicator object for all our messages.
00089     CallbackMap       _cbmap;  // The callback map -- maps KokoTag into the 
00090                                //   actual callback.
00091     KokoBuffer        _kbuf;   // The message buffer.  
00092 };
00093 
00094 #endif

Send questions, comments, and bug reports to:
jmk