monotone

monotone Mtn Source Tree

Root/netxx/probe.cxx

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 implementation of the Netxx::Probe class.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include <netxx/probe.h>
42#include <netxx/types.h>
43#include <netxx/probeinfo.h>
44#include "probe_impl.h"
45
46// standard includes
47#include <list>
48#include <utility>
49#include <memory>
50
51//####################################################################
52namespace
53{
54 typedef std::list<std::pair<Netxx::socket_type, Netxx::Probe::ready_type> > ready_return_type;
55 typedef std::list<std::pair<Netxx::socket_type, const Netxx::ProbeInfo*> > pi_type;
56
57 Netxx::Probe::result_type find_ready_match (ready_return_type &q, Netxx::Probe::ready_type rt);
58}
59//####################################################################
60struct Netxx::Probe::pimpl
61{
62 ready_return_type ready_queue_;
63 pi_type pilist_;
64 Probe_impl probe_impl_;
65};
66//####################################################################
67Netxx::Probe::Probe (void)
68{
69 pimpl_ = new pimpl;
70}
71//####################################################################
72Netxx::Probe::Probe (const Probe &other)
73{
74 std::auto_ptr<pimpl> ap(pimpl_ = new pimpl);
75
76 pimpl_->ready_queue_ = other.pimpl_->ready_queue_;
77 pimpl_->pilist_ = other.pimpl_->pilist_;
78 pimpl_->probe_impl_ = other.pimpl_->probe_impl_;
79
80 ap.release();
81}
82//####################################################################
83Netxx::Probe& Netxx::Probe::operator= (const Probe &other)
84{
85 Probe tmp(other); swap(tmp);
86 return *this;
87}
88//####################################################################
89void Netxx::Probe::swap (Probe &other)
90{
91 pimpl_->ready_queue_.swap(other.pimpl_->ready_queue_);
92 pimpl_->pilist_.swap(other.pimpl_->pilist_);
93 pimpl_->probe_impl_.swap(other.pimpl_->probe_impl_);
94}
95//####################################################################
96Netxx::Probe::~Probe (void)
97{
98 delete pimpl_;
99}
100//####################################################################
101void Netxx::Probe::clear (void)
102{
103 pimpl_->ready_queue_.clear();
104 pimpl_->pilist_.clear();
105 pimpl_->probe_impl_.clear();
106}
107//####################################################################
108std::pair<Netxx::socket_type, Netxx::Probe::ready_type> Netxx::Probe::ready (const Timeout &timeout, ready_type rt)
109{
110 result_type result;
111 bool none_match=false;
112
113 if (rt != ready_none && !pimpl_->ready_queue_.empty()) {
114result = find_ready_match(pimpl_->ready_queue_, rt);
115if (result.first != -1) return result;
116none_match = true;
117 }
118
119 if (pimpl_->ready_queue_.empty() || none_match) {
120pi_type::const_iterator p_it=pimpl_->pilist_.begin(), p_end=pimpl_->pilist_.end();
121
122for (; p_it!=p_end; ++p_it) {
123 ProbeInfo::pending_type result = p_it->second->check_pending(p_it->first, rt);
124
125 if (result != ProbeInfo::pending_none) {
126pimpl_->ready_queue_.push_back(std::make_pair(p_it->first, static_cast<ready_type>(result)));
127 }
128}
129
130Probe_impl::probe_type probe_result = pimpl_->probe_impl_.probe(timeout, rt);
131Probe_impl::probe_type::const_iterator pr_it=probe_result.begin(), pr_end=probe_result.end();
132for (; pr_it!=pr_end; ++pr_it) { pimpl_->ready_queue_.push_back(*pr_it); }
133 }
134
135 if (!pimpl_->ready_queue_.empty()) {
136if (rt != ready_none) {
137 result = find_ready_match(pimpl_->ready_queue_, rt);
138} else {
139 result = pimpl_->ready_queue_.front();
140 pimpl_->ready_queue_.erase(pimpl_->ready_queue_.begin());
141}
142
143return result;
144 }
145
146 result.first = -1;
147 result.second = ready_none;
148 return result;
149}
150//####################################################################
151void Netxx::Probe::add_socket (socket_type socketfd, ready_type rt)
152{
153 pimpl_->probe_impl_.add(socketfd, rt);
154}
155//####################################################################
156void Netxx::Probe::add_socket (const ProbeInfo *pi, socket_type socketfd, ready_type rt)
157{
158 pimpl_->pilist_.push_back(std::make_pair(socketfd, pi));
159 pimpl_->probe_impl_.add(socketfd, rt);
160}
161//####################################################################
162void Netxx::Probe::remove_socket (socket_type socketfd)
163{
164 pi_type::iterator i=pimpl_->pilist_.begin(), end=pimpl_->pilist_.end();
165 for (; i!=end; ++i) if (i->first == socketfd) { pimpl_->pilist_.erase(i); break; }
166 pimpl_->probe_impl_.remove(socketfd);
167}
168//####################################################################
169namespace
170{
171 //####################################################################
172 Netxx::Probe::result_type find_ready_match (ready_return_type &q, Netxx::Probe::ready_type rt)
173 {
174Netxx::Probe::result_type result;
175ready_return_type::iterator q_it=q.begin(), q_end=q.end();
176
177for (; q_it!=q_end; ++q_it) {
178 if (rt & q_it->second) {
179result.first = q_it->first;
180result.second = q_it->second;
181
182q.erase(q_it);
183return result;
184 }
185}
186
187result.first = -1;
188result.second = Netxx::Probe::ready_none;
189
190return result;
191 }
192 //####################################################################
193}
194//####################################################################

Archive Download this file

Branches

Tags

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