monotone

monotone Mtn Source Tree

Root/netxx/probe.h

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the defintion of the Netxx::Probe class.
35**/
36
37#ifndef _netxx_probe_h_
38#define _netxx_probe_h_
39
40// Netxx includes
41#include <netxx/types.h>
42#include <netxx/timeout.h>
43#include <netxx/probeinfo.h>
44
45// standard includes
46#include <vector>
47#include <utility>
48
49namespace Netxx {
50
51class PipeCompatibleProbe;
52
53/**
54 * The Netxx::Probe class is a wrapper around one of the Netxx probe
55 * classes. The reason that we have a wrapper is because most operating
56 * systems support select(2) but some also support more advanced APIs like
57 * kqueue(2) or /dev/poll.
58**/
59class Probe {
60 /*
61 * Probe has no public way to select read only and write only sockets
62 * needed for probing pipes, so grant PipeCompatibleProbe to use add_socket
63 */
64 friend class PipeCompatibleProbe;
65public:
66 /*
67 * Bitmask for telling Probe exactly what you want and for testing the
68 * return value from Probe::ready().
69 */
70 enum ReadyType {
71ready_none = 0x000000, ///< Nothing or Everything depeding on the context
72ready_read = 0x000001,///< Readable
73ready_write = 0x000002, ///< Writable
74ready_oobd = 0x000004 ///< Readable Out-of-band Data
75 };
76
77 /// type for holding the bitmask
78 typedef unsigned int ready_type;
79
80 /// type returned from ready()
81 typedef std::pair<socket_type, ready_type> result_type;
82
83 //####################################################################
84 /**
85 * Construct a new Netxx::Probe object.
86 *
87 * @author Peter Jones
88 **/
89 //####################################################################
90 Probe (void);
91
92 //####################################################################
93 /**
94 * Netxx::Probe copy constructor.
95 *
96 * @param other The other Probe object to copy from.
97 * @author Peter Jones
98 **/
99 //####################################################################
100 Probe (const Probe &other);
101
102 //####################################################################
103 /**
104 * Netxx::Probe assignment operator.
105 *
106 * @param other The other Probe object to copy from.
107 * @return *this.
108 * @author Peter Jones
109 **/
110 //####################################################################
111 Probe& operator= (const Probe &other);
112
113 //####################################################################
114 /**
115 * Swap this Probe and another one. Similar to std::swap().
116 *
117 * @param other The other Probe to swap with.
118 * @author Peter Jones
119 **/
120 //####################################################################
121 void swap(Probe &other);
122
123 //####################################################################
124 /**
125 * Netxx::Probe destructor.
126 *
127 * @author Peter Jones
128 **/
129 //####################################################################
130 ~Probe (void);
131
132 //####################################################################
133 /**
134 * Clear the Probe. All objects will be removed from the Probe and it
135 * will be in a brand-new like state.
136 *
137 * @author Peter Jones
138 **/
139 //####################################################################
140 void clear (void);
141
142 //####################################################################
143 /**
144 * Preform the probe. This function will block until either some data is
145 * ready or the given timeout expires. You may also supply a bitmask for
146 * the type of data you want in this probe.
147 *
148 * @param timeout How long to wait for data. Can be a NULL timeout to block until probe data is avaliable.
149 * @param rt A bitmask to control what is returned. ready_none means all data in this context.
150 * @return a std::pair where first is the ready socket and second is a bitmask to tell you what it is ready for.
151 * @return a std::pair with first set to -1 to signal a timeout.
152 * @author Peter Jones
153 **/
154 //####################################################################
155 result_type ready (const Timeout &timeout=Timeout(), ready_type rt=ready_none);
156
157 //####################################################################
158 /**
159 * Add an object to the Probe. The object must support the
160 * Netxx::ProbeInfo class. All Netxx classes such as Stream and Datagram
161 * support the ProbeInfo class.
162 *
163 * You can optionally give a bitmask that will tell Probe what type of
164 * data to probe for. The default is to probe for all data. In this
165 * case, ready_none means probe for all data.
166 *
167 * @param t The object to add to the Probe.
168 * @param rt A bitmask that tells Probe what to probe for.
169 * @author Peter Jones
170 **/
171 //####################################################################
172 template <typename T> void add (const T &t, ready_type rt=ready_none)
173 {
174// implemented inline to work around bug in MSVC
175const ProbeInfo *pi = t.get_probe_info();
176std::vector<socket_type>::const_iterator i=pi->get_sockets().begin(), end=pi->get_sockets().end();
177for (; i!=end; ++i) { if (pi->needs_pending_check()) add_socket(pi, *i, rt); else add_socket(*i, rt); }
178 }
179
180 //####################################################################
181 /**
182 * Remove the given object from the Probe.
183 *
184 * @param t The object to remove from the Probe.
185 * @author Peter Jones
186 **/
187 //####################################################################
188 template <typename T> void remove (const T &t)
189 {
190// implemented inline to work around bug in MSVC
191const ProbeInfo *pi = t.get_probe_info();
192std::vector<socket_type>::const_iterator i=pi->get_sockets().begin(), end=pi->get_sockets().end();
193for (; i!=end; ++i) remove_socket(*i);
194 }
195
196private:
197 void add_socket (socket_type socketfd, ready_type rt);
198 void add_socket (const ProbeInfo *pi, socket_type socketfd, ready_type rt);
199 void remove_socket (socket_type socketfd);
200 struct pimpl; pimpl *pimpl_;
201
202}; // end Netxx::Probe class
203} // end Netxx namespace
204#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status