~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/include/pgm/ip/pgm_endpoint.hh

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=4:sw=4:noai:noexpandtab
 
2
 * 
 
3
 * PGM endpoint
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#ifndef __PGM_IP_PGM_ENDPOINT_HH__
 
23
#define __PGM_IP_PGM_ENDPOINT_HH__
 
24
 
 
25
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
26
#       pragma once
 
27
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 
28
 
 
29
namespace pgm {
 
30
#define restrict
 
31
#include <pgm/pgm.h>
 
32
}
 
33
 
 
34
namespace ip {
 
35
 
 
36
template <typename InternetProtocol>
 
37
class pgm_endpoint
 
38
{
 
39
public:
 
40
        /// The protocol type associated with the endpoint.
 
41
        typedef InternetProtocol protocol_type;
 
42
 
 
43
        typedef struct cpgm::pgm_sockaddr_t data_type;
 
44
 
 
45
        /// Default constructor.
 
46
        pgm_endpoint()
 
47
        : data_()
 
48
        {
 
49
                data_.sa_port = 0;
 
50
                cpgm::pgm_tsi_t tmp_addr = PGM_TSI_INIT;
 
51
                data_.sa_addr = tmp_addr;
 
52
        }
 
53
 
 
54
        /// Construct an endpoint using a port number, specified in host byte
 
55
        /// order.  The GSI will be generated from the node name.
 
56
        /**
 
57
         * @par examples
 
58
         * To initialise a PGM endpoint for port 7500, use:
 
59
         * @code
 
60
         * ip::pgm::endpoint ep (7500);
 
61
         * @endcode
 
62
         */
 
63
        pgm_endpoint (unsigned short port_num)
 
64
        : data_()
 
65
        {
 
66
                data_.sa_port = port_num;
 
67
                data_.sa_addr.sport = 0;
 
68
                pgm_gsi_create_from_hostname (&data_.sa_addr.gsi, NULL);
 
69
        }
 
70
 
 
71
        /// Construct an endpoint using a port number and a TSI.
 
72
        pgm_endpoint (const cpgm::pgm_tsi_t& tsi, unsigned short port_num)
 
73
        : data_()
 
74
        {
 
75
                data_.sa_port = port_num;
 
76
                data_.sa_addr = tsi;
 
77
        }
 
78
 
 
79
        /// Construct an endpoint using a port number and a memory area.
 
80
        pgm_endpoint (const void* src, std::size_t len, unsigned short port_num)
 
81
        : data_()
 
82
        {
 
83
                data_.sa_port = port_num;
 
84
                data_.sa_addr.sport = 0;
 
85
                pgm_gsi_create_from_data (&data_.sa_addr.gsi, static_cast<const uint8_t*>(src), len);
 
86
        }
 
87
 
 
88
        /// Copy constructor.
 
89
        pgm_endpoint (const pgm_endpoint& other)
 
90
        : data_ (other.data_)
 
91
        {
 
92
        }
 
93
 
 
94
        /// Assign from another endpoint.
 
95
        pgm_endpoint& operator= (const pgm_endpoint& other)
 
96
        {
 
97
                data_ = other.data_;
 
98
                return *this;
 
99
        }
 
100
 
 
101
        /// Get the underlying endpoint in the native type.
 
102
        const data_type* data() const
 
103
        {
 
104
                return &data_;
 
105
        }
 
106
 
 
107
        /// Get the underlying size of the endpoint in the native type.
 
108
        std::size_t size() const
 
109
        {
 
110
                return sizeof(data_type);
 
111
        }
 
112
 
 
113
        /// Get the port associated with the endpoint. The port number is always in
 
114
        /// the host's byte order.
 
115
        unsigned short port() const
 
116
        {
 
117
                return data_.sa_port;
 
118
        }
 
119
 
 
120
        /// Set the port associated with the endpoint. The port number is always in
 
121
        /// the host's byte order.
 
122
        void port (unsigned short port_num)
 
123
        {
 
124
                data_.sa_port = port_num;
 
125
        }
 
126
 
 
127
        /// Get the TSI associated with the endpoint.
 
128
        const cpgm::pgm_tsi_t* address() const
 
129
        {
 
130
                return &data_.sa_addr;
 
131
        }
 
132
 
 
133
        /// Set the TSI associated with the endpoint.
 
134
        void address (cpgm::pgm_tsi_t& addr)
 
135
        {
 
136
                data_.sa_addr = addr;
 
137
        }
 
138
 
 
139
        /// Compare two endpoints for equality.
 
140
        friend bool operator== (const pgm_endpoint<InternetProtocol>& e1,
 
141
                const pgm_endpoint<InternetProtocol>& e2)
 
142
        {
 
143
                return e1.address() == e2.address() && e1.port() == e2.port();
 
144
        }
 
145
 
 
146
        /// Compare two endpoints for inequality.
 
147
        friend bool operator!= (const pgm_endpoint<InternetProtocol>& e1,
 
148
                const pgm_endpoint<InternetProtocol>& e2)
 
149
        {
 
150
                return e1.address() != e2.address() || e1.port() != e2.port();
 
151
        }
 
152
 
 
153
        /// Compare endpoints for ordering.
 
154
        friend bool operator<(const pgm_endpoint<InternetProtocol>& e1,
 
155
                const pgm_endpoint<InternetProtocol>& e2)
 
156
        {
 
157
                if (e1.address() < e2.address())
 
158
                        return true;
 
159
                if (e1.address() != e2.address())
 
160
                        return false;
 
161
                return e1.port() < e2.port();
 
162
        }
 
163
 
 
164
private:
 
165
        // The underlying PGM socket address.
 
166
        data_type data_;
 
167
};
 
168
 
 
169
} // namespace ip
 
170
 
 
171
#endif /* __PGM_IP_PGM_ENDPOINT_HH__ */