Merge pull request #83 from eadrkir/master
[deliverable/titan.core.git] / core / Port.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Baji, Laszlo
10 * Balasko, Jeno
11 * Baranyi, Botond
12 * Feher, Csaba
13 * Raduly, Csaba
14 * Szabados, Kristof
15 * Szabo, Bence Janos
16 * Szabo, Janos Zoltan – initial implementation
17 * Tatarka, Gabor
18 *
19 ******************************************************************************/
20 #ifndef PORT_HH
21 #define PORT_HH
22
23 #include <sys/types.h>
24
25 #include "Types.h"
26 #include "Event_Handler.hh"
27 #include <stddef.h> // only for NULL
28 #include <sys/select.h>
29
30 class COMPONENT;
31 class COMPONENT_template;
32 class Text_Buf;
33 class OCTETSTRING;
34 class CHARSTRING;
35
36 extern const COMPONENT_template& any_compref;
37
38 struct port_connection; // no user serviceable parts inside
39
40 /** Base class for all test ports */
41 class PORT : public Fd_And_Timeout_Event_Handler {
42 friend class PORT_LIST;
43 friend struct port_connection;
44
45 static PORT *list_head, *list_tail;
46
47 void add_to_list();
48 void remove_from_list();
49 static PORT *lookup_by_name(const char *par_port_name);
50
51 struct port_parameter; // no user serviceable parts inside
52 static port_parameter *parameter_head, *parameter_tail;
53
54 /** @brief Apply the given port parameter.
55 *
56 * Applies the parameter to the appropriate port, or all ports if it's
57 * a parameter for all ports ( "*" )
58 * @param par_ptr pointer to the port parameter
59 */
60 static void apply_parameter(port_parameter *par_ptr);
61 void set_system_parameters(const char *system_port);
62
63 public:
64 struct msg_queue_item_base {
65 struct msg_queue_item_base *next_item;
66 };
67 msg_queue_item_base *msg_queue_head, *msg_queue_tail;
68 public:
69 /** @brief Store a port parameter
70 *
71 * @param component_id component identifier
72 * @param par_port_name string, name of the port, NULL if the parameter
73 * refers to all ports ( "*" )
74 * @param parameter_name string, name of the parameter
75 * @param parameter_value string, the value
76 */
77 static void add_parameter(const component_id_t& component_id,
78 const char *par_port_name, const char *parameter_name,
79 const char *parameter_value);
80 /** Deallocates the list of port parameters */
81 static void clear_parameters();
82 /** @brief Apply port parameters to component
83
84 Iterates through all known port parameters and
85
86 @param component_reference
87 @param component_name
88 */
89 static void set_parameters(component component_reference,
90 const char *component_name);
91
92 protected:
93 const char *port_name;
94 unsigned int msg_head_count, msg_tail_count, proc_head_count,
95 proc_tail_count;
96 boolean is_active, is_started, is_halted;
97
98 private:
99 int n_system_mappings;
100 char **system_mappings;
101 PORT *list_prev, *list_next;
102 port_connection *connection_list_head, *connection_list_tail;
103
104 private:
105 /// Copy constructor disabled.
106 PORT(const PORT& other_port);
107 /// Assignment disabled.
108 PORT& operator=(const PORT& other_port);
109
110 public:
111 PORT(const char *par_port_name);
112 virtual ~PORT();
113
114 inline const char *get_name() const { return port_name; }
115 void set_name(const char * name);
116
117 virtual void log() const;
118
119 void activate_port();
120 void deactivate_port();
121 static void deactivate_all();
122
123 void clear();
124 static void all_clear();
125 void start();
126 static void all_start();
127 void stop();
128 static void all_stop();
129 void halt();
130 static void all_halt();
131
132 bool check_port_state(const CHARSTRING& type) const;
133 static bool any_check_port_state(const CHARSTRING& type);
134 static bool all_check_port_state(const CHARSTRING& type);
135
136 virtual alt_status receive(const COMPONENT_template& sender_template =
137 any_compref, COMPONENT *sender_ptr = NULL);
138 static alt_status any_receive(const COMPONENT_template& sender_template =
139 any_compref, COMPONENT *sender_ptr = NULL);
140 virtual alt_status check_receive(const COMPONENT_template&
141 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
142 static alt_status any_check_receive(const COMPONENT_template&
143 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
144
145 virtual alt_status trigger(const COMPONENT_template& sender_template =
146 any_compref, COMPONENT *sender_ptr = NULL);
147 static alt_status any_trigger(const COMPONENT_template& sender_template =
148 any_compref, COMPONENT *sender_ptr = NULL);
149
150 virtual alt_status getcall(const COMPONENT_template& sender_template =
151 any_compref, COMPONENT *sender_ptr = NULL);
152 static alt_status any_getcall(const COMPONENT_template& sender_template =
153 any_compref, COMPONENT *sender_ptr = NULL);
154 virtual alt_status check_getcall(const COMPONENT_template&
155 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
156 static alt_status any_check_getcall(const COMPONENT_template&
157 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
158
159 virtual alt_status getreply(const COMPONENT_template& sender_template =
160 any_compref, COMPONENT *sender_ptr = NULL);
161 static alt_status any_getreply(const COMPONENT_template& sender_template =
162 any_compref, COMPONENT *sender_ptr = NULL);
163 virtual alt_status check_getreply(const COMPONENT_template&
164 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
165 static alt_status any_check_getreply(const COMPONENT_template&
166 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
167
168 virtual alt_status get_exception(const COMPONENT_template&
169 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
170 static alt_status any_catch(const COMPONENT_template& sender_template =
171 any_compref, COMPONENT *sender_ptr = NULL);
172 virtual alt_status check_catch(const COMPONENT_template&
173 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
174 static alt_status any_check_catch(const COMPONENT_template&
175 sender_template = any_compref, COMPONENT *sender_ptr = NULL);
176
177 alt_status check(const COMPONENT_template& sender_template = any_compref,
178 COMPONENT *sender_ptr = NULL);
179 static alt_status any_check(const COMPONENT_template& sender_template =
180 any_compref, COMPONENT *sender_ptr = NULL);
181
182 /** Set a parameter on the port.
183 * @param parameter_name string
184 * @param parameter_value
185 *
186 * The implementation in the PORT base class issues a warning and
187 * does nothing. Derived classes need to override this method.
188 */
189 virtual void set_parameter(const char *parameter_name,
190 const char *parameter_value);
191
192 void append_to_msg_queue(msg_queue_item_base*);
193 private:
194 /** Callback interface for handling events - introduced in TITAN R7E
195 * To use the finer granularity interface, this method must not be
196 * overridden in the descendant Test Port class.
197 * Note: Error event always triggers event handler call and is indicated in
198 * the is_error parameter even if not requested.
199 */
200 virtual void Handle_Fd_Event(int fd,
201 boolean is_readable, boolean is_writable, boolean is_error);
202
203 /** Callback interface for handling timeout - introduced in TITAN R7E
204 * May not be overridden in the descendant Test Port class if
205 * timeout is not used.
206 */
207 virtual void Handle_Timeout(double time_since_last_call);
208
209 /** Callback interface for handling events - introduced in TITAN R7E
210 * These methods in the descendant Test Port class are called only if
211 * Handle_Fd_Event is not overridden in the descendant Test Port class.
212 * The method handling an event which is not used in the Test Port
213 * may not be overridden in the descendant Test Port class.
214 * This is true even for the error event handler, although error events
215 * always trigger event handler call even if not requested.
216 * (There is an empty default implementation for the error event handler.)
217 */
218 virtual void Handle_Fd_Event_Error(int fd);
219 virtual void Handle_Fd_Event_Writable(int fd);
220 virtual void Handle_Fd_Event_Readable(int fd);
221
222 public:
223 /** Callback interface for handling events
224 * This method is provided for Test Ports developed for TITAN versions
225 * before R7E.
226 * (It is called only if event handler has been registered with
227 * Install_Handler.)
228 */
229 virtual void Event_Handler(const fd_set *read_fds, const fd_set *write_fds,
230 const fd_set *error_fds, double time_since_last_call);
231
232 protected:
233 /** Interface for handling events and timeout - introduced in TITAN R7E */
234 typedef enum {
235 EVENT_RD = FD_EVENT_RD, EVENT_WR = FD_EVENT_WR,
236 EVENT_ERR = FD_EVENT_ERR,
237 EVENT_ALL = FD_EVENT_RD | FD_EVENT_WR | FD_EVENT_ERR
238 } Fd_Event_Type;
239 void Handler_Add_Fd(int fd, Fd_Event_Type event_mask = EVENT_ALL);
240 void Handler_Add_Fd_Read(int fd);
241 void Handler_Add_Fd_Write(int fd);
242 void Handler_Remove_Fd(int fd, Fd_Event_Type event_mask = EVENT_ALL);
243 void Handler_Remove_Fd_Read(int fd);
244 void Handler_Remove_Fd_Write(int fd);
245 void Handler_Set_Timer(double call_interval, boolean is_timeout = TRUE,
246 boolean call_anyway = TRUE, boolean is_periodic = TRUE);
247
248 /** Interface for handling events and timeout
249 * This method is provided for Test Ports developed for TITAN versions
250 * before R7E.
251 */
252 void Install_Handler(const fd_set *read_fds, const fd_set *write_fds,
253 const fd_set *error_fds, double call_interval);
254 /** Interface for handling events and timeout
255 * This method is in use in Test Ports developed for TITAN versions
256 * before R7E.
257 * It can be used together with the interface introduced in TITAN R7E.
258 */
259 void Uninstall_Handler();
260
261 virtual void user_map(const char *system_port);
262 virtual void user_unmap(const char *system_port);
263
264 virtual void user_start();
265 virtual void user_stop();
266
267 virtual void clear_queue();
268
269 component get_default_destination();
270
271 static void prepare_message(Text_Buf& outgoing_buf,
272 const char *message_type);
273 static void prepare_call(Text_Buf& outgoing_buf,
274 const char *signature_name);
275 static void prepare_reply(Text_Buf& outgoing_buf,
276 const char *signature_name);
277 static void prepare_exception(Text_Buf& outgoing_buf,
278 const char *signature_name);
279 void send_data(Text_Buf& outgoing_buf,
280 const COMPONENT& destination_component);
281
282 void process_data(port_connection *conn_ptr, Text_Buf& incoming_buf);
283 virtual boolean process_message(const char *message_type,
284 Text_Buf& incoming_buf, component sender_component, OCTETSTRING&);
285 virtual boolean process_call(const char *signature_name,
286 Text_Buf& incoming_buf, component sender_component);
287 virtual boolean process_reply(const char *signature_name,
288 Text_Buf& incoming_buf, component sender_component);
289 virtual boolean process_exception(const char *signature_name,
290 Text_Buf& incoming_buf, component sender_component);
291
292 private:
293 port_connection *add_connection(component remote_component,
294 const char *remote_port, transport_type_enum transport_type);
295 void remove_connection(port_connection *conn_ptr);
296 port_connection *lookup_connection_to_compref(component remote_component,
297 boolean *is_unique);
298 port_connection *lookup_connection(component remote_component,
299 const char *remote_port);
300 void add_local_connection(PORT *other_endpoint);
301 void remove_local_connection(port_connection *conn_ptr);
302
303 static unsigned int get_connection_hash(component local_component,
304 const char *local_port, component remote_component,
305 const char *remote_port);
306 static void unlink_unix_pathname(int socket_fd);
307 void connect_listen_inet_stream(component remote_component,
308 const char *remote_port);
309 void connect_listen_unix_stream(component remote_component,
310 const char *remote_port);
311 void connect_local(component remote_component, const char *remote_port);
312 void connect_stream(component remote_component, const char *remote_port,
313 transport_type_enum transport_type, Text_Buf& text_buf);
314 void disconnect_local(port_connection *conn_ptr);
315 void disconnect_stream(port_connection *conn_ptr);
316
317 void send_data_local(port_connection *conn_ptr, Text_Buf& outgoing_data);
318 boolean send_data_stream(port_connection *conn_ptr, Text_Buf& outgoing_data,
319 boolean ignore_peer_disconnect);
320
321 void handle_incoming_connection(port_connection *conn_ptr);
322 void handle_incoming_data(port_connection *conn_ptr);
323 void process_last_message(port_connection *conn_ptr);
324
325 void map(const char *system_port);
326 void unmap(const char *system_port);
327
328 public:
329 static void process_connect_listen(const char *local_port,
330 component remote_component, const char *remote_port,
331 transport_type_enum transport_type);
332 static void process_connect(const char *local_port,
333 component remote_component, const char *remote_port,
334 transport_type_enum transport_type, Text_Buf& text_buf);
335 static void process_disconnect(const char *local_port,
336 component remote_component, const char *remote_port);
337 static void make_local_connection(const char *src_port,
338 const char *dest_port);
339 static void terminate_local_connection(const char *src_port,
340 const char *dest_port);
341
342 static void map_port(const char *component_port, const char *system_port);
343 static void unmap_port(const char *component_port, const char *system_port);
344 };
345
346 #endif
This page took 0.038909 seconds and 5 git commands to generate.