alc_channel.c

Go to the documentation of this file.
00001 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <math.h>
00037 #include <sys/types.h>
00038 #include <assert.h>
00039 #include <string.h>
00040 
00041 #ifdef _MSC_VER
00042 #include <process.h>
00043 #else
00044 #include <sys/socket.h>
00045 #include <netdb.h>
00046 #endif
00047 
00048 #include "alc_channel.h"
00049 #include "alc_socket.h"
00050 #include "alc_list.h"
00051 #include "alc_rx.h"
00052 
00053 int open_alc_channel(alc_channel_t *ch, alc_session_t *s, const char *port,
00054                      const char *addr, const char *intface, const char *intface_name, int tx_rate) {
00055   
00056   int ret_val;
00057   
00058   if (!(ch = (alc_channel_t*)calloc(1, sizeof(alc_channel_t)))) {
00059     printf("Could not alloc memory for  alc channel!\n");
00060     return -1;
00061   }
00062   
00063   ch->ch_id = s->nb_channel; /* session level identifier for channel */
00064   ch->s = s;
00065   
00066   if(s->mode == SENDER) {
00067     if(ch->ch_id == 0) {
00068       
00069       if(ch->s->cc_id == Null) {
00070         ch->tx_rate = tx_rate;
00071         ch->nb_tx_units = 1;
00072         ch->ready = FALSE;
00073         
00074         s->nb_sending_channel++;
00075       }
00076       else if(ch->s->cc_id == RLC) {
00077         ch->tx_rate = tx_rate;
00078         ch->nb_tx_units = 1;
00079         ch->start_sending = TRUE;
00080         ch->ready = FALSE;
00081         ch->wait_after_sp = 0;
00082         
00083         s->nb_sending_channel++;
00084       }
00085     }
00086     else {
00087       
00088       if(ch->s->cc_id == Null) {
00089         ch->tx_rate = tx_rate * (int)pow(2.0, (double)(ch->ch_id - 1));
00090         ch->nb_tx_units = (int)pow(2.0, (double)(ch->ch_id - 1));
00091         ch->ready = FALSE;
00092         
00093         s->nb_sending_channel++;
00094       }                 
00095       else if(ch->s->cc_id == RLC) {
00096         ch->tx_rate = tx_rate * (int)pow(2.0, (double)(ch->ch_id - 1));
00097         ch->nb_tx_units = (int)pow(2.0, (double)(ch->ch_id - 1));
00098         ch->start_sending = FALSE;
00099         ch->ready = FALSE;
00100         ch->wait_after_sp = RLC_WAIT_AFTER_SP;
00101       }
00102     }
00103     
00104     ch->addr = addr;
00105     ch->port = port;
00106     ch->intface = intface;
00107     
00108     ch->previous_lost = FALSE;
00109   }
00110   else if(s->mode == RECEIVER) {
00111     ch->addr = addr;
00112     ch->port = port;
00113     ch->intface = intface;
00114     ch->intface_name = intface_name;
00115   }
00116   
00117   ret_val = init_alc_socket(ch);
00118   
00119   if(ret_val < 0) {
00120     return ret_val;
00121   }
00122   
00123   assert(s->ch_list[s->nb_channel] == NULL);
00124 
00125   if(s->mode == RECEIVER) {
00126 
00127         assert(ch->receiving_list == 0);
00128         ch->receiving_list = build_list();
00129 
00130         /* Create receiving socket thread */
00131 #ifdef _MSC_VER
00132         ch->handle_rx_socket_thread = (HANDLE)_beginthreadex(NULL, 0, (void*)rx_socket_thread,
00133                                                                   (void*)ch, 0, &ch->rx_socket_thread_id);
00134 
00135         if(ch->handle_rx_socket_thread == NULL) {
00136                 perror("open_alc_channel: _beginthread");
00137                 return -1;
00138         }
00139 #else
00140         if(pthread_create(&ch->rx_socket_thread_id, NULL, rx_socket_thread, (void*)ch) != 0) {
00141                 perror("open_alc_channel: pthread_create");
00142                 return -1;
00143         }
00144 #endif
00145   }
00146 
00147   s->ch_list[s->nb_channel] = ch;
00148   s->nb_channel++;
00149   
00150   return ch->ch_id;
00151 }
00152 
00153 int close_alc_channel(alc_channel_t *ch, alc_session_t *s) {
00154         
00155   int ret_val;
00156   int ch_id = ch->ch_id;
00157   
00158   ret_val = close_alc_socket(ch);
00159   
00160   /* trying again if ret_val == -1 */
00161   if(ret_val == -1) {
00162     ret_val = close_alc_socket(ch);
00163   }     
00164   
00165   assert(s->ch_list[ch_id] == ch);
00166   freeaddrinfo(ch->addrinfo);
00167 
00168   if(s->mode == RECEIVER) {
00169         join_rx_socket_thread(ch);
00170         destroy_list(ch->receiving_list);
00171         ch->receiving_list = NULL;
00172   }
00173 
00174   assert(ch->receiving_list == NULL);
00175 
00176   free(ch);
00177   s->ch_list[ch_id] = NULL;
00178   
00179   s->nb_channel--;
00180   
00181   return ret_val;
00182 }
00183 

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