1 /** \file alc_session.h \brief ALC session
2  *
3  *  $Author: peltotal $ $Date: 2007/02/26 13:48:19 $ $Revision: 1.94 $
4  *
5  *  MAD-ALCLIB: Implementation of ALC/LCT protocols, Compact No-Code FEC,
6  *  Simple XOR FEC, Reed-Solomon FEC, and RLC Congestion Control protocol.
7  *  Copyright (c) 2003-2007 TUT - Tampere University of Technology
8  *  main authors/contacts: jani.peltotalo@tut.fi and sami.peltotalo@tut.fi
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  *  In addition, as a special exception, TUT - Tampere University of Technology
25  *  gives permission to link the code of this program with the OpenSSL library (or
26  *  with modified versions of OpenSSL that use the same license as OpenSSL), and
27  *  distribute linked combinations including the two. You must obey the GNU
28  *  General Public License in all respects for all of the code used other than
29  *  OpenSSL. If you modify this file, you may extend this exception to your version
30  *  of the file, but you are not obligated to do so. If you do not wish to do so,
31  *  delete this exception statement from your version.
32  */
33 
34 #ifndef _ALC_SESSION_H_
35 #define _ALC_SESSION_H_
36 
37 #ifdef _MSC_VER
38 #define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
39 #include <windows.h>
40 #else
41 #include <pthread.h>
42 #endif
43 
44 #include "defines.h"
45 #include "mad.h"
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 /**
52  * ALC session states.
53  * @enum alc_session_states
54  */
55 
56 enum alc_session_states {
57 	SError = -1,	/**< error in the session */
58 	SActive,		/**< session is active */
59 	SExiting,		/**< ctrl^c pressed */
60 	STxStopped,		/**< sender has stopped sending and receiving lists are empty */
61 	SClosed,		/**< session is closed */
62 	SAFlagReceived,	/**< A flag is received */
63 	SPaused			/**< session is paused */
64 };
65 
66 /**
67  * Information for object which is wanted to receive.
68  * @struct wanted_obj
69  */
70 
71 typedef struct wanted_obj {
72 
73   struct wanted_obj *prev;				/**< previous item */
74   struct wanted_obj *next;				/**< next item */
75 
76   unsigned long long toi;				/**< transport object identifier */
77   unsigned long long transfer_len;		/**< transfer length */
78 
79   unsigned short es_len;				/**< encoding symbol length */
80   unsigned int max_sb_len;				/**< maximum source block length */
81   int fec_inst_id;						/**< FEC instance id */
82   short fec_enc_id;						/**< FEC encoding id */
83   unsigned short max_nb_of_es;			/**< maximum number of encoding symbols*/
84   unsigned char content_enc_algo;		/**< content encoding algorithm */
85 
86   unsigned char finite_field;			/**< finite field parameter with new RS FEC */
87   unsigned char nb_of_es_per_group;		/**< number of encoding symbols in packet with new RS FEC */
88 } wanted_obj_t;
89 
90 /**
91  * Structure which stores information for received FDT instance.
92  * @struct rx_fdt_instance
93  */
94 
95 typedef struct rx_fdt_instance {
96 
97     struct rx_fdt_instance *prev;		/**< previous item */
98     struct rx_fdt_instance *next;		/**< next item */
99     unsigned int fdt_instance_id;		/**< FDT instance id*/
100 } rx_fdt_instance_t;
101 
102 /**
103  * Structure for the FLUTE packet when tx_thread() function is used.
104  * @struct tx_queue_struct
105  */
106 
107 typedef struct tx_queue_struct {
108 	unsigned char *data;				/**< data to be sent */
109 	unsigned int datalen;				/**< length of data to be sent */
110 	unsigned int nb_tx_ch;				/**< number of channel where data is sent */
111 	struct tx_queue_struct *next;		/**< next item */
112 } tx_queue_t;
113 
114 /**
115  * Structure for the ALC session.
116  * @struct alc_session
117  */
118 
119 typedef struct alc_session {
120   int s_id;								/**< ALC session identifier */
121   int mode;								/**< mode for the ALC session (sender or receiver) */
122 
123   unsigned long long tsi;				/**< transport session identifier */
124   unsigned long long starttime;			/**< start time of the session */
125   unsigned long long stoptime;			/**< stop time of the session */
126   double ftimestarttime;
127 
128   struct alc_channel *ch_list[MAX_CHANNELS_IN_SESSION];	/**< channels in the session */
129   int nb_channel;						/**< number of channels in the session */
130   int max_channel;				       /**< number of maximum channels in the session */
131 
132   int nb_sending_channel;			/**< number of sending channels */
133   int nb_ready_channel;				/**< number of ready channels */
134 
135   enum alc_session_states state;	/**< state of the session */
136 
137   int addr_family;					/**< address family */
138   int addr_type;					/**< address type, multicast (0) or unicast (1) */
139 
140   int fdt_instance_id;				/**< current FDT instance */
141 
142   struct mad_rlc *rlc;				/**< RLC congestion control */
143 
144   int def_ttl;						/**< time to live */
145   int def_tx_rate;					/**< transmission rate in kbit/s */
146   unsigned short def_eslen;			/**< encoding symbol length */
147   unsigned int def_mxnbes;			/**< maximum number of encoding symbols
148 									that can be generated from one source block */
149   unsigned int def_max_sblen;		/**< maximum source block length */
150   BOOL simul_losses;				/**< simulate packet losses, TRUE = yes, FALSE = no */
151   double loss_ratio1;				/**< packet loss ratio one */
152   double loss_ratio2;				/**< packet loss ratio two */
153   int def_fec_ratio;				/**< FEC ratio percent */
154 
155   unsigned char def_fec_enc_id;		/**< FEC encoding id */
156   unsigned short def_fec_inst_id;	/**< FEC instance id  */
157   int cc_id;						/**< used congestion control, 0 = NULL, 1 = RLC */
158   int use_fec_oti_ext_hdr;			/**< use FEC OTI extension header */
159 
160   int rx_memory_mode;				/**< used memory mode in the receiver */
161   int verbosity;					/**< verbosity level */
162 
163   unsigned long long sent_bytes;		/**< bytes sent in the session */
164   unsigned long long obj_sent_bytes;	/**< bytes sent for object */
165   unsigned long long obj_start_time;	/**< start time for the transport object which sender is currently sending */
166   unsigned long long tx_toi;			/**< transport object which sender is currently sending */
167 
168   double last_print_tx_percent;		/**< last printed transmission percent */
169 
170   int a_flag;						/**< send A flag in the session */
171 
172   tx_queue_t *tx_queue_begin;		/**< first item in transmission queue */
173   tx_queue_t *tx_queue_end;			/**< last item in transmission queue */
174   unsigned int tx_queue_size;		/**< size of transmission queue */
175 
176   BOOL first_unit_in_loop;			/**< first unit in the session sent to be sent? */
177   int encode_content;				/**< encode content, 0 = no, 1 = ZLIB FDT, 2 = ZLIB FDT and GZIP files, 3 = PAD files */
178   BOOL half_word;					/**< use half word flag */
179   BOOL optimize_tx_rate;			/**< optimize transmission rate (use more CPU) */
180   BOOL calculate_session_size;      /**< TRUE if the transmission is simulated and the sent_bytes is calculated */
181 
182 #ifdef SSM
183   BOOL ssm;							/**< use source specific multicast */
184 #endif
185 
186   char base_dir[MAX_PATH_LENGTH];			/**< Base directory for downloaded/sent files */
187   char src_addr[40];						/**< source address for the session in the receiver */
188 
189   unsigned int rx_objs;						/**< number of objects received in this session */
190   struct trans_obj *obj_list;				/**< pointer to first object */
191   struct trans_obj *fdt_list;				/**< pointer to first FDT instance */
192   wanted_obj_t *wanted_obj_list;			/**< pointer to first wanted object */
193   rx_fdt_instance_t *rx_fdt_instance_list;	/**< pointer to first FDT instance information structure */
194 
195 #ifdef _MSC_VER
196   HANDLE handle_rx_thread;					/**< handle to thread which receives packets from the session */
197   unsigned int rx_thread_id;				/**< identifier for thread which receives packets from the session */
198   HANDLE handle_tx_thread;					/**< handle to thread which sents packets to the session */
199   unsigned int tx_thread_id;				/**< identifier for thread which sents packets to the session */
200 #else
201   pthread_t rx_thread_id;					/**< identifier for thread which receives packets from the session */
202   pthread_t tx_thread_id;					/**< identifier for thread which sents packets to the session */
203 #endif
204 
205   unsigned int lost_packets;				/**< how many packets is lost*/
206   int accept_expired_fdt_inst;				/**< accept expired FDT instances */
207 
208 #ifdef USE_RETRIEVE_UNIT
209   struct trans_unit_container *unit_pool;  /**< list of preallocated units */
210   struct trans_unit_container *last_given; /**< last unit returned */
211 #endif
212 
213   BOOL waiting_fdt_instance;				/**< FDT instance is in parsing state */
214 
215 } alc_session_t;
216 
217 /**
218  * This function creates and initializes new session.
219  *
220  * @param a ALC specific commandline arguments
221  *
222  * @return identifier for the created session in success, -1 otherwise
223  *
224  */
225 
226 int open_alc_session(alc_arguments_t *a);
227 
228 /**
229  * This function closes the session.
230  *
231  * @param s_id session identifier
232  *
233  */
234 
235 void close_alc_session(int s_id);
236 
237 /**
238  * This function returns session from the alc_session_list.
239  *
240  * @param s_id session identifier
241  *
242  * @return pointer to session or NULL if session does not exist
243  *
244  */
245 
246 alc_session_t* get_alc_session(int s_id);
247 
248 /**
249  * This function adds channel to the session.
250  * @param s_id session identifier
251  * @param port pointer to the port string
252  * @param addr pointer to the address string
253  * @param intface pointer to the interface string
254  * @param intface_name pointer to the interface name string
255  *
256  * @return identifier for the created channel in success, -1 in error cases
257  *
258  */
259 
260 int add_alc_channel(int s_id, const char *port, const char *addr, const char *intface, const char *intface_name);
261 
262 /**
263  * This function removes channels from session.
264  *
265  * @param s_id session identifier
266  *
267  */
268 
269 void remove_alc_channels(int s_id);
270 
271 /**
272  * This function returns session object list.
273  *
274  * @param s_id session identifier
275  *
276  * @return pointer to the object list, NULL in error cases
277  *
278  */
279 
280 struct trans_obj* get_session_obj_list(int s_id);
281 
282 /**
283  * This function returns session fdt list.
284  *
285  * @param s_id session identifier
286  *
287  * @return pointer to the fdt list, NULL in error cases
288  *
289  */
290 
291 struct trans_obj* get_session_fdt_list(int s_id);
292 
293 /**
294  * This function returns session wanted object list.
295  *
296  * @param s_id session identifier
297  *
298  * @return pointer to the wanted object list, NULL in error cases
299  *
300  */
301 
302 wanted_obj_t* get_session_wanted_obj_list(int s_id);
303 
304 /**
305  * This function returns the state of session.
306  *
307  * @param s_id session identifier
308  *
309  * @return the state of the session, -1 if the session does not exist anymore
310  *
311  */
312 
313 int get_session_state(int s_id);
314 
315 /**
316  * This function sets state of session.
317  *
318  * @param s_id session identifier
319  * @param state new state for the session
320  *
321  */
322 
323 void set_session_state(int s_id, enum alc_session_states state);
324 
325 /**
326  * This function sets state for all sessions.
327  *
328  * @param state new state for all sessions
329  *
330  */
331 
332 void set_all_sessions_state(enum alc_session_states state);
333 
334 /**
335  * This function returns the status of A flag usage in the session.
336  *
337  * @param s_id session identifier
338  *
339  * @return the state of A flag usage (0 = no, 1 = yes)
340  *
341  */
342 
343 int get_session_a_flag_usage(int s_id);
344 
345 /**
346  * This function sets the A flag usage in session.
347  *
348  * @param s_id session identifier
349  *
350  */
351 
352 void set_session_a_flag_usage(int s_id);
353 
354 /**
355  * This function returns session's current FDT instance id.
356  *
357  * @param s_id session identifier
358  *
359  * @return session's FDT instance id
360  *
361  */
362 
363 unsigned int get_fdt_instance_id(int s_id);
364 
365 /**
366  * This function sets session's FDT instance id.
367  *
368  * @param s_id session identifier
369  * @param instance_id FDT instance id to be set
370  *
371  */
372 
373 void set_fdt_instance_id(int s_id, unsigned int instance_id);
374 
375 /**
376  * This function sets FDT instance parsed.
377  *
378  * @param s_id session identifier
379  *
380  */
381 
382 void set_fdt_instance_parsed(int s_id);
383 
384 /**
385  * This function returns the number of bytes sent in the session.
386  *
387  * @param s_id session identifier
388  *
389  * @return number of bytes sent in the session since latest zeroing
390  *
391  */
392 
393 unsigned long long get_session_sent_bytes(int s_id);
394 
395 /**
396  * This function sets the number of bytes sent in the session.
397  *
398  * @param s_id session identifier
399  * @param sent_bytes bytes sent
400  *
401  */
402 
403 void set_session_sent_bytes(int s_id, unsigned long long sent_bytes);
404 
405 /**
406  * This function adds sent bytes in session.
407  *
408  * @param s_id session identifier
409  * @param sent_bytes bytes sent
410  *
411  */
412 
413 void add_session_sent_bytes(int s_id, unsigned int sent_bytes);
414 
415 /**
416  * This function returns the number of bytes sent for the object.
417  *
418  * @param s_id session identifier
419  *
420  * @return number of bytes sent for the object since latest zeroing
421  *
422  */
423 
424 unsigned long long get_object_sent_bytes(int s_id);
425 
426 /**
427  * This function sets number of bytes sent for the current object.
428  *
429  * @param s_id session identifier
430  * @param sent_bytes bytes sent
431  *
432  */
433 
434 void set_object_sent_bytes(int s_id, unsigned long long sent_bytes);
435 
436 /**
437  * This function adds sent bytes for the object.
438  *
439  * @param s_id session identifier
440  * @param sent_bytes bytes sent
441  *
442  */
443 
444 void add_object_sent_bytes(int s_id, unsigned int sent_bytes);
445 
446 /**
447  * This function returns last printed percent for the object.
448  *
449  * @param s_id session identifier
450  *
451  * @return last printed percent
452  *
453  */
454 
455 double get_object_last_print_tx_percent(int s_id);
456 
457 /**
458  * This function sets last printed percent for the object.
459  *
460  * @param s_id session identifier
461  * @param last_print_tx_percent printed percent
462  *
463  */
464 
465 void set_object_last_print_tx_percent(int s_id, double last_print_tx_percent);
466 
467 /**
468  * This function returns identifier for the object which sender is currently sending.
469  *
470  * @param s_id session identifier
471  *
472  * @return transport object identifier for the object which sender is currently sending
473  *
474  */
475 
476 unsigned long long get_session_tx_toi(int s_id);
477 
478 /**
479  * This function sets transport object which sender is currently sending.
480  *
481  * @param s_id session identifier
482  * @param toi transport object identifier for the object which sender is currently sending
483  *
484  */
485 
486 void set_session_tx_toi(int s_id, unsigned long long toi);
487 
488 /**
489  * This function sets state of session.
490  *
491  * @param s_id session identifier
492  * @param base_tx_rate new base tx rate for the session
493  *
494  */
495 
496 void update_session_tx_rate(int s_id, int base_tx_rate);
497 
498 /**
499  * This function checks if object identified with TOI is wanted and returns pointer to the wanted object structure.
500  *
501  * @param s pointer to the session
502  * @param toi transport object identifier to be checked
503  *
504  * @return pointer to wanted object structure in success, NULL otherwise
505  *
506  */
507 
508 wanted_obj_t* get_wanted_object(alc_session_t *s, unsigned long long toi);
509 
510 /**
511  * This function set object wanted from the session.
512  *
513  * @param s_id session identifier
514  * @param toi transport object identifier
515  * @param transfer_len transport object length
516  * @param es_len encoding symbol length
517  * @param max_sb_len maximum source block length
518  * @param fec_inst_id FEC instance id
519  * @param fec_enc_id FEC encoding id
520  * @param max_nb_of_enc_symb maximum number of encoding symbols
521  * @param content_enc_algo content encoding algorithm
522  * @param finite_field finite field parameter with new RS FEC
523  * @param nb_of_es_per_group number of encoding symbols per FLUTE packet
524  * @param content_len the size of file corresponding this transport object
525  * @param file_uri file URI for the file corresponding this transport object
526  *
527  * @return 0 in success, -1 otherwise
528  *
529  */
530 
531 int set_wanted_object(int s_id,
532 		      unsigned long long toi,
533 		      unsigned long long transfer_len,
534 		      unsigned short es_len, unsigned int max_sb_len, int fec_inst_id,
535 		      short fec_enc_id, unsigned short max_nb_of_enc_symb,
536 		      unsigned char content_enc_algo, unsigned char finite_field,
537 			  unsigned char nb_of_es_per_group
538 		      );
539 
540 /**
541  * This function removes wanted object identified with toi from the session.
542  *
543  * @param s_id session identifier
544  * @param toi transport object identifier
545  *
546  */
547 
548 void remove_wanted_object(int s_id, unsigned long long toi);
549 
550 /**
551  * This function checks if FDT Instance is already received
552  *
553  * @param s pointer to the session
554  * @param fdt_instance_id FDT instance id to be checked
555  *
556  * @return TRUE if FDT instance is already received, FALSE otherwise
557  *
558  */
559 
560 BOOL is_received_instance(alc_session_t *s, unsigned int fdt_instance_id);
561 
562 /**
563  * This function sets FDT Instance to the received list.
564  *
565  * @param s pointer to the session
566  * @param fdt_instance_id FDT instance id to be set
567  *
568  * @return 0 in success, -1 otherwise
569  *
570  */
571 
572 int set_received_instance(alc_session_t *s, unsigned int fdt_instance_id);
573 
574 /**
575  * This function returns session's base directory.
576  *
577  * @param s_id session identifier
578  *
579  * @return pointer to base directory
580  *
581  */
582 
583 char* get_session_basedir(int s_id);
584 
585 /**
586  * This function initializes session handler.
587  *
588  */
589 
590 void initialize_session_handler();
591 
592 /**
593  * This function releases session handler.
594  *
595  */
596 
597 void release_session_handler();
598 
599 #ifdef __cplusplus
600 }; //extern "C"
601 #endif
602 
603 #endif /* _ALC_SESSION_H_ */
604 
605