File: wb_seg.c

package info (click to toggle)
tgif 1%3A4.2.5-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 19,648 kB
  • sloc: ansic: 194,560; sh: 10,884; perl: 2,956; awk: 487; makefile: 138
file content (216 lines) | stat: -rw-r--r-- 6,411 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Author:     Denise Jorge de Oliveira <dezinha@land.ufrj.br> in Dec, 1999
 *
 * @(#)$Header: /mm2/home/cvs/bc-src/tgif/wb_seg.c,v 1.2 2005/07/26 18:30:27 william Exp $
 */
#ifndef  _SEG_C
#define  _SEG_C

#define _INCLUDE_FROM_WB_SEG_C_

#include "tgifdefs.h"

#include "wb_seg.e"

struct SegmentationList *first=NULL;
struct SegmentationList *last=NULL;

/************************************************************************/
static
struct SegmentationList *NewNodeList ( struct SegmentationPack Pack )
{
   struct SegmentationList *indice;
   
   indice = (struct SegmentationList *)malloc( sizeof(struct SegmentationList));
   
   memcpy(indice->id, Pack.id, SEG_ID);
   indice->LogicalClock = Pack.LogicalClock;
   indice->NumPackets   = Pack.NumPackets;
   indice->ArrivedPcts  = 0;
   indice->Frag = (struct Fragment *)malloc(Pack.NumPackets*(sizeof(struct Fragment)));
   indice->prev = last;
   indice->next = NULL;
   memset( (char *)indice->Frag, 0, Pack.NumPackets*(sizeof(struct Fragment)) );
   
   if( last != NULL )
   {
       last->next = indice;
       last       = indice;
   }
   else
   {
       first = last = indice;
   }
   
   return( last );
}
/************************************************************************/
static
void DeleteNode( struct SegmentationList *indice )
{
    struct SegmentationList *prev, *next;
    
    prev = indice->prev;
    if( prev == NULL )
        first = indice->next;
    else
        prev->next = indice->next;
        
    next = indice->next;
    if( next == NULL )
        last = indice->prev;
    else
        next->prev = indice->prev;
        
    free( indice->Frag );
    free( indice );
}
/************************************************************************/
static
void NewPacket ( struct SegmentationPack *pack, char *id, char *data, int datasize, int  LogicalClock, int  NumPackets, int  NumSeq)
{
   /* fprintf(stderr, "\n****Antes de Converter %d\n", NumSeq ); */
   memcpy(pack->id, id, SEG_ID);
   memcpy(pack->data, data, SEG_DATA_SIZE);
   pack->DataSize     = htonl(datasize);
   pack->LogicalClock = htonl(LogicalClock);
   pack->NumPackets   = htonl(NumPackets);
   pack->NumSeq       = htonl(NumSeq);
   /* fprintf(stderr, "\n&&&&DEPOS de Converter %d\n", pack->NumSeq ); */
}
/************************************************************************/
void InitializeSegmentation ()
{
   first = last = NULL; 
}
/************************************************************************/
ptrSegPack Segment (char * Buffer, int BufLen, char *id, int LogicalClock,
                    int * NPackets)
{
   int   divisor;
   int   offset;
   int   i;
   int   qtd;
   char  aux[SEG_DATA_SIZE];
   ptrSegPack pack;

   divisor = (int) (BufLen/SEG_DATA_SIZE);
    
   if( BufLen % SEG_DATA_SIZE != 0 )
     *NPackets = divisor + 1;
   else *NPackets = divisor;

   pack = (ptrSegPack) malloc ((*NPackets)*(sizeof(SegPack)));

   offset = 0;
   for (i = 0; i < *NPackets; i++, offset += SEG_DATA_SIZE) {
      if( BufLen - offset > SEG_DATA_SIZE )
          qtd = SEG_DATA_SIZE;
      else
          qtd = BufLen - offset;
      memcpy (aux, &Buffer[offset], qtd );
      NewPacket (&pack[i], id, aux, qtd, LogicalClock, *NPackets, i);
   }
   
   return( pack );
}
/******************************************************************************
    Verifica se ja existe uma entrada para a fonte do pacote Pack. Se sim,
retorna TRUE e a posicao dessa entrada em indice.
*******************************************************************************/
static
struct SegmentationList *Compare( struct SegmentationPack Pack )
{
   int status;
   struct SegmentationList *indice;
   
   status = FALSE;
   indice = first;
   
   while( indice != NULL )
   {
       if( Pack.LogicalClock == indice->LogicalClock )
       {
           if( strcmp( Pack.id, indice->id ) == 0 )
           {
               status = TRUE;
               break;
           }
       }
       
       indice = indice->next;
   }

   if( !status )
       indice = NULL;

   return( indice );
}
/**************************************************************************************
    Copia o novo fragmento para a sua posicao na lista. Retorna TRUE se todos os
fragmentos de um pacote chegaram.
**************************************************************************************/
static
int FillFragment(struct SegmentationPack Pack, struct SegmentationList *index)
{
    int status;
    
    status = FALSE;
    if( !index->Frag[ Pack.NumSeq ].DataSize )
    {
        memcpy( index->Frag[ Pack.NumSeq ].data, Pack.data, Pack.DataSize );
        index->Frag[ Pack.NumSeq ].DataSize = Pack.DataSize;

        index->ArrivedPcts++;

        if( index->ArrivedPcts == index->NumPackets )
            status = TRUE;
    }
        
    return( status );
}
/************************************************************************/
static
void ConvertNetworkToHost (struct SegmentationPack *pack)
{
   /* fprintf(stderr, "\n****Antes de desconverter %d\n", pack->NumSeq ); */
   pack->DataSize     = ntohl(pack->DataSize);
   pack->LogicalClock = ntohl(pack->LogicalClock);
   pack->NumPackets   = ntohl(pack->NumPackets);
   pack->NumSeq       = ntohl(pack->NumSeq);
}
/************************************************************************/
char *DeSegment (struct SegmentationPack Pack, int *NumBytes )
{
    struct SegmentationList *indice;
    int packetLength;
    char *buffer;
    int i;

    indice = NULL;
    buffer = NULL;
    *NumBytes = packetLength = 0;
    ConvertNetworkToHost(&Pack);
    /* fprintf(stderr, "Chegou pacote %d\n", Pack.NumSeq ); */
    /* Check if this source was already inserted */    
    if( (indice = Compare( Pack )) == NULL )
        indice = NewNodeList( Pack );
    
    /* If this is the last fragment */
    if( FillFragment( Pack, indice ) )
    {
        packetLength = (indice->NumPackets - 1) * SEG_DATA_SIZE +
                        indice->Frag[indice->NumPackets - 1].DataSize;
        buffer = (char *)malloc( packetLength );
        for( i = 0; i < indice->NumPackets; i++ )
            memcpy( &buffer[ i * SEG_DATA_SIZE ], (char *)indice->Frag[i].data, indice->Frag[i].DataSize );

        *NumBytes = packetLength;           
        DeleteNode( indice );
    }
    
    return( buffer );
}
/************************************************************************/

#endif  /* _SEG_C */