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::ProbeInfo class.
35 **/
36 
37 #ifndef _netxx_probeinfo_h_
38 #define _netxx_probeinfo_h_
39 
40 // Netxx includes
41 #include "types.h"
42 
43 // standard includes
44 #include <vector>
45 
46 namespace Netxx {
47 
48 /**
49  * The Netxx::ProbeInfo class is used to provide information to the
50  * Netxx::Probe class on how to probe another Netxx object. This
51  * implementation of the class does not do much. The entire implementation
52  * is inline to make most of it disapper at compile time.
53  *
54  * For Netxx objects that need custom probing, they will create their own
55  * ProbeInfo class and drive from this one.
56 **/
57 class ProbeInfo {
58 public:
59     /**
60      * The ProbeInfo::PendingType is used to signal what state a socket is
61      * in after a pending check.
62      */
63     enum PendingType {
64 	pending_none	= 0x000000, ///< The socket is not ready
65 	pending_read	= 0x000001, ///< The socket is ready for reading
66 	pending_write	= 0x000002, ///< The socket is ready for writing
67 	pending_oobd	= 0x000004  ///< The socket is ready for reading OOB data.
68     };
69 
70     /// a type for combinding different PendingType's
71     typedef unsigned int pending_type;
72 
73     //####################################################################
74     /**
75      * Netxx::ProbeInfo Default constructor.
76      *
77      * @author Peter Jones
78     **/
79     //####################################################################
ProbeInfo(void)80     ProbeInfo (void)
81     { }
82 
83     //####################################################################
84     /**
85      * Construct a new ProbeInfo using a vector of sockets.
86      *
87      * @param sockets The list of sockets to use for probing.
88      * @author Peter Jones
89     **/
90     //####################################################################
ProbeInfo(const std::vector<socket_type> & sockets)91     explicit ProbeInfo (const std::vector<socket_type> &sockets)
92 	: sockets_(sockets) { }
93 
94     //####################################################################
95     /**
96      * Construct a new ProbeInfo using only one socket.
97      *
98      * @param socketfd The socket to probe for.
99      * @author Peter Jones
100     **/
101     //####################################################################
ProbeInfo(socket_type socketfd)102     explicit ProbeInfo (socket_type socketfd)
103     { add_socket(socketfd); }
104 
105     //####################################################################
106     /**
107      * Copy constructor.
108      *
109      * @param other The ProbeInfo to copy from.
110      * @author Peter Jones
111     **/
112     //####################################################################
ProbeInfo(const ProbeInfo & other)113     ProbeInfo (const ProbeInfo &other)
114 	: sockets_(other.sockets_) { }
115 
116     //####################################################################
117     /**
118      * Assignment operator.
119      *
120      * @param other The ProbeInfo to copy from.
121      * @return *this.
122      * @author Peter Jones
123     **/
124     //####################################################################
125     ProbeInfo& operator= (const ProbeInfo &other)
126     { ProbeInfo tmp(other); swap(tmp); return *this; }
127 
128     //####################################################################
129     /**
130      * Swap this ProbeInfo for another.
131      *
132      * @param other The ProbeInfo to swap with.
133      * @author Peter Jones
134     **/
135     //####################################################################
swap(ProbeInfo & other)136     void swap (ProbeInfo &other)
137     { sockets_.swap(other.sockets_); }
138 
139     //####################################################################
140     /**
141      * Class destructor.
142      *
143      * @author Peter Jones
144     **/
145     //####################################################################
~ProbeInfo(void)146     virtual ~ProbeInfo (void)
147     { }
148 
149     //####################################################################
150     /**
151      * Add a socket file descriptor to the list of sockets to probe.
152      *
153      * @param socketfd The socket file descriptor to add.
154      * @author Peter Jones
155     **/
156     //####################################################################
add_socket(socket_type socketfd)157     void add_socket (socket_type socketfd)
158     { sockets_.push_back(socketfd); }
159 
160     //####################################################################
161     /**
162      * Get the list of sockets that need to be probed.
163      *
164      * @return A vector that contains all the sockets to be probed.
165      * @author Peter Jones
166     **/
167     //####################################################################
get_sockets(void)168     const std::vector<socket_type>& get_sockets (void) const
169     { return sockets_; }
170 
171     //####################################################################
172     /**
173      * Reset the list of sockets to probe. Note: this will not reset the
174      * sockets to probe in the actual Netxx::Probe class. You should call
175      * Netxx::Probe::clear() as well.
176      *
177      * @author Peter Jones
178     **/
179     //####################################################################
clear(void)180     void clear (void)
181     { sockets_.clear(); }
182 
183     //####################################################################
184     /**
185      * Override this function if you need special pending checks for your
186      * socket file descriptors.
187      *
188      * @return True if Probe should call check_pending().
189      * @return False if Probe should not call check_pending().
190      * @author Peter Jones
191     **/
192     //####################################################################
needs_pending_check(void)193     virtual bool needs_pending_check (void) const
194     { return false; }
195 
196     //####################################################################
197     /**
198      * Override this function to return the correct pending state for the
199      * given socket file descriptor. This call should be very fast and
200      * should NEVER EVER block!
201      *
202      * @param socket_type The socket to check pending status on.
203      * @param pending_type What type of pending state are we looking for.
204      * @return The pending state for the socket.
205      * @author Peter Jones
206     **/
207     //####################################################################
check_pending(socket_type,pending_type)208     virtual pending_type check_pending (socket_type, pending_type) const
209     { return pending_none; }
210 
211 private:
212     std::vector<socket_type> sockets_;
213 
214 }; // end ProbeInfo class
215 } // end Netxx namespace
216 #endif
217