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;
42 while ( -1 != (c = getopt_long (argc, argv, ":h:f:dl:t:Dpw::", long_options, 0)) ) {
46 if (strlen(optarg)!=40)
47 quit("SHA1 hash must be 40 hex symbols\n");
48 root_hash = Sha1Hash(optarg);
49 if (root_hash==Sha1Hash::ZERO)
50 quit("SHA1 hash must be 40 hex symbols\n");
53 filename = strdup(optarg);
59 bindaddr = Address(optarg);
60 if (bindaddr==Address())
61 quit("address must be hostname:port, ip:port or just port\n");
64 tracker = Address(optarg);
65 if (tracker==Address())
66 quit("address must be hostname:port, ip:port or just port\n");
69 Channel::debug_file = optarg ? fopen(optarg,"a") : stdout;
72 report_progress = true;
75 wait_time = TINT_NEVER;
78 if (sscanf(optarg,"%lli%c",&wait_time,&unit)!=2)
79 quit("time format: 1234[umsMHD], e.g. 1M = one minute\n");
81 case 'D': wait_time *= 24;
82 case 'H': wait_time *= 60;
83 case 'M': wait_time *= 60;
84 case 's': wait_time *= 1000;
85 case 'm': wait_time *= 1000;
87 default: quit("time format: 1234[umsMHD], e.g. 1D = one day\n");
97 int file = Open(filename,root_hash);
99 printf("Root hash: %s\n", RootMerkleHash(file).hex().c_str());
101 if (root_hash==Sha1Hash() && bindaddr==Address())
104 if (bindaddr!=Address()) { // seeding
105 if (Listen(bindaddr)<=0)
106 quit("cant listen to %s\n",bindaddr.str())
108 wait_time=TINT_NEVER;
110 int base = rand()%10000, i;
111 for (i=0; i<100 && Listen(Address(INADDR_ANY,i*7+base))<=0; i++);
113 quit("cant listen to a port\n");
117 if (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(std::min(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);