0d9296271da495afc7848162d09241c9dbf47e92
[senf.git] / senf / Scheduler / WriteHelper.hh
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 public header */
23
24 #ifndef HH_SENF_Scheduler_WriteHelper_
25 #define HH_SENF_Scheduler_WriteHelper_ 1
26
27 // Custom includes
28 #include <string>
29 #include <boost/intrusive_ptr.hpp>
30 #include <boost/function.hpp>
31 #include <senf/Utils/intrusive_refcount.hh>
32 #include "FdEvent.hh"
33
34 //#include "WriteHelper.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     /** \brief Asynchronous writing helper
40
41         This class provides a simple asynchronous writing facility. This helper will register with
42         the Scheduler to write the requested data. It will stay registered until the data has been
43         completely sent or some error condition is encountered. As soon as the WriteHelper is done,
44         the callback will be called.
45
46         The WriteHelper accepts the same flexible file handle interfaces as the Scheduler.
47
48         The callback must take a WriteHelper::ptr argument. Using this WriteHelper instance, the
49         callback can access the state information and check the termination status.
50
51         \todo Add additional interface to better access the intermediate status (data sent so far)
52      */
53     template <class Handle>
54     class WriteHelper
55         : public senf::intrusive_refcount
56     {
57     public:
58         ///////////////////////////////////////////////////////////////////////////
59         // Types
60
61         typedef boost::intrusive_ptr<WriteHelper> ptr; ///< Smart pointer type for this class
62         typedef boost::function<void (ptr)> Callback; ///< Callback type
63
64         ///////////////////////////////////////////////////////////////////////////
65         ///\name Structors and default members
66         ///@{
67
68         static ptr dispatch(Handle handle, std::string const & data, Callback callback);
69                                         ///< Register new WriteHelper instance
70                                         /**< The registered callback will be called after all \a
71                                              data has been sent or when some error condition is
72                                              encountered.
73                                              \param[in] handle file descriptor or handle providing
74                                                  the Handle interface defined above.
75                                              \param[in] data data to send
76                                              \param[in] callback callback
77                                              \returns smart pointer to new WriteHelper instance */
78
79         ///@}
80         ///////////////////////////////////////////////////////////////////////////
81
82         Handle handle() const;
83
84         std::string const & data() const; ///< Return the data
85                                         /**< After all data has been sent, this member will return
86                                              an empty string. Until then, the complete string will
87                                              be returned. */
88
89         bool complete() const;          ///< Check whether the write has completed successfully
90         bool error() const;             ///< Check for error condition
91         void throw_error() const;       ///< If an error occurred, throw it
92
93         void revoke();                  ///< Remove the WriteHelper from the scheduler
94
95     protected:
96
97     private:
98         WriteHelper(Handle handle, std::string const & data, Callback callback);
99
100         static void dispatchProcess(ptr helper, Handle handle,
101                                     senf::scheduler::FdEvent::Events event);
102         void process(Handle handle, senf::scheduler::FdEvent::Events event);
103         void done();
104
105         Handle handle_;
106         scheduler::FdEvent fde_;
107         mutable std::string data_;
108         Callback callback_;
109
110         mutable std::string::iterator offset_;
111         int errno_;
112     };
113
114
115 }
116
117 ///////////////////////////////hh.e////////////////////////////////////////
118 //#include "WriteHelper.cci"
119 #include "WriteHelper.ct"
120 #include "WriteHelper.cti"
121 //#include "WriteHelper.mpp"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // comment-column: 40
133 // End: