Move include files in debian packge into 'senf' subdirectory
[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     try {
73         if (event != senf::Scheduler::EV_READ)
74             throw SystemException(EPIPE);
75         std::string rcv;
76         handle.read(rcv, 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         }
86     }
87     catch (senf::SystemException const & ex) {
88         errno_ = ex.err;
89         done();
90         return;
91     }
92     if (complete_)
93         done();
94 }
95
96 template <class Handle>
97 prefix_ void senf::ReadHelper<Handle>::done()
98 {
99     revoke();
100     callback_(ptr(this));
101 }
102
103 template <class Handle>
104 template <class Predicate>
105 prefix_ std::string::size_type
106 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
107 operator()(std::string const & data)
108 {
109     return predicate(data);
110 }
111
112 ///////////////////////////////ct.e////////////////////////////////////////
113 #undef prefix_
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // c-file-style: "senf"
120 // indent-tabs-mode: nil
121 // ispell-local-dictionary: "american"
122 // compile-command: "scons -u test"
123 // comment-column: 40
124 // End: