2 inherit Service.Service;
10 void send_update(object obj, int id, string func, void|mixed args)
15 args = ({ id }) + args;
17 send_cmd(obj, func, args, 1); // do not wait
20 void download(int id, string meta)
22 Stdio.File f = Stdio.File("torrent/"+id+".torrent", "wct");
25 Protocols.Bittorrent.Torrent t=Protocols.Bittorrent.Torrent();
26 t->downloads_update_status = lambda() {
27 send_update(updater, id, "downloads_update_status");
30 // Callback when pieces status change (when we get new stuff): //
31 t->pieces_update_status = lambda() {
32 send_update(updater, id, "piece_update");
35 // Callback when peer status changes (connect, disconnect, choked...): //
36 t->peer_update_status = lambda() {
37 send_update(updater, id, "peer_update");
40 // Callback when download is completed:
41 t->download_completed_callback= lambda() {
42 send_update(updater, id, "finished");
44 t->warning = lambda() {
45 send_update(updater, id, "warn");
48 // Initiate targets from Torrent,
49 // if target was created, no need to verify:
50 write("Checking existing file:");
51 function progress = lambda() {
52 send_update(updater, id, "progress");
55 if (t->fix_targets(1,0,progress)==1)
56 t->verify_targets(progress);
58 // Open port to listen on,
59 // we want to do this to be able to talk to firewalled peers:
63 // Ok, start calling tracker to get peers,
65 t->start_update_tracker();
71 void notify(mixed args)
75 void call_service(mixed args)
79 download(args[1], args[2]);
91 if ( !Stdio.exist("torrents") )
93 // read all torrents in torrents/
94 foreach(get_dir("torrents"), string fname) {
96 Stdio.File f = Stdio.File("torrents/"+fname, "r");
97 string meta = f->read();
99 if ( sscanf(fname, "%d.torrent", id) )
114 int main(int argc, array argv)
116 init( "torrent", argv );