4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief SocketSink public header */
31 #ifndef HH_SENF_PPI_SocketSink_
32 #define HH_SENF_PPI_SocketSink_ 1
35 #include <senf/Packets/Packets.hh>
36 #include <senf/Socket/ClientSocketHandle.hh>
37 #include <senf/Socket/SocketPolicy.hh>
38 #include <senf/Socket/ReadWritePolicy.hh>
39 #include <senf/Socket/FramingPolicy.hh>
40 #include <senf/Socket/CommunicationPolicy.hh>
42 #include "Connectors.hh"
43 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
46 //#include "SocketSink.mpp"
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 /** \brief Writer for module::ActiveSocketSink / module::PassiveSocketSink
54 This writer will write the packets completely as datagrams to the given socket which must be connected.
56 class ConnectedDgramWriter
59 typedef senf::ClientSocketHandle<
60 senf::MakeSocketPolicy< senf::WriteablePolicy,
61 senf::DatagramFramingPolicy,
62 senf::ConnectedCommunicationPolicy>::policy > Handle;
63 ///< Handle type supported by this writer
64 typedef Packet PacketType;
66 bool operator()(Handle & handle, Packet const & packet);
67 ///< Write \a packet to \a handle
68 /**< Write the complete \a packet as a datagram to \a
70 \param[in] handle Handle to write data to
71 \param[in] packet Packet to write */
74 /** \brief Writer sending data with ClientSocketHandle::writeto()
76 This writer will send out data using ClientSocketHandle::writeto(). The target address can
77 be specified in the writer constructor and can be adjusted at any time.
79 If no target address is set, incoming data will be <em>silently dropped</em>.
81 template <class HandleType>
82 class TargetDgramWriter
85 typedef HandleType Handle;
86 typedef Packet PacketType;
88 TargetDgramWriter(); ///< Create TargetDgramWriter with unset target address
89 TargetDgramWriter(typename Handle::Address const & target);
90 ///< Create TargetDgramWriter sending to \a target
92 typename Handle::Address target() const; ///< Get current target address
93 void target(typename Handle::Address const & target); ///< Set target address
95 bool operator()(Handle & handle, Packet const & packet); ///< Write \a packet to \a handle
96 /**< Write the complete \a packet as a datagram to \a
98 \param[in] handle Handle to write data to
99 \param[in] packet Packet to write */
102 typename Handle::Address target_;
105 class IPv4SourceForcingDgramWriter : ConnectedDgramWriter
108 IPv4SourceForcingDgramWriter();
109 IPv4SourceForcingDgramWriter(senf::INet4Address sourceAddr, senf::INet4SocketAddress destAddr);
110 typedef senf::ClientSocketHandle<
111 senf::MakeSocketPolicy< senf::WriteablePolicy,
112 senf::DatagramFramingPolicy>::policy > Handle;
113 ///< Handle type supported by this writer
114 typedef Packet PacketType;
116 void source(senf::INet4Address & source);
117 senf::INet4Address source();
118 void destination(senf::INet4SocketAddress & dest);
119 senf::INet4SocketAddress destination();
121 bool operator()(Handle & handle, Packet const & packet);
122 ///< Write \a packet to \a handle
123 /**< Write the complete \a packet as a datagram to \a
125 \param[in] handle Handle to write data to
126 \param[in] packet Packet to write */
128 int sendtoandfrom(int sock, const void *data, size_t dataLen, const in_addr *dst, int port, const in_addr *src);
129 senf::INet4Address source_;
130 senf::INet4Address destination_;
131 unsigned int protocolId_;
134 class IPv6SourceForcingDgramWriter : ConnectedDgramWriter
137 IPv6SourceForcingDgramWriter();
138 IPv6SourceForcingDgramWriter(senf::INet6Address sourceAddr, senf::INet6SocketAddress destAddr);
139 typedef senf::ClientSocketHandle<
140 senf::MakeSocketPolicy< senf::WriteablePolicy,
141 senf::DatagramFramingPolicy>::policy > Handle;
142 ///< Handle type supported by this writer
143 typedef Packet PacketType;
145 void source(senf::INet6Address & source);
146 senf::INet6Address source();
147 void destination(senf::INet6SocketAddress & dest);
148 senf::INet6SocketAddress destination();
150 bool operator()(Handle & handle, Packet const & packet);
151 ///< Write \a packet to \a handle
152 /**< Write the complete \a packet as a datagram to \a
154 \param[in] handle Handle to write data to
155 \param[in] packet Packet to write */
157 int sendtoandfrom(int sock, const void *data, size_t dataLen, const in6_addr *dst, int port, const in6_addr *src);
158 senf::INet6Address source_;
159 senf::INet6Address destination_;
160 unsigned int protocolId_;
170 /** \brief Output %module writing data to a FileHandle using the provided Writer.
171 If using the default ConnectedDgramWriter the filehandle must be writable, connected and
172 able to handle complete datagrams.
174 This output %module will write data to a FileHandle object using a given \a Writer. This
175 output %module is active. This requires the file handle to be able to signal its readiness to
176 accept more data via the Scheduler.
178 The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
181 A \a Writer must fulfill the following interface:
186 typedef unspecified Handle; // type of handle requested
187 typedef unspecified_type PacketType // type of packet read
189 SomeWriter(); // EITHER default constructible OR
190 SomeWriter(SomeWriter const & other); // copy constructible
192 bool operator()(Handle handle, Packet packet); // insertion function
195 Whenever a packet is received for sending, the \a Writer's \c operator() is called.
199 template <class Writer=ConnectedDgramWriter>
200 class ActiveSocketSink : public Module
202 SENF_PPI_MODULE(ActiveSocketSink);
205 typedef typename Writer::Handle Handle; ///< Handle type requested by writer
207 connector::ActiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
209 ActiveSocketSink(); ///< Create non-connected writer
210 /**< The writer will be disabled until a socket is set
211 \pre Requires \a Writer to be default constructible */
212 explicit ActiveSocketSink(Writer const & writer);
213 ///< Create non-connected writer
214 /**< The writer will be disabled until a socket is set
215 \pre Requires \a Writer to be copy constructible
216 \param[in] writer Writer helper writing packet date to
218 explicit ActiveSocketSink(Handle const & handle);
219 ///< Create new writer for the given handle
220 /**< Data will be written to \a handle using \a Writer.
221 \pre Requires \a Writer to be default constructible
222 \param[in] handle Handle to write data to */
223 ActiveSocketSink(Handle const & handle, Writer const & writer);
224 ///< Create new writer for the given handle
225 /**< Data will be written to \a handle using \a Writer.
226 \pre Requires \a Writer to be copy constructible
227 \param[in] handle Handle to write data to
228 \param[in] writer Writer helper writing packet date to
231 Writer & writer(); ///< Access the Writer
232 Handle handle(); ///< Access handle
233 void handle(Handle const & handle);
235 /**< Assigning an empty or in-valid() handle will disable
236 the module until a new. valid handle is assigned. */
246 /** \brief Output module writing data to a FileHandle using the provided \a Writer.
247 If using the default ConnectedDgramWriter the filehandle must be writable, connected and
248 able to handle complete datagrams.
250 This output module will write data to a FileHandle object using a given \a Writer. This
251 output module is passive. This implies, that <em>the output handle may not block</em>. This
252 also implies, that data will probably get lost if written to fast for the underlying
253 transport mechanism. Either this is desired (like for a UDP socket) or some additional
254 bandwidth shaping needs to be used.
256 The default \a Writer is senf::ppi::ConnectedDgramWriter which will write out the complete packet to
259 The \a Writer must fulfill the following interface:
264 typedef unspecified Handle; // type of handle requested
265 typedef unspecified_type PacketType // type of packet read
267 SomeWriter(); // EITHER default constructible
268 SomeWriter(SomeWriter const & other); // OR copy constructible
270 bool operator()(Handle handle, Packet packet); // insertion function
273 Whenever a packet is received for sending, the \a Writer's \c operator() is called.
277 template <class Writer=ConnectedDgramWriter>
278 class PassiveSocketSink : public Module
280 SENF_PPI_MODULE(PassiveSocketSink);
283 typedef typename Writer::Handle Handle; ///< Handle type requested by writer
285 connector::PassiveInput<typename Writer::PacketType> input; ///< Input connector from which data is received
287 PassiveSocketSink(); ///< Create non-connected writer
288 /**< The writer will be disabled until a socket is set
289 \pre Requires \a Writer to be default constructible */
290 explicit PassiveSocketSink(Writer const & writer);
291 ///< Create non-connected writer
292 /**< The writer will be disabled until a socket is set
293 \pre Requires \a Writer to be copy constructible
294 \param[in] writer Writer helper writing packet date to
296 explicit PassiveSocketSink(Handle const & handle);
297 ///< Create new writer for the given handle
298 /**< Data will be written to \a handle using \a Writer.
299 \pre Requires \a Writer to be default constructible
300 \param[in] handle Handle to write data to */
301 PassiveSocketSink(Handle const & handle, Writer const & writer);
302 ///< Create new writer for the given handle
303 /**< Data will be written to \a handle using \a Writer.
304 \pre Requires \a Writer to be copy constructible
305 \param[in] handle Handle to write data to
306 \param[in] writer Writer helper writing packet date to
309 Writer & writer(); ///< Access the Writer
310 Handle & handle(); ///< Access handle
311 void handle(Handle const & handle);
313 /**< Assigning an empty or in-valid() handle will disable
314 the module until a new valid handle is assigned. */
317 void replaceHandle(Handle newHandle);
322 void checkThrottle();
331 //-/////////////////////////////////////////////////////////////////////////////////////////////////
332 #include "SocketSink.cci"
333 #include "SocketSink.ct"
334 #include "SocketSink.cti"
341 // c-file-style: "senf"
342 // indent-tabs-mode: nil
343 // ispell-local-dictionary: "american"
344 // compile-command: "scons -u test"
345 // comment-column: 40