source: source/ariba/utility/addressing/port_address.hpp@ 5638

Last change on this file since 5638 was 5284, checked in by mies, 15 years ago

+ added new transport modules and adapted ariba to them
+ exchange endpoint descriptors an link establishment
+ clean up of base communication
+ link establishment with in the presence of multiple endpoints
+ local discovery for ipv6, ipv4 and bluetooth mac addresses

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