Main Page | Class List | File List | Class Members | File Members

rcpc.c

Go to the documentation of this file.
00001 #include "standard.h"
00002 #include "rcpc.h"
00003 #include "Buffer.h"
00004 #include "arq.h"
00005 #include "between.h"
00006 #include "error.h"
00007 
00008 int max_arq(int rtype)
00009 /*
00010  * returns the most arq's that can be sent for a given rate type rtype
00011  */
00012 {
00013   int rval;
00014 
00015   switch (rtype) {
00016     case 24:
00017       rval = 3;
00018       break;
00019     case 23:
00020     case 22:
00021       rval = 2;
00022       break;
00023     case 21:
00024     case 20:
00025     case 19:
00026     case 18:
00027     case 17:
00028     case 16:
00029       rval = 1;
00030       break;
00031     case 15:
00032     case 14:
00033     case 13:
00034     case 12:
00035     case 11:
00036     case 10:
00037     case 9:
00038     case 8:
00039     case 7:
00040     case 6:
00041     case 5:
00042     case 4:
00043     case 3:
00044     case 2:
00045     case 1:
00046     case 0:
00047       rval = 0;
00048       break;
00049     default:
00050       error("max_arq","Invalid rtype input");
00051   }
00052 }
00053 
00054 
00055 int inc_xmit_count(buf_rcpc *br)
00056 /*
00057  * increments xmit count of br and returns it modulo 2
00058  */
00059 {
00060   br->xmit_count = (br->xmit_count + 1);
00061     return (br->xmit_count) % 2;
00062 }
00063 
00064 buf_rcpc *find_rcpc(Buffer *rcpcs, int seq_num)
00065 /* 
00066  * Finds an rcpc buffer entry with a given timestamp
00067  * Returns a pointer to it if it exists, or NULL if it doesn't
00068  */
00069 {
00070   int i;
00071   buf_rcpc *rcpc;
00072  
00073   for (i=blevel(rcpcs)-1; i >= 0; i--) {
00074     rcpc = bpeek(rcpcs, i);
00075     if (seq_num == rcpc->sequence_number)
00076       return (rcpc);
00077   }
00078   return(NULL);
00079 }
00080  
00081 buf_rcpc *place_rcpc(Buffer *rcpcs, int seq_num, byte *data, int num_bytes,
00082                      int arq_type, int last_written, int *last_recd, 
00083                      int old_threshold)
00084 /*
00085  * Places a block of bytes in the correct rcpc structure, and returns
00086  * a pointer to it.
00087  * seq_num is the sequence number
00088  * data is the byte array of data to put in the block
00089  * num_bytes is the length of the byte array
00090  * arq_type is the arq_type (1,2 or 3)
00091  * last_written is the seq_num of last packet written out from buffer
00092  * last_recd is seq_num of last new packet received
00093  */
00094 {
00095   buf_rcpc *rcpc,new_rcpc;
00096   int i,old;
00097 
00098 
00099   old = minus(last_written,old_threshold);
00100   if (between(seq_num,old,last_written) || (seq_num == last_written)) {
00101     printf("place_rcpc() - Disregarding packet %d.  It's too old\n",seq_num);
00102     return(NULL);
00103   } 
00104   else {
00105     if ((between(seq_num,*last_recd,old)) || (seq_num == old)) { 
00106       /* it's a new entry */
00107       *last_recd = seq_num;
00108       /* cheat to find out if we're going to overwrite the last buffer entry
00109        * and if we are, be sure to free any allocated blocks
00110        */
00111       if (rcpcs->full) {
00112         rcpc=bpeek(rcpcs, 0);
00113         if (rcpc->block[0]!=NULL) free(rcpc->block[0]);
00114         if (rcpc->block[1]!=NULL) free(rcpc->block[1]);
00115         if (rcpc->block[2]!=NULL) free(rcpc->block[2]);
00116         if (rcpc->block[3]!=NULL) free(rcpc->block[3]);
00117       }
00118       new_rcpc.sequence_number=seq_num;
00119       new_rcpc.count=1;
00120       new_rcpc.xmit_count=1;
00121       new_rcpc.length=num_bytes;
00122       new_rcpc.block[0]=malloc(num_bytes);
00123       new_rcpc.block[1]=NULL;
00124       new_rcpc.block[2]=NULL;
00125       new_rcpc.block[3]=NULL;
00126       if (new_rcpc.block[0]==NULL) {
00127         fprintf(stderr, "place_rcpc: malloc() failure\n");
00128         return NULL;
00129       }
00130       for (i=0; i<num_bytes; i++) {
00131         new_rcpc.block[0][i]=data[i];
00132       }
00133       bwrite(rcpcs, &new_rcpc);
00134       return(find_rcpc(rcpcs,seq_num));
00135     }
00136     else { /* it's a retransmission */
00137       /* 
00138        */
00139         printf("Got retransmission of sequence number %d\n",seq_num);
00140         rcpc = find_rcpc(rcpcs,seq_num);
00141         if (rcpc->count == 4) {
00142           printf("place_rcpc()-ERROR tried to place block into full rcpc struct\n");
00143           return(NULL);
00144         }
00145         if (arq_type == 1) {
00146           for (i=0; i<num_bytes; i++) {
00147             rcpc->block[0][i]=data[i];
00148           }
00149         } else {
00150           rcpc->block[rcpc->count]=malloc(num_bytes);
00151           if (rcpc->block[rcpc->count]==NULL) {
00152             fprintf(stderr, "place_rcpc: malloc() failure\n");
00153             return(rcpc);
00154           }
00155           for (i=0; i<num_bytes; i++) {
00156             rcpc->block[rcpc->count][i]=data[i];
00157           }
00158           rcpc->count++;
00159         }
00160         /* rcpc->xmit_count++; */
00161         return(rcpc);
00162       }
00163     }
00164 /*  } */
00165 }
00166  

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1