wake earlier-than-expected bug
[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     if (swift::Listen(bindaddr)<=0) {
47         fprintf(stderr,"Cannot listen on %s\n",bindaddr.str());
48         return -3;
49     }
50
51         swift::SetTracker(tracker);
52
53         int file = swift::Open(filename,root_hash);
54     printf("Downloading %s\n",root_hash.hex().c_str());
55
56     tint start = NOW;
57
58     while (true){ //!swift::IsComplete(file)){// && NOW-start<TINT_SEC*60) {
59             swift::Loop(TINT_SEC);
60         eprintf("%s %lli of %lli (seq %lli) %lli dgram %lli bytes up, %lli dgram %lli bytes down\n",
61                swift::IsComplete(file) ? "DONE" : "done",
62                swift::Complete(file), swift::Size(file), swift::SeqComplete(file),
63                Datagram::dgrams_up, Datagram::bytes_up,
64                Datagram::dgrams_down, Datagram::bytes_down );
65     }
66
67     bool complete = swift::IsComplete(file);
68
69         swift::Close(file);
70
71         swift::Shutdown();
72
73     return !complete;
74 }