source: trash/old-modules/transport/protlib/tp_over_uds.h@ 5641

Last change on this file since 5641 was 5641, checked in by Christoph Mayer, 15 years ago
File size: 8.6 KB
Line 
1/// ----------------------------------------*- mode: C++; -*--
2/// @file tp_over_uds.h
3/// Transport over Unix Domain Sockets
4/// ----------------------------------------------------------
5/// $Id: tp_over_uds.h 2872 2008-02-18 10:58:03Z bless $
6/// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/tp_over_uds.h $
7// ===========================================================
8//
9// Copyright (C) 2005-2007, all rights reserved by
10// - Institute of Telematics, Universitaet Karlsruhe (TH)
11//
12// More information and contact:
13// https://projekte.tm.uka.de/trac/NSIS
14//
15// This program is free software; you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation; version 2 of the License
18//
19// This program is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License along
25// with this program; if not, write to the Free Software Foundation, Inc.,
26// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27//
28// ===========================================================
29// ----------------------------------------*- mode: C++; -*--
30// tp_over_uds.h
31// Implementation of a transport module for Unix domain sockets
32// ----------------------------------------------------------
33// $Id: tp_over_uds.h 2872 2008-02-18 10:58:03Z bless $
34// $HeadURL: https://svn.ipv6.tm.uka.de/nsis/protlib/trunk/include/tp_over_uds.h $
35// ==========================================================
36/** @ingroup transport
37 * @file tp_over_uds.h
38 * TP over UDS
39 */
40
41#ifndef TP_OVER_UDS_H
42#define TP_OVER_UDS_H
43
44#include <ext/hash_map>
45
46#include "tp.h"
47#include "threads.h"
48#include "threadsafe_db.h"
49#include "connectionmap_uds.h"
50#include "assocdata_uds.h"
51
52namespace protlib
53{
54/** this struct conatains parameters that determine
55 * the behavior of listener and receiver threads in TPoverUDS
56 * @param port - port number for master listener thread (server port)
57 * @param sleep - time (in ms) that listener and receiver wait at a poll() call
58 * @param d - destination module, where internal message are sent
59 */
60struct TPoverUDSParam : public ThreadParam
61{
62 /// constructor
63 TPoverUDSParam(
64 unsigned short common_header_length,
65 bool (*const getmsglength) (NetMsg& m, uint32& clen_bytes),
66 string udssocket,
67 bool server,
68 uint32 sleep = ThreadParam::default_sleep_time,
69 bool debug_pdu = false,
70 message::qaddr_t source = message::qaddr_transport,
71 message::qaddr_t dest = message::qaddr_signaling,
72 bool sendaborts = false,
73 uint8 tos = 0x10) :
74 ThreadParam(sleep,"TPoverUDS", 1,1),
75 udssocket(udssocket),
76 debug_pdu(debug_pdu),
77 source(source),
78 dest(dest),
79 common_header_length(common_header_length),
80 getmsglength(getmsglength),
81 terminate(false),
82 ip_tos(tos),
83 server(server)
84 {};
85
86 /// port to bind master listener thread to
87 const string udssocket;
88 bool debug_pdu;
89 /// message source
90 const message::qaddr_t source;
91 const message::qaddr_t dest;
92 /// what is the length of the common header
93 const unsigned short common_header_length;
94
95 /// function pointer to a function that figures out the msg length in number of 4 byte words
96 /// it returns false if error occured (e.g., malformed header), result is returned in variable clen_words
97 bool (*const getmsglength) (NetMsg& m, uint32& clen_words);
98
99 /// should master thread terminate?
100 const bool terminate;
101 const uint8 ip_tos;
102 bool server;
103}; // end TPoverUDPParam
104
105
106/// TP over TCP
107/** This class implements the TP interface using TCP. */
108class TPoverUDS : public TP, public Thread
109{
110/***** inherited from TP *****/
111public:
112 /// sends a network message, spawns receiver thread if necessary
113 virtual void send(NetMsg* msg,const address& addr, bool use_existing_connection);
114 virtual void terminate(const address& addr);
115
116 /***** inherited from Thread *****/
117public:
118 /// main loop
119 virtual void main_loop(uint32 nr);
120
121/***** other members *****/
122public:
123 /// constructor
124 TPoverUDS(const TPoverUDSParam& p) :
125 TP(253,"uds",p.name,p.common_header_length,p.getmsglength),
126 Thread(p), tpparam(p), already_aborted(false), msgqueue(NULL), debug_pdu(p.debug_pdu)
127 {
128 // perform some initializing actions
129 // currently not required (SCTP had to init its library)
130 init= true; ///< init done;
131 }
132 /// virtual destructor
133 virtual ~TPoverUDS();
134
135 typedef
136 struct receiver_thread_arg
137 {
138 const AssocDataUDS* peer_assoc;
139 bool sig_terminate;
140 bool terminated;
141 public:
142 receiver_thread_arg(const AssocDataUDS* peer_assoc) :
143 peer_assoc(peer_assoc), sig_terminate(false), terminated(true) {};
144 } receiver_thread_arg_t;
145
146 class receiver_thread_start_arg_t
147 {
148 public:
149 TPoverUDS* instance;
150 receiver_thread_arg_t* rtargp;
151
152 receiver_thread_start_arg_t(TPoverUDS* instance, receiver_thread_arg_t* rtargp) :
153 instance(instance), rtargp(rtargp) {};
154 };
155
156 class sender_thread_start_arg_t
157 {
158 public:
159 TPoverUDS* instance;
160 FastQueue* sender_thread_queue;
161
162 sender_thread_start_arg_t(TPoverUDS* instance, FastQueue* sq) :
163 instance(instance), sender_thread_queue(sq) {};
164 };
165
166private:
167 /// returns already existing connection or establishes a new one
168 AssocDataUDS* get_connection_to(udsaddress& addr);
169
170 /// receiver thread for a specific socket
171 void sender_thread(void *argp);
172
173 /// receiver thread for a specific socket
174 void receiver_thread(void *argp);
175
176 /// send a message to the local process via UNIX domain sockets
177 void udssend(NetMsg* msg, udsaddress* addr);
178
179 /// sender thread starter for a specific socket
180 static void* sender_thread_starter(void *argp);
181
182 /// receiver thread starter for a specific socket
183 static void* receiver_thread_starter(void *argp);
184
185 /// a static starter method to invoke the actual main listener
186 static void* master_listener_thread_starter(void *argp);
187
188 /// main listener thread procedure
189 void master_listener_thread();
190
191 // create and start new sender thread
192 void create_new_sender_thread(FastQueue* senderqueue);
193
194 // create and start new receiver thread
195 void create_new_receiver_thread(AssocDataUDS* peer_assoc);
196
197 /// terminates particular thread
198 void stop_receiver_thread(AssocDataUDS* peer_assoc);
199
200 /// cleans up thread management structures
201 void cleanup_receiver_thread(AssocDataUDS* peer_assoc);
202
203 /// terminates a sender thread
204 void terminate_sender_thread(const AssocDataUDS* assoc);
205
206 /// terminates all active receiver or sender threads
207 void terminate_all_threads();
208
209 /// ConnectionMap instance for keeping track of all existing connections
210 ConnectionMapUDS connmap;
211
212 /// store per receiver thread arguments, e.g. for signaling termination
213 typedef hash_map<pthread_t, receiver_thread_arg_t*> recv_thread_argmap_t;
214 recv_thread_argmap_t recv_thread_argmap;
215
216 /// store sender thread related information
217 typedef hash_map<udsaddress, FastQueue*> sender_thread_queuemap_t;
218 sender_thread_queuemap_t senderthread_queuemap;
219
220 /// parameters for main TPoverUDS thread
221 const TPoverUDSParam tpparam;
222
223 /// did we already abort at thread shutdown
224 bool already_aborted;
225 /// message queue
226 FastQueue* msgqueue;
227
228 bool debug_pdu;
229}; // end class TPoverUDS
230
231/** A simple internal message for selfmessages
232 * please note that carried items may get deleted after use of this message
233 * the message destructor does not delete any item automatically
234 */
235class TPoverUDSMsg : public message
236{
237 public:
238 // message type start/stop thread, send data
239 enum msg_t { start,
240 stop,
241 send_data
242 };
243
244 private:
245 const AssocDataUDS* peer_assoc;
246 const TPoverUDSMsg::msg_t type;
247 NetMsg* netmsg;
248 udsaddress* addr;
249
250public:
251 TPoverUDSMsg(const AssocDataUDS* peer_assoc, message::qaddr_t source= qaddr_unknown, TPoverUDSMsg::msg_t type= stop) :
252 message(type_transport, source), peer_assoc(peer_assoc), type(type), netmsg(0), addr(0) {}
253
254 TPoverUDSMsg(NetMsg* netmsg, udsaddress* addr, message::qaddr_t source= qaddr_unknown) :
255 message(type_transport, source), peer_assoc(0), type(send_data), netmsg(netmsg), addr(addr) {}
256
257 const AssocDataUDS* get_peer_assoc() const { return peer_assoc; }
258 TPoverUDSMsg::msg_t get_msgtype() const { return type; }
259 NetMsg* get_netmsg() const { return netmsg; }
260 udsaddress* get_udsaddr() const { return addr; }
261};
262
263} // end namespace protlib
264
265#endif
Note: See TracBrowser for help on using the repository browser.