00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #ifndef _MARSHALER_H_
00013 #define _MARSHALER_H_
00014
00015
00016 #include "ace/Thread_Mutex.h"
00017
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
00046 std::map<IdType, Marshaler *> mar_tbl_;
00047 ACE_Thread_Mutex mar_tbl_lock_;
00048 public:
00049 MarshalerRegistry () {
00050
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