b26f4cfb375a01433eb9f1c3c5c59a371f0f4028
[chaz/yoink] / src / Moof / Service.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 /**
13 * \file Service.hh
14 * Classes for publishing and finding network services.
15 */
16
17 #ifndef _MOOF_SERVICE_HH_
18 #define _MOOF_SERVICE_HH_
19
20 #include <map>
21
22 #include <Moof/Math.hh>
23 #include <Moof/Socket.hh>
24
25
26 namespace Mf {
27
28
29 /**
30 * Class representing a network service.
31 */
32 class Service
33 {
34 public:
35
36 /**
37 * Construct a network service. The type of service will be inferred
38 * from the address.
39 * \param address The address of the host.
40 * \param name The service name.
41 * \param text The service information.
42 */
43 Service(const SocketAddress& address,
44 const std::string& name,
45 const std::string& text);
46
47 /**
48 * Construct a network service with an explicit type.
49 * \param address The address of the host.
50 * \param type The type of service.
51 * \param name The service name.
52 * \param text The service information.
53 */
54 Service(const SocketAddress& address,
55 const std::string& type,
56 const std::string& name,
57 const std::string& text);
58
59
60 /**
61 * Publish the service on the network.
62 */
63 void publish();
64
65 /**
66 * Stop publishing the service on the network.
67 */
68 void stop();
69
70
71 /**
72 * Get the host address.
73 * \return The address.
74 */
75 const SocketAddress& address() const
76 {
77 return mAddress;
78 }
79
80 /**
81 * Get the type of the service.
82 * \return The service type.
83 */
84 const std::string& type() const
85 {
86 return mType;
87 }
88
89 /**
90 * Get the service name.
91 * \return The service name.
92 */
93 const std::string& name() const
94 {
95 return mName;
96 }
97
98 /**
99 * Get the service information.
100 * \return The service information as a string.
101 */
102 const std::string& text() const
103 {
104 return mText;
105 }
106
107
108 ~Service();
109
110
111 private:
112
113 int handlePacket(SocketMultiplexer& sock,
114 Packet& packet,
115 const SocketAddress& address);
116
117 SocketAddress mAddress;
118 std::string mType;
119 std::string mName;
120 std::string mText;
121 };
122
123
124 class ServiceFinder
125 {
126 public:
127
128 /**
129 * Construct a service finder to find services of the same type as an
130 * address.
131 * \address The address.
132 */
133 ServiceFinder(const SocketAddress& address);
134
135 /**
136 * Construct a service finder to find services of a certain type.
137 * \param type The type of the service.
138 * \param sockType The type of socket.
139 */
140 ServiceFinder(const std::string& type, int sockType = SOCK_STREAM);
141
142
143 const std::map<std::string,Service>& services() const
144 {
145 return mServices;
146 }
147
148
149 private:
150
151 int handlePacket(SocketMultiplexer& sock,
152 Packet& packet,
153 const SocketAddress& address);
154
155 std::string mType;
156 std::map<std::string,Service> mServices;
157 };
158
159
160 } // namespace Mf
161
162 #endif // _MOOF_SERVICE_HH_
163
This page took 0.034656 seconds and 3 git commands to generate.