Fixed whitespace in all files (no tabs)
[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 "Utils/Exception.hh"
29 #include "Scheduler.hh"
30
31 #define prefix_
32 ///////////////////////////////ct.p////////////////////////////////////////
33
34 template <class Handle>
35 prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string data,
36                                                       Callback callback)
37     : handle_(handle), data_(data), callback_(callback),
38       offset_(0), errno_(0)
39 {
40     senf::Scheduler::instance()
41         .add(handle_, boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
42              senf::Scheduler::EV_WRITE);
43 }
44
45 template <class Handle>
46 prefix_ std::string const & senf::WriteHelper<Handle>::data()
47     const
48 {
49     if (offset_ > 0) {
50         data_.erase(0,offset_);
51         offset_ = 0;
52     }
53     return data_;
54 }
55
56 template <class Handle>
57 prefix_ void senf::WriteHelper<Handle>::revoke()
58 {
59     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
60     senf::Scheduler::instance()
61         .remove(handle_, senf::Scheduler::EV_WRITE);
62 }
63
64 template <class Handle>
65 prefix_ void
66 senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
67                                                   senf::Scheduler::EventId event)
68 {
69     // since we have a 'ptr' argument, the instance cannot be deleted
70     // before this method returns
71     return helper->process(handle,event);
72 }
73
74 template <class Handle>
75 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
76                                                        senf::Scheduler::EventId event)
77 {
78     /** \fixme Move the done() calls to outside the try/catch block */
79     try {
80         if (event != senf::Scheduler::EV_WRITE)
81             throw senf::SystemException(EPIPE);
82         offset_ += handle.write(data_.data()+offset_,data_.size()-offset_);
83         if (offset_ >= data_.size()) {
84             data_.erase();
85             done();
86         }
87     }
88     catch (senf::SystemException const & ex) {
89         errno_ = ex.err;
90         done();
91     }
92 }
93
94 template <class Handle>
95 prefix_ void senf::WriteHelper<Handle>::done()
96 {
97     revoke();
98     callback_(ptr(this));
99 }
100
101 ///////////////////////////////ct.e////////////////////////////////////////
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 // End: