-
Notifications
You must be signed in to change notification settings - Fork 121
/
fec_test.cpp
133 lines (114 loc) · 3.48 KB
/
fec_test.cpp
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
//
// Created by 理 傅 on 2017/1/4.
//
#include <iostream>
#include <sys/time.h>
#include "fec.h"
int main() {
struct timeval time;
gettimeofday(&time, NULL);
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
int datashard = 5;
int parityshard = 3;
int totalshard = datashard + parityshard;
FEC fec = FEC::New(20, datashard, parityshard);
byte arr[] = {0, 0, 0};
std::vector<row_type> shards(totalshard);
for (int i = 0; i < datashard; i++) {
for (int j = 0; j < 3; j++) {
arr[j] = byte(rand() % 255);
}
shards[i] = std::make_shared<std::vector<byte>>(arr, &arr[3]);
}
for (int i = datashard; i < totalshard; i++) {
shards[i] = std::make_shared<std::vector<byte>>(3);
}
std::cout << "shards to encode:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
for (auto b : *shards[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
fec.Encode(shards);
std::cout << "encoded:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
for (auto b : *shards[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
std::cout << "remove 2 datashards & 1 parityshards:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
if (i < 2 || i == 5) {
continue;
}
fecPacket pkt;
pkt.data = shards[i];
pkt.seqid = i;
if (i < 5) {
pkt.flag = typeData;
} else {
pkt.flag = typeFEC;
}
auto recovered = fec.Input(pkt);
if (recovered.size() > 0) {
std::cout << "recovered:" << std::endl;
for (int i = 0; i < recovered.size(); i++) {
for (auto b : *recovered[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
}
}
std::cout << "encoding 2nd Round:" << std::endl;
for (int i = 0; i < datashard; i++) {
for (int j = 0; j < 3; j++) {
arr[j] = byte(rand() % 255);
}
shards[i] = std::make_shared<std::vector<byte>>(arr, &arr[3]);
}
for (int i = datashard; i < totalshard; i++) {
shards[i] = std::make_shared<std::vector<byte>>(3);
}
std::cout << "shards to encode:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
for (auto b : *shards[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
fec.Encode(shards);
std::cout << "encoded:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
for (auto b : *shards[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
std::cout << "remove 2 datashards & 1 parityshards:" << std::endl;
for (int i = 0; i < shards.size(); i++) {
if (i < 2 || i == 5) {
continue;
}
fecPacket pkt;
pkt.data = shards[i];
pkt.seqid = i;
if (i < 5) {
pkt.flag = typeData;
} else {
pkt.flag = typeFEC;
}
auto recovered = fec.Input(pkt);
if (recovered.size() > 0) {
std::cout << "recovered:" << std::endl;
for (int i = 0; i < recovered.size(); i++) {
for (auto b : *recovered[i]) {
std::cout << int(b) << " ";
}
std::cout << std::endl;
}
}
}
}