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