1 | // port_address.hpp, created on 24.06.2009 by Sebastian Mies |
---|
2 | |
---|
3 | #ifndef PORT_ADDRESS_HPP_ |
---|
4 | #define PORT_ADDRESS_HPP_ |
---|
5 | |
---|
6 | #include<string> |
---|
7 | #include<cstdio> |
---|
8 | |
---|
9 | #include<boost/tr1/functional.hpp> |
---|
10 | |
---|
11 | #include "detail/address_convenience.hpp" |
---|
12 | |
---|
13 | namespace ariba { |
---|
14 | namespace addressing { |
---|
15 | |
---|
16 | struct port_address_info { |
---|
17 | static const uint16_t type_id; |
---|
18 | static const std::string type_name; |
---|
19 | }; |
---|
20 | |
---|
21 | /** |
---|
22 | * TODO: Doc |
---|
23 | * |
---|
24 | * @author Sebastian Mies <mies@tm.uka.de> |
---|
25 | */ |
---|
26 | template<class AddressInfo = port_address_info> |
---|
27 | class port_address_tpl: public detail::address_convenience< |
---|
28 | port_address_tpl<AddressInfo> > { |
---|
29 | private: |
---|
30 | uint16_t port; |
---|
31 | |
---|
32 | public: |
---|
33 | typedef port_address_tpl<AddressInfo> self; |
---|
34 | |
---|
35 | port_address_tpl() { |
---|
36 | port = 0; |
---|
37 | } |
---|
38 | |
---|
39 | port_address_tpl( const port_address_tpl& copy ) : port(copy.port) { |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | port_address_tpl(const std::string& text) { |
---|
44 | assign( text ); |
---|
45 | } |
---|
46 | |
---|
47 | port_address_tpl(const char* text) { |
---|
48 | assign( std::string(text) ); |
---|
49 | } |
---|
50 | |
---|
51 | port_address_tpl(uint16_t port) : port(port) { |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | port_address_tpl(const uint8_t* bytes, size_t size) { |
---|
56 | assign( bytes, size ); |
---|
57 | } |
---|
58 | |
---|
59 | //--- compare operations -------------------------------------------------- |
---|
60 | |
---|
61 | /// implements comparison operators |
---|
62 | int compare_to(const self& rhs) const { |
---|
63 | return port - rhs.port; |
---|
64 | } |
---|
65 | |
---|
66 | //--- bytes representation ------------------------------------------------ |
---|
67 | |
---|
68 | /// returns true, if this address has a fixed size in bytes |
---|
69 | bool is_bytes_size_static() const { |
---|
70 | return true; |
---|
71 | } |
---|
72 | |
---|
73 | /// returns the number of bytes used for serialization of this address |
---|
74 | size_t to_bytes_size() const { |
---|
75 | return 2; |
---|
76 | } |
---|
77 | |
---|
78 | /// converts this address to a binary representation |
---|
79 | void to_bytes(uint8_t* bytes) const { |
---|
80 | bytes[0] = port >> 8; |
---|
81 | bytes[1] = port & 0xFF; |
---|
82 | } |
---|
83 | |
---|
84 | /// Assigns an address using a bunch of bytes |
---|
85 | bool assign(const uint8_t* bytes, size_t size) { |
---|
86 | port = ((bytes[0] << 8) + bytes[1]); |
---|
87 | return false; |
---|
88 | } |
---|
89 | |
---|
90 | //--- text representation ------------------------------------------------- |
---|
91 | |
---|
92 | /// convert address to a string that can be used to reconstruct the address |
---|
93 | std::string to_string() const { |
---|
94 | char str[8]; |
---|
95 | sprintf(str, "%d", port); |
---|
96 | return std::string(str); |
---|
97 | } |
---|
98 | |
---|
99 | /// Assigns an address using a human-readable |
---|
100 | bool assign(const std::string& text) { |
---|
101 | unsigned int port_; |
---|
102 | sscanf(text.c_str(), "%d", &port_); |
---|
103 | if (port_ >= 0 && port_ <= 65535) { |
---|
104 | port = (uint16_t) port_; |
---|
105 | return false; |
---|
106 | } |
---|
107 | return true; |
---|
108 | } |
---|
109 | |
---|
110 | //--- assignment ---------------------------------------------------------- |
---|
111 | |
---|
112 | /// Assigns an address |
---|
113 | bool assign(const self& rhs) { |
---|
114 | port = rhs.port; |
---|
115 | return false; |
---|
116 | } |
---|
117 | |
---|
118 | //--- address info -------------------------------------------------------- |
---|
119 | |
---|
120 | /// returns the type name |
---|
121 | const std::string& type_name() const { |
---|
122 | return AddressInfo::type_name; |
---|
123 | } |
---|
124 | |
---|
125 | /// returns the type identifier |
---|
126 | const uint16_t type_id() const { |
---|
127 | return AddressInfo::type_id; |
---|
128 | } |
---|
129 | |
---|
130 | //--- conversions --------------------------------------------------------- |
---|
131 | |
---|
132 | uint16_t asio() const { |
---|
133 | return port; |
---|
134 | } |
---|
135 | |
---|
136 | void asio( uint16_t port ) { |
---|
137 | this->port = port; |
---|
138 | } |
---|
139 | |
---|
140 | uint16_t value() const { |
---|
141 | return port; |
---|
142 | } |
---|
143 | |
---|
144 | void value( uint16_t v ) { |
---|
145 | port = v; |
---|
146 | } |
---|
147 | }; |
---|
148 | |
---|
149 | typedef port_address_tpl<> port_address; |
---|
150 | |
---|
151 | }} // namespace ariba::addressing |
---|
152 | |
---|
153 | namespace boost { |
---|
154 | |
---|
155 | template<class T> |
---|
156 | struct hash<ariba::addressing::port_address_tpl<T> >: public std::unary_function<ariba::addressing::port_address_tpl<T>, std::size_t> { |
---|
157 | std::size_t operator()(const ariba::addressing::port_address_tpl<T>& ep) const { |
---|
158 | return hash_value(ep.value()); |
---|
159 | } |
---|
160 | }; |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | #endif /* PORT_ADDRESS_HPP_ */ |
---|