switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / ReadHelper.ct
1 // $Id$
2 //
3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
4 //
5 // The contents of this file are subject to the Fraunhofer FOKUS Public License
6 // Version 1.0 (the "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at 
8 // http://senf.berlios.de/license.html
9 //
10 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
11 // but modifies the Mozilla Public License Version 1.1.
12 // See the full license text for the amendments.
13 //
14 // Software distributed under the License is distributed on an "AS IS" basis, 
15 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
16 // for the specific language governing rights and limitations under the License.
17 //
18 // The Original Code is Fraunhofer FOKUS code.
19 //
20 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
21 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
22 // All Rights Reserved.
23 //
24 // Contributor(s):
25
26 // Copyright (C) 2006
27 /** \file
28     \brief ReadHelper non-inline template implementation */
29
30 #include "ReadHelper.ih"
31
32 // Custom includes
33 #include <boost/bind.hpp>
34 #include <senf/Utils/Exception.hh>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 template <class Handle>
40 prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
41                                                     InternalPredicate * predicate, Callback cb)
42     : handle_(handle),
43       fde_("senf::ReadHelper", boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1),
44            handle, scheduler::FdEvent::EV_READ),
45       maxSize_(maxSize), predicate_(predicate), callback_(cb), errno_(0), complete_(false)
46 {
47     // Here we add a *static* member taking a *smart* pointer as first
48     // argument instead of a simple bound-member as callback to the
49     // scheduler. This ensures, that the refcount is at least 1 as
50     // long as the helper is registered with the scheduler.
51 }
52
53 template <class Handle>
54 prefix_ void senf::ReadHelper<Handle>::revoke()
55 {
56     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
57     fde_.disable();
58     fde_.action(0); // Remove smart pointer reference to this
59 }
60
61 template <class Handle>
62 prefix_ void
63 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle, int event)
64 {
65     // since we have a 'ptr' argument, the instance cannot be deleted
66     // before this method returns
67     helper->process(handle,event);
68 }
69
70 template <class Handle>
71 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
72 {
73     try {
74         if (event != scheduler::FdEvent::EV_READ)
75             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
76         std::string rcv;
77         handle.read(rcv, maxSize_ - data_.size());
78         data_.append(rcv);
79         std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
80         if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
81             complete_ = true;
82             if (n < data_.size()) {
83                 tail_.assign(data_,n,std::string::npos);
84                 data_.erase(n);
85             }
86         }
87     }
88     catch (senf::SystemException const & ex) {
89         errno_ = ex.errorNumber();
90         done();
91         return;
92     }
93     if (complete_)
94         done();
95 }
96
97 template <class Handle>
98 prefix_ void senf::ReadHelper<Handle>::done()
99 {
100     revoke();
101     callback_(ptr(this));
102 }
103
104 template <class Handle>
105 template <class Predicate>
106 prefix_ std::string::size_type
107 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
108 operator()(std::string const & data)
109 {
110     return predicate(data);
111 }
112
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40
125 // End: