b9ba80b0389cdbbf7ecca60836b2c7551f13a24b
[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 <errno.h>
28 #include <boost/bind.hpp>
29 #include <senf/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 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
65                                                         senf::scheduler::FdEvent::Events event)
66 {
67     // since we have a 'ptr' argument, the instance cannot be deleted
68     // before this method returns
69     return helper->process(handle,event);
70 }
71
72 template <class Handle>
73 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
74                                                 senf::scheduler::FdEvent::Events event)
75 {
76     bool complete_ (false);
77     try {
78         if (event != senf::scheduler::FdEvent::EV_WRITE)
79             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
80         offset_ = handle.write(std::make_pair(offset_,data_.end()));
81         if (offset_ == data_.end()) {
82             data_.erase();
83             offset_ = data_.begin();
84             complete_ = true;
85         }
86     }
87     catch (senf::SystemException const & ex) {
88         errno_ = ex.errorNumber();
89         done();
90         return;
91     }
92     if (complete_)
93         done();
94 }
95
96 template <class Handle>
97 prefix_ void senf::WriteHelper<Handle>::done()
98 {
99     revoke();
100     callback_(ptr(this));
101 }
102
103 ///////////////////////////////ct.e////////////////////////////////////////
104 #undef prefix_
105
106 \f
107 // Local Variables:
108 // mode: c++
109 // fill-column: 100
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // comment-column: 40
115 // End: