Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[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), handle, _1),
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, int event)
61 {
62     // since we have a 'ptr' argument, the instance cannot be deleted
63     // before this method returns
64     return helper->process(handle,event);
65 }
66
67 template <class Handle>
68 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
69 {
70     try {
71         if (event != senf::Scheduler::EV_READ)
72             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
73         std::string rcv;
74         handle.read(rcv, maxSize_ - data_.size());
75         data_.append(rcv);
76         std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
77         if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
78             complete_ = true;
79             if (n < data_.size()) {
80                 tail_.assign(data_,n,std::string::npos);
81                 data_.erase(n);
82             }
83         }
84     }
85     catch (senf::SystemException const & ex) {
86         errno_ = ex.errorNumber();
87         done();
88         return;
89     }
90     if (complete_)
91         done();
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 // fill-column: 100
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // comment-column: 40
122 // End: