fast hack for Maciek
[swift-upb.git] / exec / leecher.cpp
1 /*
2  *  leecher.cpp
3  *  swift
4  *
5  *  Created by Victor Grishchenko on 11/3/09.
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9 #include "swift.h"
10 #include <time.h>
11
12
13 using namespace swift;
14
15
16 /** swift downloader. Params: root hash, filename, tracker ip/port, own ip/port */
17 int main (int argn, char** args) {
18
19     srand(time(NULL));
20
21     if (argn<4) {
22         fprintf(stderr,"parameters: root_hash filename tracker_ip:port [own_ip:port]\n");
23         return -1;
24     }
25     Sha1Hash root_hash(true,args[1]);
26     if (root_hash==Sha1Hash::ZERO) {
27         fprintf(stderr,"Sha1 hash format: hex, 40 symbols\n");
28         return -2;
29     }
30
31     swift::LibraryInit();
32
33     char* filename = args[2];
34
35     Address tracker(args[3]), bindaddr;
36
37     if (tracker==Address()) {
38         fprintf(stderr,"Tracker address format: [1.2.3.4:]12345, not %s\n",args[3]);
39         return -2;
40     }
41     if (argn>=5)
42         bindaddr = Address(args[4]);
43     else
44         bindaddr = Address((uint32_t)INADDR_ANY,rand()%10000+7000);
45
46     assert(0<swift::Listen(bindaddr));
47
48         swift::SetTracker(tracker);
49
50         int file = swift::Open(filename,root_hash);
51     printf("Downloading %s\n",root_hash.hex().c_str());
52
53     tint start = NOW;
54
55     while (true){ //!swift::IsComplete(file)){// && NOW-start<TINT_SEC*60) {
56             swift::Loop(TINT_SEC);
57         eprintf("%s %lli of %lli (seq %lli) %lli dgram %lli bytes up, %lli dgram %lli bytes down\n",
58                swift::IsComplete(file) ? "DONE" : "done",
59                swift::Complete(file), swift::Size(file), swift::SeqComplete(file),
60                Datagram::dgrams_up, Datagram::bytes_up,
61                Datagram::dgrams_down, Datagram::bytes_down );
62     }
63
64     bool complete = swift::IsComplete(file);
65
66         swift::Close(file);
67
68         swift::Shutdown();
69
70     return !complete;
71 }