83a557ac2916bdda0e388f89064c518a489eb92a
[senf.git] / Scheduler / ReadHelper.ct
1 // $Id$
2 //
3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 // Copyright (C) 2006 
20
21 /** \file
22     \brief ReadHelper non-inline template implementation */
23
24 #include "ReadHelper.ih"
25
26 // Custom includes
27 #include <errno.h>
28 #include "Utils/membind.hh"
29 #include "Utils/Exception.hh"
30 #include "Scheduler.hh"
31
32 #define prefix_
33 ///////////////////////////////ct.p////////////////////////////////////////
34
35 template <class Handle>
36 prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
37                                                     InternalPredicate * predicate, Callback cb)
38     : handle_(handle), maxSize_(maxSize), predicate_(predicate), callback_(cb), 
39       errno_(0), complete_(false)
40 {
41     // Here we add a *static* member taking a *smart* pointer as first
42     // argumnet instead of a simple bound-member as callback to the
43     // scheduler. This ensures, that the refcount is at least 1 as
44     // long as the helper is registered with the scheduler.
45     senf::Scheduler::instance()
46         .add(handle,boost::bind(&ReadHelper::dispatchProcess,ptr(this),_1,_2),
47              senf::Scheduler::EV_READ);
48 }
49
50 template <class Handle>
51 prefix_ void senf::ReadHelper<Handle>::revoke()
52 {
53     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
54     senf::Scheduler::instance()
55         .remove(handle_,senf::Scheduler::EV_READ);
56 }
57
58 template <class Handle>
59 prefix_ void
60 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
61                                                  senf::Scheduler::EventId event)
62 {
63     // since we have a 'ptr' argument, the instance cannot be deleted
64     // before this method returns
65     return helper->process(handle,event);
66 }
67
68 template <class Handle>
69 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,
70                                                       senf::Scheduler::EventId event)
71 {
72     /** \fixme Move the done() calls to outside the try/catch block */
73     try {
74         if (event != senf::Scheduler::EV_READ)
75             throw SystemException(EPIPE);
76         std::string rcv (handle.read(maxSize_ - data_.size()));
77         data_.append(rcv);
78         std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
79         if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
80             complete_ = true;
81             if (n < data_.size()) {
82                 tail_.assign(data_,n,std::string::npos);
83                 data_.erase(n);
84             }
85             done();
86         }
87     }
88     catch (senf::SystemException const & ex) {
89         errno_ = ex.err;
90         done();
91     }
92 }
93
94 template <class Handle>
95 prefix_ void senf::ReadHelper<Handle>::done()
96 {
97     revoke();
98     callback_(ptr(this));
99 }
100
101 template <class Handle>
102 template <class Predicate>
103 prefix_ std::string::size_type
104 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
105 operator()(std::string const & data)
106 {
107     return predicate(data);
108 }
109
110 ///////////////////////////////ct.e////////////////////////////////////////
111 #undef prefix_
112
113 \f
114 // Local Variables:
115 // mode: c++
116 // End: