env.SharedLibrary (
target='p2tp',
- source = [ 'bin.cpp','hashtree.cpp','datagram.cpp',
- 'bins.cpp' ],
+ source = [ 'bin64.cpp','hashtree.cpp','datagram.cpp',
+ 'bins.cpp', 'transfer.cpp' ],
LIBS=['stdc++','gtest','glog','crypto'] )
SConscript('tests/SConscript')
int Datagram::Wait (int sockcnt, int* sockets, tint usec) {
LOG(INFO)<<"waiting for "<<sockcnt;
struct timeval timeout;
- timeout.tv_sec = usec/SEC;
- timeout.tv_usec = usec%SEC;
+ timeout.tv_sec = usec/TINT_SEC;
+ timeout.tv_usec = usec%TINT_SEC;
int max_sock_fd = 0;
fd_set bases, err;
FD_ZERO(&bases);
env = Environment(CPPPATH = '.',CXXFLAGS="-g")
-env.Program(
- target='bintest',
- source=['bintest.cpp'],
- CPPPATH=['..'],
- LIBS=['p2tp','stdc++','gtest','glog'],
- LIBPATH='..' )
-
env.Program(
target='binstest2',
source=['binstest2.cpp'],
env.Program(
target='bin64test',
- source=['bin64test.cpp','../bin64.h'],
+ source=['bin64test.cpp'],
+ CPPPATH=['..'],
+ LIBS=['p2tp','gtest'],
+ LIBPATH='..' )
+
+env.Program(
+ target='transfertest',
+ source=['transfertest.cpp'],
CPPPATH=['..'],
- LIBS=['gtest'],
+ LIBS=['p2tp','gtest'],
LIBPATH='..' )
EXPECT_STREQ(hash123,hash.hex().c_str());
}
+/*
TEST(Sha1HashTest,HashTreeTest) {
Sha1Hash roothash123(hash123);
for(bin pos=1; pos<bin::ALL; pos=pos.parent())
EXPECT_TRUE ( bootstree.bits[2]==bbbhash );
EXPECT_FALSE ( bootstree.bits[2]==aaahash );
}
-
+*/
int main (int argc, char** argv) {
- bin::init();
+ //bin::init();
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
TEST(Datagram,LedbatTest) {
int MAX_REORDERING = 3;
- tint TARGET = 25*MSEC;
+ tint TARGET = 25*TINT_MSEC;
float GAIN = 1.0/TARGET;
int seq_off = 0;
float cwnd = 1;
- tint DELAY_BIN = SEC*30;
- tint min_delay = NEVER;
- tint rtt_avg = NEVER>>4, dev_avg = NEVER>>4;
+ tint DELAY_BIN = TINT_SEC*30;
+ tint min_delay = TINT_NEVER;
+ tint rtt_avg = TINT_NEVER>>4, dev_avg = TINT_NEVER>>4;
tint last_bin_time = 0;
tint last_drop_time = 0;
int delay_bin = 0;
deque<tint> history, delay_history;
- tint min_delay_bins[4] = {NEVER,NEVER,
- NEVER,NEVER};
- tint cur_delays[4] = {NEVER,NEVER,
- NEVER,NEVER};
+ tint min_delay_bins[4] = {TINT_NEVER,TINT_NEVER,
+ TINT_NEVER,TINT_NEVER};
+ tint cur_delays[4] = {TINT_NEVER,TINT_NEVER,
+ TINT_NEVER,TINT_NEVER};
tint last_sec = 0;
int sec_ackd = 0;
uint8_t* garbage = (uint8_t*) malloc(1024);
int socks[2] = {send_sock,ack_sock};
int sock2read;
- tint wait_time = 100*MSEC;
+ tint wait_time = 100*TINT_MSEC;
while (sock2read = Datagram::Wait(2,socks,wait_time)) {
tint now = Datagram::Time();
ack.Push64(now);
if (4+8!=ack.Send())
fprintf(stderr,"short write\n");
- fprintf(stderr,"%lli rcvd%i\n",now/SEC,seq);
+ fprintf(stderr,"%lli rcvd%i\n",now/TINT_SEC,seq);
// TODO: peer cwnd !!!
continue;
}
if (send_time/DELAY_BIN != last_bin_time) {
last_bin_time = send_time/DELAY_BIN;
delay_bin = (delay_bin+1) % 4;
- min_delay_bins[delay_bin] = NEVER;
- min_delay = NEVER;
+ min_delay_bins[delay_bin] = TINT_NEVER;
+ min_delay = TINT_NEVER;
for(int i=0;i<4;i++)
if (min_delay_bins[i]<min_delay)
min_delay = min_delay_bins[i];
if (delay < min_delay)
min_delay = delay;
cur_delays[(seq_off+seq)%4] = delay;
- tint current_delay = NEVER;
+ tint current_delay = TINT_NEVER;
for(int i=0; i<4; i++)
if (current_delay > cur_delays[i])
current_delay = cur_delays[i]; // FIXME avg
fprintf(stderr,"ackd cwnd%f cur%lli min%lli seq%i off%i\n",
cwnd,current_delay,min_delay,seq_off+seq,seq);
- if (now/SEC!=last_sec/SEC) {
+ if (now/TINT_SEC!=last_sec/TINT_SEC) {
fprintf(stderr,"%i KB/sec\n",sec_ackd);
sec_ackd = 0;
last_sec = now; // FIXME
if (history.size()<cwnd)
wait_time = rtt_avg/cwnd;
else
- wait_time = 100*MSEC;
+ wait_time = 100*TINT_MSEC;
} // while
}
TEST(Datagram,LedbatTest) {
int MAX_REORDERING = 3;
- tint TARGET = 25*MSEC;
+ tint TARGET = 25*TINT_MSEC;
float GAIN = 1.0/TARGET;
int seq_off = 0;
float cwnd = 1;
- tint DELAY_BIN = SEC*30;
- tint min_delay = NEVER;
- tint rtt_avg = NEVER>>4, dev_avg = NEVER>>4;
+ tint DELAY_BIN = TINT_SEC*30;
+ tint min_delay = TINT_NEVER;
+ tint rtt_avg = TINT_NEVER>>4, dev_avg = TINT_NEVER>>4;
tint last_bin_time = 0;
tint last_drop_time = 0;
int delay_bin = 0;
deque<tint> history, delay_history;
- tint min_delay_bins[4] = {NEVER,NEVER,
- NEVER,NEVER};
- tint cur_delays[4] = {NEVER,NEVER,
- NEVER,NEVER};
+ tint min_delay_bins[4] = {TINT_NEVER,TINT_NEVER,
+ TINT_NEVER,TINT_NEVER};
+ tint cur_delays[4] = {TINT_NEVER,TINT_NEVER,
+ TINT_NEVER,TINT_NEVER};
tint last_sec = 0;
int sec_ackd = 0;
uint8_t* garbage = (uint8_t*) malloc(1024);
int socks[2] = {send_sock,ack_sock};
int sock2read;
- tint wait_time = 100*MSEC;
+ tint wait_time = 100*TINT_MSEC;
while (sock2read = Datagram::Wait(2,socks,wait_time)) {
tint now = Datagram::Time();
ack.Push64(now);
if (4+8!=ack.Send())
fprintf(stderr,"short write\n");
- fprintf(stderr,"%lli rcvd%i\n",now/SEC,seq);
+ fprintf(stderr,"%lli rcvd%i\n",now/TINT_SEC,seq);
// TODO: peer cwnd !!!
continue;
}
if (send_time/DELAY_BIN != last_bin_time) {
last_bin_time = send_time/DELAY_BIN;
delay_bin = (delay_bin+1) % 4;
- min_delay_bins[delay_bin] = NEVER;
- min_delay = NEVER;
+ min_delay_bins[delay_bin] = TINT_NEVER;
+ min_delay = TINT_NEVER;
for(int i=0;i<4;i++)
if (min_delay_bins[i]<min_delay)
min_delay = min_delay_bins[i];
if (delay < min_delay)
min_delay = delay;
cur_delays[(seq_off+seq)%4] = delay;
- tint current_delay = NEVER;
+ tint current_delay = TINT_NEVER;
for(int i=0; i<4; i++)
if (current_delay > cur_delays[i])
current_delay = cur_delays[i]; // FIXME avg
fprintf(stderr,"ackd cwnd%f cur%lli min%lli seq%i off%i\n",
cwnd,current_delay,min_delay,seq_off+seq,seq);
- if (now/SEC!=last_sec/SEC) {
+ if (now/TINT_SEC!=last_sec/TINT_SEC) {
fprintf(stderr,"%i KB/sec\n",sec_ackd);
sec_ackd = 0;
last_sec = now; // FIXME
if (history.size()<cwnd)
wait_time = rtt_avg/cwnd;
else
- wait_time = 100*MSEC;
+ wait_time = 100*TINT_MSEC;
} // while
}
--- /dev/null
+/*
+ * transfertest.cpp
+ * p2tp
+ *
+ * Created by Victor Grishchenko on 10/7/09.
+ * Copyright 2009 Delft Technical University. All rights reserved.
+ *
+ */
+#include <gtest/gtest.h>
+#include "p2tp.h"
+
+using namespace p2tp;
+
+const char* BTF = "big_test_file";
+
+Sha1Hash A,B,C,D,E,AB,CD,ABCD,E0,E000,ABCDE000,ROOT;
+
+
+TEST(TransferTest,TransferFile) {
+
+ AB = Sha1Hash(A,B);
+ CD = Sha1Hash(C,D);
+ ABCD = Sha1Hash(AB,CD);
+ E0 = Sha1Hash(E,Sha1Hash::ZERO);
+ E000 = Sha1Hash(E0,Sha1Hash::ZERO);
+ ABCDE000 = Sha1Hash(ABCD,E000);
+ ROOT = ABCDE000;
+ for (int i=0; i<60; i++)
+ ROOT = Sha1Hash(ROOT,Sha1Hash::ZERO);
+
+ // submit a new file
+ FileTransfer* seed = new FileTransfer(Sha1Hash::ZERO,BTF);
+ EXPECT_TRUE(ROOT==seed->root_hash);
+ EXPECT_EQ(4100,seed->size);
+ EXPECT_EQ(5,seed->sizek);
+ EXPECT_EQ(4100,seed->complete);
+ EXPECT_EQ(4100,seed->seq_complete);
+
+ // retrieve it
+ FileTransfer* leech = new FileTransfer(seed->root_hash,"copy");
+ // transfer peak hashes
+ for(int i=0; i<seed->peak_count; i++)
+ leech->OfferHash(seed->peaks[i],seed->peak_hashes[i]);
+ ASSERT_EQ(1<<12,leech->size);
+ ASSERT_EQ(4,leech->sizek);
+ ASSERT_EQ(0,leech->complete);
+ // transfer data and hashes
+ // ABCDHASH
+ // ABHASH CDHASH
+ // AAAA BBBB CCCC DDDD E
+ leech->OfferHash(bin64_t(1,0), seed->hashes[bin64_t(1,0)]);
+ leech->OfferHash(bin64_t(1,1), seed->hashes[bin64_t(1,1)]);
+ for (int i=0; i<4; i++) {
+ /*if (leech->complete()==trap) {
+ delete leech;
+ FileTransfer* leech = new FileTransfer(seed,"copy");
+ }*/
+ bin64_t next = leech->picker->Pick(seed->ack_out,0);
+ uint8_t buf[1024]; //size_t len = seed->storer->ReadData(next,&buf);
+ size_t len = pread(seed->fd,buf,1024,next.base_offset());
+ bin64_t sibling = next.sibling();
+ leech->OfferHash(sibling, seed->hashes[sibling]);
+ leech->OfferData(next, buf, len);
+ }
+ EXPECT_EQ(4100,leech->size);
+ EXPECT_EQ(5,leech->sizek);
+ EXPECT_EQ(4100,leech->complete);
+ EXPECT_EQ(4100,leech->seq_complete);
+
+ unlink("copy");
+
+}
+
+
+int main (int argc, char** argv) {
+
+ int f = open(BTF,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+ uint8_t buf[1024];
+ memset(buf,'A',1024);
+ A = Sha1Hash(buf,1024);
+ write(f,buf,1024);
+ memset(buf,'B',1024);
+ B = Sha1Hash(buf,1024);
+ write(f,buf,1024);
+ memset(buf,'C',1024);
+ C = Sha1Hash(buf,1024);
+ write(f,buf,1024);
+ memset(buf,'D',1024);
+ D = Sha1Hash(buf,1024);
+ write(f,buf,1024);
+ memset(buf,'E',4);
+ E = Sha1Hash(buf,4);
+ write(f,buf,4);
+ close(f);
+
+ testing::InitGoogleTest(&argc, argv);
+ int ret = RUN_ALL_TESTS();
+
+ unlink(BTF);
+
+ return ret;
+}