flute_file_repair.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <assert.h>
00036 #include <time.h>
00037 
00038 #ifdef _MSC_VER
00039 #include <winsock2.h>
00040 #include <ws2tcpip.h>
00041 #include <windows.h>
00042 #include <process.h>
00043 #else
00044 #include <unistd.h>
00045 #include <pthread.h>
00046 #endif
00047 
00048 #include "flute_file_repair.h"
00049 
00050 #include "../alclib/alc_session.h"
00051 #include "receiver.h"
00052 #include "parse_args.h"
00053 #include "fdt.h"
00054 
00055 #ifdef USE_FILE_REPAIR
00056 
00057 int flute_file_repair(flute_receiver_t *receiver, arguments_t *a, char *sdp_buf) {
00058   
00059   unsigned short i;
00060   int retval = 0;
00061   
00062   char addrs[MAX_CHANNELS_IN_SESSION][INET6_ADDRSTRLEN];        /* Mcast addresses */
00063   char ports[MAX_CHANNELS_IN_SESSION][MAX_PORT_LENGTH]; /* Local port numbers  */
00064   
00065   alc_session_t *old_session;
00066   alc_session_t *new_session;
00067   int new_s_id; /* new session identifier */
00068 
00069 #ifdef _MSC_VER
00070   HANDLE handle_fdt_thread;
00071   unsigned int fdt_thread_id;
00072 #else
00073   pthread_t fdt_thread_id;
00074   int join_retval;
00075 #endif
00076 
00077   time_t systime;
00078   BOOL is_printed = FALSE;
00079   
00080   unsigned long long curr_time;
00081   
00082   if(parse_sdp_file(a, addrs, ports, sdp_buf) == -1) {
00083         return -1;
00084   }
00085   
00086   if(a->alc_a.nb_channel == 0) {
00087         printf("Error: No acceptable channels found in SDP.");
00088         fflush(stdout);
00089         return -1;
00090   }
00091   
00092   if(a->alc_a.stop_time != 0) {
00093         time(&systime);
00094         curr_time = systime + 2208988800U;
00095         
00096         if(a->alc_a.stop_time <= curr_time) {
00097           printf("Session end time reached\n");
00098           fflush(stdout);
00099           return -1;
00100         }
00101   }
00102   
00103   new_s_id = open_alc_session(&a->alc_a);
00104   
00105   /* let's get the old flute session */
00106   old_session = get_alc_session(receiver->s_id);
00107   
00108   /* let's copy needed information from the old session to the new one */
00109   
00110   new_session = get_alc_session(new_s_id);
00111 
00112   new_session->obj_list = get_session_obj_list(receiver->s_id);
00113   new_session->fdt_list = get_session_fdt_list(receiver->s_id);
00114   new_session->wanted_obj_list = get_session_wanted_obj_list(receiver->s_id);
00115 
00116   /* change s_id to new_s_id */
00117   receiver->s_id = new_s_id;
00118   
00119   old_session->obj_list = NULL;
00120   old_session->fdt_list = NULL;
00121   old_session->wanted_obj_list = NULL;
00122   
00123   if(a->alc_a.start_time != 0) {
00124         while(1) {
00125           
00126           time(&systime);
00127           curr_time = systime + 2208988800U;
00128           
00129           if((a->alc_a.start_time - 3) > curr_time) {
00130         
00131         if(!is_printed) {
00132           printf("Waiting for session start time...\n");
00133           fflush(stdout);
00134           is_printed = TRUE;
00135         }
00136 #ifdef _MSC_VER
00137         Sleep(1000);
00138 #else
00139         sleep(1);
00140 #endif
00141           }
00142           else {
00143         break;
00144           }
00145           
00146           if(get_session_state(new_s_id) == SExiting) {
00147                 return -5;
00148           }
00149         }
00150   }
00151   
00152   if(a->alc_a.cc_id == Null) {
00153         
00154         for(i = 0; (int)i < a->alc_a.nb_channel; i++) {
00155           
00156           if(a->alc_a.addr_type == 1) {
00157         retval = add_alc_channel(new_s_id, ports[i], addrs[0], a->alc_a.intface, a->alc_a.intface_name);
00158           }
00159           else {
00160         retval = add_alc_channel(new_s_id, ports[i], addrs[i], a->alc_a.intface, a->alc_a.intface_name);
00161           }
00162           
00163           if(retval == -1) {
00164         close_alc_session(new_s_id);
00165         return -1;
00166           }
00167         }
00168   }
00169   else if(a->alc_a.cc_id == RLC) {
00170         
00171         retval = add_alc_channel(new_s_id, ports[0], addrs[0], a->alc_a.intface, a->alc_a.intface_name);
00172         
00173         if(retval == -1) {
00174           close_alc_session(new_s_id);
00175           return -1;    
00176         }
00177   }
00178 
00179   /* Create FDT receiving thread */
00180   
00181 #ifdef _MSC_VER
00182   handle_fdt_thread =
00183         (HANDLE)_beginthreadex(NULL, 0,
00184                            (void*)fdt_thread, (void*)receiver, 0, &fdt_thread_id);
00185   if(handle_fdt_thread==NULL) {
00186         printf("Error: flute_file_repair, _beginthread\n");
00187         fflush(stdout);
00188         close_alc_session(new_s_id);
00189         return -1;
00190   }
00191 #else
00192   if((pthread_create(&fdt_thread_id, NULL, fdt_thread, (void*)receiver)) != 0) {
00193         printf("Error: flute_file_repair, pthread_create\n");
00194         fflush(stdout);
00195         close_alc_session(new_s_id);
00196         return -1;
00197   }
00198 #endif
00199 
00200   retval = receiver_in_fdt_based_mode(a, receiver);
00201   
00202   set_session_state(new_s_id, SExiting);
00203   
00204 #ifdef _MSC_VER
00205   WaitForSingleObject(get_alc_session(new_s_id)->handle_rx_thread, INFINITE);
00206   CloseHandle(get_alc_session(new_s_id)->handle_rx_thread);
00207 
00208   WaitForSingleObject(handle_fdt_thread, INFINITE);
00209   CloseHandle(handle_fdt_thread);
00210 #else
00211   join_retval = pthread_join(get_alc_session(new_s_id)->rx_thread_id, NULL);
00212   assert(join_retval == 0);
00213   pthread_detach(get_alc_session(new_s_id)->rx_thread_id);
00214   
00215   join_retval = pthread_join(fdt_thread_id, NULL);
00216   assert(join_retval == 0);
00217   pthread_detach(fdt_thread_id);
00218 #endif
00219 
00220   /* Let's point new lists to NULL so that the data is not removed twise with close_alc_session */
00221 
00222   old_session->obj_list = get_session_obj_list(new_s_id);
00223   old_session->fdt_list = get_session_fdt_list(new_s_id);
00224   old_session->wanted_obj_list = get_session_wanted_obj_list(new_s_id);
00225 
00226   new_session->obj_list = NULL;
00227   new_session->fdt_list = NULL;
00228   new_session->wanted_obj_list = NULL;
00229 
00230   close_alc_session(new_s_id);
00231   
00232   sf_free(a->src_filt);
00233   free(a->src_filt);
00234   sdp_message_free(a->sdp);
00235   
00236   return retval;
00237 }
00238 
00239 #endif
00240 

Generated on Fri Mar 9 20:08:52 2007 for MAD-FCL by  doxygen 1.5.0