16f386b6207ad971591cf46362c2de82d3ff673b
[senf.git] / 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 <errno.h>
28 #include <boost/bind.hpp>
29 #include "../Utils/Exception.hh"
30 #include "Scheduler.hh"
31
32 #define prefix_
33 ///////////////////////////////ct.p////////////////////////////////////////
34
35 template <class Handle>
36 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
37                                                       Callback callback)
38     : handle_(handle), 
39       fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2), 
40            handle, scheduler::FdEvent::EV_WRITE),
41       data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
42 {}
43
44 template <class Handle>
45 prefix_ std::string const & senf::WriteHelper<Handle>::data()
46     const
47 {
48     if (offset_ > data_.begin()) {
49         data_.erase(data_.begin(),offset_);
50         offset_ = data_.begin();
51     }
52     return data_;
53 }
54
55 template <class Handle>
56 prefix_ void senf::WriteHelper<Handle>::revoke()
57 {
58     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
59     fde_.disable();
60     fde_.action(0); // To remove the smart pointer reference to this
61 }
62
63 template <class Handle>
64 prefix_ void
65 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
66                                                   senf::Scheduler::EventId event)
67 {
68     // since we have a 'ptr' argument, the instance cannot be deleted
69     // before this method returns
70     return helper->process(handle,event);
71 }
72
73 template <class Handle>
74 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
75                                                        senf::Scheduler::EventId event)
76 {
77     bool complete_ (false);
78     try {
79         if (event != senf::Scheduler::EV_WRITE)
80             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
81         offset_ = handle.write(std::make_pair(offset_,data_.end()));
82         if (offset_ == data_.end()) {
83             data_.erase();
84             offset_ = data_.begin();
85             complete_ = true;
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::WriteHelper<Handle>::done()
99 {
100     revoke();
101     callback_(ptr(this));
102 }
103
104 ///////////////////////////////ct.e////////////////////////////////////////
105 #undef prefix_
106
107 \f
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40
116 // End: