3 * swift the multiparty transport protocol
5 * Created by Victor Grishchenko on 2/15/10.
6 * Copyright 2010 Delft University of Technology. All rights reserved.
14 using namespace swift;
16 #define quit(...) {fprintf(stderr,__VA_ARGS__); exit(1); }
19 int main (int argc, char** argv) {
21 static struct option long_options[] =
23 {"hash", required_argument, 0, 'h'},
24 {"file", required_argument, 0, 'f'},
25 {"daemon", no_argument, 0, 'd'},
26 {"listen", required_argument, 0, 'l'},
27 {"tracker", required_argument, 0, 't'},
28 {"debug", no_argument, 0, 'D'},
29 {"progress",no_argument, 0, 'p'},
30 {"wait", optional_argument, 0, 'w'},
36 bool daemonize = false, report_progress = false;
44 while ( -1 != (c = getopt_long (argc, argv, ":h:f:dl:t:Dpw::", long_options, 0)) ) {
48 if (strlen(optarg)!=40)
49 quit("SHA1 hash must be 40 hex symbols\n");
50 root_hash = Sha1Hash(true,optarg); // FIXME ambiguity
51 if (root_hash==Sha1Hash::ZERO)
52 quit("SHA1 hash must be 40 hex symbols\n");
55 filename = strdup(optarg);
61 bindaddr = Address(optarg);
62 if (bindaddr==Address())
63 quit("address must be hostname:port, ip:port or just port\n");
66 tracker = Address(optarg);
67 if (tracker==Address())
68 quit("address must be hostname:port, ip:port or just port\n");
72 Channel::debug_file = optarg ? fopen(optarg,"a") : stdout;
75 report_progress = true;
78 wait_time = TINT_NEVER;
81 if (sscanf(optarg,"%lli%c",&wait_time,&unit)!=2)
82 quit("time format: 1234[umsMHD], e.g. 1M = one minute\n");
84 case 'D': wait_time *= 24;
85 case 'H': wait_time *= 60;
86 case 'M': wait_time *= 60;
87 case 's': wait_time *= 1000;
88 case 'm': wait_time *= 1000;
90 default: quit("time format: 1234[umsMHD], e.g. 1D = one day\n");
98 if (bindaddr!=Address()) { // seeding
99 if (Listen(bindaddr)<=0)
100 quit("cant listen to %s\n",bindaddr.str())
102 wait_time=TINT_NEVER;
103 } else if (tracker!=Address()) { // leeching
104 int base = rand()%10000, i;
105 for (i=0; i<100 && Listen(Address((uint32_t)INADDR_ANY,i*7+base))<=0; i++);
107 quit("cant listen to a port\n");
110 if (tracker!=Address())
113 int file = Open(filename,root_hash);
115 printf("Root hash: %s\n", RootMerkleHash(file).hex().c_str());
117 if (root_hash==Sha1Hash() && bindaddr==Address() && tracker==Address())
120 tint start_time = NOW;
121 tint end_time = TINT_NEVER;
123 while (NOW<end_time+wait_time){
124 if (end_time==TINT_NEVER && IsComplete(file))
126 // and yes, I add up infinities and go away with that
127 tint towait = (end_time+wait_time)-NOW;
128 Loop(TINT_SEC<towait?TINT_SEC:towait);
129 if (report_progress) {
131 "%s %lli of %lli (seq %lli) %lli dgram %lli bytes up, "\
132 "%lli dgram %lli bytes down\n",
133 IsComplete(file) ? "DONE" : "done",
134 Complete(file), Size(file), SeqComplete(file),
135 Datagram::dgrams_up, Datagram::bytes_up,
136 Datagram::dgrams_down, Datagram::bytes_down );
142 if (Channel::debug_file)
143 fclose(Channel::debug_file);