-
Notifications
You must be signed in to change notification settings - Fork 2
/
clientresources.c
151 lines (132 loc) · 3.37 KB
/
clientresources.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
/*************************************************************************
> File Name: clientresources.c
> Author: Xubin Zhuge
> Mail: [email protected]
> Created Time: Sat 21 Nov 2015 04:11:41 PM EST
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "tfrc_client.h"
#include <math.h>
#include <sys/time.h>
#include "clientresources.h"
/* Returns time in seconds with precision down to micresecond *
double get_time() {
struct timeval curTime;
(void) gettimeofday(&curTime, (struct timezone *) NULL);
return (((((double)curTime.tv_sec)*1000000.0) + (double) curTime.tv_usec)/1000000.0);
}*/
uint64_t get_time() {
struct timeval curTime;
(void) gettimeofday(&curTime, (struct timezone *) NULL);
return (((curTime.tv_sec)*1000000.0) + curTime.tv_usec);
}
void initializeparams() {
tfrc_client.cntrlTimeout = 10; // cntrl pkts timeout = 10 sec
tfrc_client.cntrlTimeoutCounter = 0;
tfrc_client.lossEventCounter = 0;
tfrc_client.X_trans = tfrc_client.msgSize; //
tfrc_client.tld = 0;
tfrc_client.t_RTO = 2; // 2 secs
tfrc_client.b = 1; // one ACK per packet
tfrc_client.t_now = get_time();
tfrc_client.R = DATAMAX; //Initial RTT: MSS
}
void newsendingrate()
{
double fq;
if(tfrc_client.p>0) // congestion avoidance phase
{
fq = (tfrc_client.R/MEG)*sqrt(2*tfrc_client.b*tfrc_client.p/3) +
(tfrc_client.t_RTO*(3*sqrt(3*tfrc_client.b*tfrc_client.p/8)*tfrc_client.p*(1+32*tfrc_client.p*tfrc_client.p)));
tfrc_client.X_calc = tfrc_client.msgSize *8.0 * MEG / fq; // X_calc should be in [ bps]
tfrc_client.X_trans = fmax(fmin(fmin(tfrc_client.X_calc,2*tfrc_client.X_recv),tfrc_client.maxAllowedThroughput),tfrc_client.msgSize*8.0/t_mbi);
}
else
if(tfrc_client.t_now-tfrc_client.tld >= tfrc_client.R) // initial slow start
{
tfrc_client.X_trans = fmax(fmin(fmin(2*tfrc_client.X_trans,2*tfrc_client.X_recv),tfrc_client.maxAllowedThroughput),tfrc_client.msgSize*8.0/tfrc_client.R/MEG);
tfrc_client.tld = tfrc_client.t_now;
}
}
//Converts 8 bytes from host byte order to network byte order
double htond(double host)
{
static int is_big = -1; //1 if Big Endian 0 if Little Endian
double network; //Holds network byte order representation
double host1 = host;
char *n = (char *)&network + sizeof(double)-1;
char *h = (char *)&host1;
unsigned int x = 1;
char *y = (char *)&x;
char z = 1;
int i;
if(is_big < 0)
{
if(*y == z)
{
is_big = 0;
}
else
{
is_big = 1;
}
}
if(is_big)
{
return host;
}
for(i=0;i<sizeof(double);i++)
{
*n = *h;
n--;
h++;
}
return network;
}
//Converts 8 bytes from network byte order to host byte order
double ntohd(double network)
{
static int is_big = -1; //1 if Big Endian 0 if Little Endian
double host; //Holds host byte order representation
char *n = (char *)&network + sizeof(double) -1;
char *h = (char *)&host;
unsigned int x = 1;
char *y = (char *)&x;
char z = 1;
int i;
if(is_big < 0)
{
if(*y == z)
{
is_big = 0;
}
else
{
is_big = 1;
}
}
if(is_big)
{
return network;
}
for(i=0;i<sizeof(double);i++)
{
*h = *n;
n--;
h++;
}
return host;
}
/*Shifts loss interval values*/
void shift_s_values(int s[])
{
int i;
for(i = N_MAX - 1; i>0; i--)
{
s[i] = s[i-1];
}
s[0] = 0;
return;
}