Sunday, August 2, 2015

Now coding a torrent library for dart developers

My English skill is poor. If I make mistakes in my English,please pardon me.

I'm coding a torrent library for  dart developers which name is hetimatorrent.
Bep3 spec function is works for me now.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import 'dart:html';
import 'dart:async';

import 'package:chrome/chrome_app.dart' as chrome;
import 'package:hetimatorrent/hetimatorrent.dart';
import 'package:hetimanet/hetimanet_chrome.dart';
import 'package:hetimacore/hetimacore_cl.dart';
import 'package:hetimacore/hetimacore.dart';

void main() {
  querySelector("#inputFile").onChange.listen(startDownload);
}

Future startDownload(MouseEvent event) {
  TorrentEngine engine;
  List<File> selectedFile = (querySelector("#inputFile") as InputElement).files;

  bool isStop = false;
  return TorrentFile.createFromTorrentFile(new HetimaFileToBuilder(new HetimaDataBlob(selectedFile[0]))).then((TorrentFile torrentFile) {
    return TorrentEngine.createTorrentEngine(new HetiSocketBuilderChrome(), torrentFile, new HetimaDataFS("save.dat")).then((TorrentEngine engine) {
      engine.start(usePortMap: true);
      engine.onProgress.listen((TorrentEngineProgress progress) {
        print("${progress.toString()}");
        if (progress.downloadSize >= progress.fileSize && isStop == false) {
          isStop = true;
          new Future.delayed(new Duration(minutes: 5)).then((_) {
            engine.stop().catchError((e) {});
          });
        }
      });
    });
  });
}

This code could download files from torrent p2p network at 20-30 line code.
check following code.
 - this sample
  https://github.com/kyorohiro/dart_hetimatorrent/tree/master/example/TorrentClient
 - library page
  https://github.com/kyorohiro/dart_hetimatorrent

Sorry, now this api support only on chrome socket api and pre-Alpha version.
Thank you.

-------
Kyorohiro work


No comments:

Post a Comment