switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / WriteHelper.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 WriteHelper non-inline template implementation */
29
30 //#include "WriteHelper.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::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
41                                                       Callback callback)
42     : handle_(handle),
43       fde_("WriteHelper", boost::bind(&WriteHelper::dispatchProcess, ptr(this), _1, _2),
44            handle, scheduler::FdEvent::EV_WRITE),
45       data_(data), callback_(callback), offset_(data_.begin()), errno_(0)
46 {}
47
48 template <class Handle>
49 prefix_ std::string const & senf::WriteHelper<Handle>::data()
50     const
51 {
52     if (offset_ > data_.begin()) {
53         data_.erase(data_.begin(),offset_);
54         offset_ = data_.begin();
55     }
56     return data_;
57 }
58
59 template <class Handle>
60 prefix_ void senf::WriteHelper<Handle>::revoke()
61 {
62     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
63     fde_.disable();
64     fde_.action(0); // To remove the smart pointer reference to this
65 }
66
67 template <class Handle>
68 prefix_ void senf::WriteHelper<Handle>::dispatchProcess(ptr helper, Handle handle,
69                                                         scheduler::FdEvent::Events event)
70 {
71     // since we have a 'ptr' argument, the instance cannot be deleted
72     // before this method returns
73     helper->process(handle, event);
74 }
75
76 template <class Handle>
77 prefix_ void senf::WriteHelper<Handle>::process(Handle handle,
78                                                 scheduler::FdEvent::Events event)
79 {
80     bool complete_ (false);
81     try {
82         if (event != scheduler::FdEvent::EV_WRITE)
83             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
84         offset_ = handle.write(std::make_pair(offset_,data_.end()));
85         if (offset_ == data_.end()) {
86             data_.erase();
87             offset_ = data_.begin();
88             complete_ = true;
89         }
90     }
91     catch (senf::SystemException const & ex) {
92         errno_ = ex.errorNumber();
93         done();
94         return;
95     }
96     if (complete_)
97         done();
98 }
99
100 template <class Handle>
101 prefix_ void senf::WriteHelper<Handle>::done()
102 {
103     revoke();
104     callback_(ptr(this));
105 }
106
107 //-/////////////////////////////////////////////////////////////////////////////////////////////////
108 #undef prefix_
109
110 \f
111 // Local Variables:
112 // mode: c++
113 // fill-column: 100
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // comment-column: 40
119 // End: