L:/channel/channel/include/Marshaler.h

Go to the documentation of this file.
00001 
00002 // Copyright (c) 2005, 2006 Yigong Liu
00003 // Permission to use, copy, modify, distribute and sell this software for any 
00004 //     purpose is hereby granted without fee, provided that the above copyright 
00005 //     notice appear in all copies and that both that copyright notice and this 
00006 //     permission notice appear in supporting documentation.
00007 // The author makes no representations about the 
00008 //     suitability of this software for any purpose. It is provided "as is" 
00009 //     without express or implied warranty.
00011 
00012 #ifndef _MARSHALER_H_
00013 #define _MARSHALER_H_
00014 
00015 //ACE headers
00016 #include "ace/Thread_Mutex.h"
00017 //for ACE_GUARD macros, need the following 2 headers
00018 #include "ace/Synch.h"
00019 #include "ace/OS.h"
00020 
00021 #include <map>
00022 
00023 #include <BaseDef.h>
00024 #include <LinearIdTrait.h>
00025 #include <HierarchicalIdTrait.h>
00026 
00027 namespace channel {
00028 
00030   typedef void (*MsgFreeCallback) (char * data);
00031 
00033   class Marshaler {
00034   public:
00035     virtual int marshal(ACE_OutputCDR &cdr, const char *data, const int size) = 0;
00036     virtual int demarshal(ACE_InputCDR &cdr, char * &data, int &size, MsgFreeCallback &callback) = 0;
00037   };
00038 
00039   template <class> class PubSubMsg_Marshaler;
00040   class ChannelMsg_Marshaler;
00041 
00042   template <class IdType, class IdTrait>
00043   class MarshalerRegistry {
00044   private:
00045     //marshaler registration
00046     std::map<IdType, Marshaler *> mar_tbl_;
00047     ACE_Thread_Mutex mar_tbl_lock_;
00048   public:
00049     MarshalerRegistry () {
00050       //register marshaler for system internal msgs
00051       ChannelMsg_Marshaler *mar0 = new ChannelMsg_Marshaler();
00052       register_marshaler (IdTrait::CHANNEL_CONN_MSG, mar0);
00053       register_marshaler (IdTrait::CHANNEL_DISCONN_MSG, mar0);
00054             
00055       PubSubMsg_Marshaler<IdType> *mar1 = new PubSubMsg_Marshaler<IdType>();
00056       register_marshaler (IdTrait::INIT_SUBSCRIPTION_INFO_MSG, mar1);
00057       register_marshaler (IdTrait::INIT_PUBLICATION_INFO_MSG, mar1);
00058       register_marshaler (IdTrait::SUBSCRIPTION_INFO_MSG, mar1);
00059       register_marshaler (IdTrait::UNSUBSCRIPTION_INFO_MSG, mar1);
00060       register_marshaler (IdTrait::PUBLICATION_INFO_MSG, mar1);
00061       register_marshaler (IdTrait::UNPUBLICATION_INFO_MSG, mar1);
00062     }
00063 
00064     Status register_marshaler(IdType type, Marshaler *mar)
00065       {
00066         ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, mar_tbl_lock_, FAILURE);
00067         mar_tbl_[type] = mar;
00068         return SUCCESS;
00069       }
00070 
00071     Marshaler * get_marshaler(IdType type)
00072       {
00073         ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, mar_tbl_lock_, NULL);
00074         if (mar_tbl_.find(type) != mar_tbl_.end())
00075           return mar_tbl_[type];
00076         else
00077           return NULL;
00078       }
00079 
00080   };
00081 
00082 };
00083 
00084 #endif

Generated on Mon Feb 27 19:59:21 2006 for channel by  doxygen 1.4.6-NO