-
Notifications
You must be signed in to change notification settings - Fork 2
/
logQueue.c
250 lines (218 loc) · 6.03 KB
/
logQueue.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <inttypes.h> // for print uint64
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h> //for bool
#include "tfrc.h"
#include "tfrc-server.h"
//struct logEntry {
// struct data_t *packet;
// uint64_t timeArrived;
//};
//
//typedef struct Queue
//{
// struct logEntry **qBase;
// int front;
// int rear;
//}QUEUE;
//void initQueue(QUEUE *pq);
//void enQueue(QUEUE *pq , struct logEntry *value);
//bool isemptyQueue(QUEUE *pq);
//bool is_fullQueue(QUEUE *pq);
//void deQueue(QUEUE *pq , struct logEntry *value);
//void traverseQueue( QUEUE *pq);
/************************************
* init a empty queue
* ************************************/
void initQueue(QUEUE *pq)
{
pq->qBase = (struct logEntry **)malloc(sizeof(struct logEntry *)*MAXN);
int i;
for (i=0;i<MAXN;i++)
{
pq->qBase[i] = (struct logEntry *)malloc(sizeof(struct logEntry));
pq->qBase[i]->packet = (struct data_t*)malloc(sizeof(struct data_t));
//pq->qBase[i]->timeArrived = (uint64_t*)malloc(sizeof(uint64_t));
/*
pq->qBase[i]->packet->msgLength = (uint16_t)malloc(sizeof(uint16_t));
pq->qBase[i]->packet->msgType = (uint8_t)malloc(sizeof(uint8_t));
pq->qBase[i]->packet->code = (uint8_t)malloc(sizeof(uint8_t));
pq->qBase[i]->packet->CxID = (uint32_t)malloc(sizeof(uint32_t));
pq->qBase[i]->packet->seqNum = (uint32_t)malloc(sizeof(uint32_t));
pq->qBase[i]->packet->timeStamp = (uint64_t)malloc(sizeof(uint64_t));
pq->qBase[i]->packet->RTT = (uint32_t)malloc(sizeof(uint32_t));
//pq->qBase[i]->packet->X = (uint8_t)malloc(sizeof(uint8_t));*/
}
if(pq->qBase == NULL)
{
printf("can't init!\n");
exit(-1);
}
pq->front = pq->rear = 0;
}
/************************************************************
*
* ???????
*
* ?:???????????????,?????????
*
* ************************************************************/
void enQueue(QUEUE *pq , struct logEntry *value)
{
if(is_fullQueue(pq))
deQueue(pq, value);
pq->qBase[pq->rear]->timeArrived = value->timeArrived;
//printf("%" PRIu64 "\n\n",pq->qBase[pq->rear]->timeArrived);
//printf("%" PRIu64 "\n\n",value->timeArrived);
pq->qBase[pq->rear]->packet->msgLength = value->packet->msgLength;
pq->qBase[pq->rear]->packet->msgType = value->packet->msgType;
pq->qBase[pq->rear]->packet->code = value->packet->code;
pq->qBase[pq->rear]->packet->CxID = value->packet->CxID;
pq->qBase[pq->rear]->packet->seqNum = value->packet->seqNum;
pq->qBase[pq->rear]->packet->timeStamp = value->packet->timeStamp;
pq->qBase[pq->rear]->packet->RTT = value->packet->RTT;
/*
//pq->qBase[i]->packet->X = (uint8_t)malloc(sizeof(uint8_t));*/
pq->rear = (pq->rear + 1)%MAXN ;
//printf("\n %d ?? \n" , value);
}
/*************************************
*
* ??????,?????????
*
* ?:??????????????
*
* *************************************/
void deQueue(QUEUE *pq , struct logEntry *value)
{
if(isemptyQueue(pq))
{
printf("Is empty!");
}else
{
//value = pq->qBase[pq->front];
//printf("\n %d ?? \n",*value);
pq->front = (pq->front + 1)%MAXN ;
//free(value);
}
}
/************************************
*
* ??????????
*
* ************************************/
bool isemptyQueue(QUEUE *pq)
{
if(pq->front == pq->rear)
{
printf("empty");
return true;
}else
return false;
}
/************************************
* ??????????
* ************************************/
bool is_fullQueue(QUEUE *pq)
{
if((pq->rear +1)%MAXN == pq->front)
{
return true;
}else
return false;
}
uint32_t getMaxSeqNum(QUEUE *pq,int index)
{
uint32_t m[4];
int i = 0;
if(isemptyQueue(pq))
return 0;
int tail = pq->front;
for(i=0;i<4;i++)
{
m[i] = pq->qBase[tail]->packet->seqNum;
}
while(tail != pq->rear)
{
if((pq->qBase[tail])->packet->seqNum > m[0]){
m[3] = m[2];
m[2] = m[1];
m[1] = m[0];
m[0] = pq->qBase[tail]->packet->seqNum;
}
else if(pq->qBase[tail]->packet->seqNum > m[1]){
m[3] = m[2];
m[2] = m[1];
m[1] = pq->qBase[tail]->packet->seqNum;
}
else if(pq->qBase[tail]->packet->seqNum > m[2]){
m[2] = m[1];
m[2] = pq->qBase[tail]->packet->seqNum;
}
tail = (tail + 1)%MAXN;
}
if (index == 3)
return m[2];
else if (index==4)
return m[3];
else{
printf("index wrong\n");
exit(1);
}
}
int existSeqNum(QUEUE *pq, uint32_t seqNum)
{
if(isemptyQueue(pq))
return -2;
int tail = pq->front;
while(tail != pq->rear)
{
if(pq->qBase[tail]->packet->seqNum == seqNum)
return tail;
tail = (tail + 1)%MAXN ;
}
return -1;
}
int getIndexBefore(QUEUE *pq, uint32_t S_loss)
{
int tail = pq->front;
int index = tail;
while(tail != pq->rear)
{
if(pq->qBase[tail]->packet->seqNum < S_loss
&& pq->qBase[tail]->packet->seqNum
> pq->qBase[index]->packet->seqNum)
index = tail;
tail = (tail + 1)%MAXN ;
}
return index;
}
int getIndexAfter(QUEUE *pq, uint32_t S_loss)
{
int tail = pq->front;
int index = pq->rear;
while(tail != pq->rear)
{
if(pq->qBase[tail]->packet->seqNum > S_loss
&& pq->qBase[tail]->packet->seqNum
< pq->qBase[index]->packet->seqNum){
index = tail;
}
tail = (tail + 1)%MAXN;
}
return index;
}
int getRecvBits(QUEUE *pq, uint64_t timeLine)
{
int Bytes = 0;
int tail = pq->front;
while(tail != pq->rear)
{
if (pq->qBase[tail]->timeArrived > timeLine)
Bytes += pq->qBase[tail]->packet->msgLength;
tail = (tail + 1)%MAXN;
}
//printf("bytes:%d\n",Bytes);
return Bytes*8;
}