d05a9c8ae2715ebe0772c66d6f09dd7be0140fc1
[senf.git] / senf / Scheduler / WriteHelper.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 WriteHelper non-inline template implementation */
23
24 //#include "WriteHelper.ih"
25
26 // Custom includes
27 #include <boost/bind.hpp>
28 #include <senf/Utils/Exception.hh>
29
30 #define prefix_
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32
33 template <class Handle>
34 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
35                                                       Callback callback)
36     : handle_(handle),
37       fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
38            handle, scheduler::FdEvent::EV_WRITE),
39       data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
40 {}
41
42 template <class Handle>
43 prefix_ std::string const & senf::WriteHelper<Handle>::data()
44     const
45 {
46     if (offset_ > data_.begin()) {
47         data_.erase(data_.begin(),offset_);
48         offset_ = data_.begin();
49     }
50     return data_;
51 }
52
53 template <class Handle>
54 prefix_ void senf::WriteHelper<Handle>::revoke()
55 {
56     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
57     fde_.disable();
58     fde_.action(0); // To remove the smart pointer reference to this
59 }
60
61 template <class Handle>
62 prefix_ void senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
63                                                         scheduler::FdEvent::Events 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::WriteHelper<Handle>::process(Handle handle,
72                                                 scheduler::FdEvent::Events event)
73 {
74     bool complete_ (false);
75     try {
76         if (event != scheduler::FdEvent::EV_WRITE)
77             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
78         offset_ = handle.write(std::make_pair(offset_,data_.end()));
79         if (offset_ == data_.end()) {
80             data_.erase();
81             offset_ = data_.begin();
82             complete_ = true;
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::WriteHelper<Handle>::done()
96 {
97     revoke();
98     callback_(ptr(this));
99 }
100
101 //-/////////////////////////////////////////////////////////////////////////////////////////////////
102 #undef prefix_
103
104 \f
105 // Local Variables:
106 // mode: c++
107 // fill-column: 100
108 // c-file-style: "senf"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -u test"
112 // comment-column: 40
113 // End: