1 // author : Boris Kolpackov <boris@kolpackov.net>
3 #ifndef ACE_RMCAST_PROTOCOL_H
4 #define ACE_RMCAST_PROTOCOL_H
7 #include "ace/Bound_Ptr.h"
9 #include "ace/Vector_T.h"
10 #include "ace/Hash_Map_Manager.h"
12 #include "ace/CDR_Stream.h"
13 #include "ace/CDR_Size.h"
15 #include "ace/INET_Addr.h"
16 #include "ace/Null_Mutex.h"
18 #include "ace/OS_NS_string.h"
19 #include "ace/OS_NS_stdlib.h"
27 typedef ACE_CDR::UShort u16
;
28 typedef ACE_CDR::ULong u32
;
29 typedef ACE_CDR::ULongLong u64
;
31 // Protocol parameters
34 unsigned short const max_service_size
= 60; // service profiles (Part, SN,
35 // etc), sizes plus message size.
40 typedef ACE_INET_Addr Address
;
45 operator() (Address
const& a
) const
47 unsigned long port (a
.get_port_number ());
48 unsigned long ip (a
.get_ip_address ());
50 port
<<= sizeof (unsigned long) - sizeof (unsigned short);
56 //@@ Provide stream<< (Address const&)
59 typedef ACE_OutputCDR ostream
;
60 typedef ACE_SizeCDR sstream
;
61 typedef ACE_InputCDR istream
;
66 ACE_Strong_Bound_Ptr
<Profile
, Mutex
>
75 Header (u16 id
, u16 size
)
76 : id_ (id
), size_ (size
)
82 (void) (is
>> id_
>> size_
);
105 friend struct Profile
;
121 Profile_ptr
p (clone_ ());
131 Profile (Header
const& h
)
141 operator= (Profile
const&) = delete;
147 return header_
.id ();
153 return header_
.size ();
170 return static_cast<u16
> (ss
.total_length ());
175 serialize_body (ostream
&) const = 0;
178 serialize_body (sstream
&) const = 0;
182 operator<< (ostream
& os
, Profile
const& p
);
186 operator<< (sstream
& ss
, Profile
const& p
);
194 operator<< (ostream
& os
, Profile::Header
const& hdr
)
204 operator<< (sstream
& ss
, Profile::Header
const& hdr
)
214 operator<< (ostream
& os
, Profile
const& p
)
217 p
.serialize_body (os
);
224 operator<< (sstream
& ss
, Profile
const& p
)
227 p
.serialize_body (ss
);
239 ACE_Strong_Bound_Ptr
<Message
, Mutex
>
245 ACE_Hash_Map_Manager
<u16
, Profile_ptr
, ACE_Null_Mutex
>
257 Message_ptr
cloned (new Message (*this));
262 Message (Message
const& m
)
265 for (Profiles::const_iterator
i (m
.profiles_
); !i
.done (); i
.advance ())
267 // Shallow copy of profiles. This implies that profiles are not
268 // modified as they go up/down the stack.
270 profiles_
.bind ((*i
).ext_id_
, (*i
).int_id_
);
276 operator= (Message
const&);
284 if (profiles_
.find (id
) == 0)
289 profiles_
.bind (id
, p
);
295 replace (Profile_ptr p
)
297 profiles_
.rebind (p
->id (), p
);
303 profiles_
.unbind (id
);
309 Profiles::ENTRY
* e
= 0;
311 if (profiles_
.find (id
, e
) == -1) return 0;
313 return e
->int_id_
.get ();
317 Profiles::const_iterator
323 return ProfileIterator (profiles_
);
336 for (Profiles::const_iterator
i (profiles_
); !i
.done (); i
.advance ())
338 ss
<< *((*i
).int_id_
);
341 return ss
.total_length ();
346 operator<< (ostream
& os
, Message
const& m
)
352 for (Profiles::const_iterator
i (m
.profiles_
); !i
.done (); i
.advance ())
354 os
<< *((*i
).int_id_
);
364 typedef ACE_Vector
<Message_ptr
, ACE_VECTOR_DEFAULT_SIZE
> Messages
;
372 ACE_Strong_Bound_Ptr
<From
, Mutex
>
375 struct From
: Profile
380 From (Header
const& h
, istream
& is
)
389 address_
= Address (port
, addr
);
392 From (Address
const& addr
)
393 : Profile (id
), address_ (addr
)
395 size (calculate_size ());
401 return From_ptr (clone_ ());
408 Profile_ptr
p (new From (*this));
412 From (From
const& from
)
414 address_ (from
.address_
)
427 serialize_body (ostream
& os
) const
429 u32
addr (address_
.get_ip_address ());
430 u16
port (address_
.get_port_number ());
437 serialize_body (sstream
& ss
) const
457 ACE_Strong_Bound_Ptr
<To
, Mutex
>
465 To (Header
const& h
, istream
& is
)
474 address_
= Address (port
, addr
);
477 To (Address
const& addr
)
478 : Profile (id
), address_ (addr
)
480 size (calculate_size ());
486 return To_ptr (clone_ ());
493 Profile_ptr
p (new To (*this));
499 address_ (to
.address_
)
512 serialize_body (ostream
& os
) const
514 u32
addr (address_
.get_ip_address ());
515 u16
port (address_
.get_port_number ());
522 serialize_body (sstream
& ss
) const
542 ACE_Strong_Bound_Ptr
<Data
, Mutex
>
545 struct Data
: Profile
554 operator delete (buf_
);
557 Data (Header
const& h
, istream
& is
)
565 buf_
= reinterpret_cast<char*> (operator new (capacity_
));
566 is
.read_char_array (buf_
, size_
);
570 Data (void const* buf
, size_t s
, size_t capacity
= 0)
574 capacity_ (capacity
< size_
? size_
: capacity
)
578 buf_
= reinterpret_cast<char*> (operator new (capacity_
));
579 ACE_OS::memcpy (buf_
, buf
, size_
);
582 Profile::size (calculate_size ());
588 return Data_ptr (clone_ ());
595 Profile_ptr
p (new Data (*this));
603 capacity_ (d
.capacity_
)
607 buf_
= reinterpret_cast<char*> (operator new (capacity_
));
608 ACE_OS::memcpy (buf_
, d
.buf_
, size_
);
611 Profile::size (calculate_size ());
641 Profile::size (calculate_size ());
652 serialize_body (ostream
& os
) const
654 os
.write_char_array (buf_
, size_
);
658 serialize_body (sstream
& ss
) const
660 ss
.write_char_array (buf_
, size_
);
676 ACE_Strong_Bound_Ptr
<SN
, Mutex
>
684 SN (Header
const& h
, istream
& is
)
691 : Profile (id
), n_ (n
)
693 size (calculate_size ());
699 return SN_ptr (clone_ ());
706 Profile_ptr
p (new SN (*this));
725 serialize_body (ostream
& os
) const
731 serialize_body (sstream
& ss
) const
747 ACE_Strong_Bound_Ptr
<NAK
, Mutex
>
750 class NAK
: public Profile
755 typedef ACE_Vector
<u64
, ACE_VECTOR_DEFAULT_SIZE
> SerialNumbers
;
756 typedef SerialNumbers::Iterator iterator
;
758 NAK (Header
const& h
, istream
& is
)
768 size_t sn_size (ss
.total_length ());
775 size_t addr_size (ss
.total_length ());
781 // num_of_sns = (size - addr_size) / sn_size
783 for (unsigned long i (0); i
< ((h
.size () - addr_size
) / sn_size
); ++i
)
790 address_
= Address (port
, addr
);
793 NAK (Address
const& src
)
794 : Profile (id
), address_ (src
)
796 size (calculate_size ());
802 return NAK_ptr (clone_ ());
809 Profile_ptr
p (new NAK (*this));
815 address_ (nak
.address_
),
825 size (calculate_size ());
839 return iterator (sns_
);
857 // Count max number of elements that will fit into NAK profile
858 // with size <= max_size.
861 max_count (u32 max_size
)
867 Profile::Header
hdr (0, 0);
880 if (ss
.total_length () <= max_size
)
883 if (ss
.total_length () >= max_size
)
892 serialize_body (ostream
& os
) const
894 NAK
& this_
= const_cast<NAK
&> (*this); // Don't put in ROM.
896 u32
addr (address_
.get_ip_address ());
897 u16
port (address_
.get_port_number ());
902 // Stone age iteration.
904 for (iterator
i (this_
.begin ()); !i
.done (); i
.advance ())
913 serialize_body (sstream
& ss
) const
915 NAK
& this_
= const_cast<NAK
&> (*this); // Don't put in ROM.
923 // Stone age iteration.
925 for (iterator
i (this_
.begin ()); !i
.done (); i
.advance ())
943 ACE_Strong_Bound_Ptr
<NRTM
, Mutex
>
946 struct NRTM
: Profile
951 NRTM (Header
const& h
, istream
& is
)
952 : Profile (h
), map_ (10)
964 size_t block_size (ss
.total_length ());
967 // num_of_blocks = size / block_size
969 for (size_t i (0); i
< (h
.size () / block_size
); ++i
)
975 map_
.bind (Address (port
, addr
), sn
);
980 : Profile (id
), map_ (10)
982 size (calculate_size ());
988 return NRTM_ptr (clone_ ());
995 Profile_ptr
p (new NRTM (*this));
999 NRTM (NRTM
const& nrtm
)
1002 for (Map::const_iterator
i (nrtm
.map_
); !i
.done (); i
.advance ())
1004 map_
.bind ((*i
).ext_id_
, (*i
).int_id_
);
1010 insert (Address
const& addr
, u64 sn
)
1012 map_
.bind (addr
, sn
);
1014 size (calculate_size ());
1018 find (Address
const& addr
) const
1022 if (map_
.find (addr
, sn
) == -1) return 0;
1030 return map_
.current_size () == 0;
1034 // Count max number of elements that will fit into NRTM profile
1035 // with size <= max_size.
1038 max_count (u32 max_size
)
1044 Profile::Header
hdr (0, 0);
1057 if (ss
.total_length () <= max_size
)
1060 if (ss
.total_length () >= max_size
)
1069 serialize_body (ostream
& os
) const
1071 for (Map::const_iterator
i (map_
), e (map_
, 1); i
!= e
; ++i
)
1073 u32
addr ((*i
).ext_id_
.get_ip_address ());
1074 u16
port ((*i
).ext_id_
.get_port_number ());
1075 u64
sn ((*i
).int_id_
);
1084 serialize_body (sstream
& ss
) const
1086 for (Map::const_iterator
i (map_
), e (map_
, 1); i
!= e
; ++i
)
1100 ACE_Hash_Map_Manager_Ex
<Address
,
1103 ACE_Equal_To
<Address
>,
1117 ACE_Strong_Bound_Ptr
<NoData
, Mutex
>
1120 struct NoData
: Profile
1122 static u16
const id
;
1125 NoData (Header
const& h
, istream
&)
1139 return NoData_ptr (clone_ ());
1146 Profile_ptr
p (new NoData (*this));
1150 NoData (NoData
const& no_data
)
1157 serialize_body (ostream
&) const
1162 serialize_body (sstream
&) const
1174 ACE_Strong_Bound_Ptr
<Part
, Mutex
>
1177 struct Part
: Profile
1179 static u16
const id
;
1182 Part (Header
const& h
, istream
& is
)
1190 Part (u32 num
, u32 of
, u64 total_size
)
1194 total_size_ (total_size
)
1196 size (calculate_size ());
1202 return Part_ptr (clone_ ());
1209 Profile_ptr
p (new Part (*this));
1213 Part (Part
const& part
)
1217 total_size_ (part
.total_size_
)
1242 serialize_body (ostream
& os
) const
1250 serialize_body (sstream
& ss
) const
1268 operator<< (std::ostream& os, ACE_RMCast::Address const& a)
1271 a.addr_to_string (buf, 64, 1);
1276 #endif // ACE_RMCAST_PROTOCOL_H