📜 ⬆️ ⬇️

QLiveBittorrent - console bittorrent client for viewing files before downloading

Exactly one month ago the article LiveDC - Fast access to p2p files was published. Its meaning is that Erty_Hackward wrote a DC-client with the ability to view files before their final download. With it you can, for example, watch movies a couple of minutes after the start of download, rewind them, watch from any moment. And you can extract the desired file from a large archive without downloading the whole archive.

I really liked the idea of ​​this program. But it is written in C #. And I would like to use it in Linux. Therefore, with the permission of the author, I with great pleasure took up the creation of a similar program for Linux. The result is a console QLiveBittorrent client bittorrent.



')

How does it work

I'll tell you about the work of QLiveBittorrent on the example of downloading a movie. Well, for example, my friend posted a video of the prom that had recently been held at our school online, and I want to download it, and once again enjoy this unforgettable spectacle.

I'm starting to download a movie. I specify the folder where it should be mounted. And then I try to open it. The film opens 1-2 minutes - depending on the speed. I start to look - as a rule, there are no lags if there is enough speed. Next, I rewind the movie to the middle. I watch the brakes ~ 30 seconds, and continue to watch it from the middle.

Something like this happens. Initially, at the time of opening the movie in the player, the program downloads the first few blocks of the movie and the last block to get information about its duration. Then she continues to download the movie from the beginning. But at the time of rewinding the movie to the middle, the program starts to download the block that was requested by the player.

In addition to downloading movies, the program can be used to work with archives, iso-images, to view folders with a huge number of pictures. It is very convenient to start downloading the archive, and from the very beginning, without waiting for it to download, open it, immediately indicate which file you need, and download it.

How did it start

After I realized that it would be easier to write my own, than to try to port LiveDC to Linux, I immediately began to develop.

At first I thought to build my own functionality into my favorite bittorrent client . But after viewing the source, this desire disappeared. Millions of files, everywhere defin-s on different systems, all in the form of a sheet, and nothing is clear. However, under the impression of qbittorrent, I chose the same toolkit.
  1. Qt5
  2. libtorrent-rasterbar
  3. FUSE

The qlive file system driver was written the day before .
Another day - graphics and torrent downloads.
It looked like this:


On this development fell into a stupor. The program worked, pumped the necessary pieces, but regularly received SIGSEGV (error “the program did not address itself”). At the same time, I was sadly looking at the qbittorrent and utorrent functional , and it slowly came to me that I wouldn't have enough enthusiasm for months of development ...

Second wind

The second breath opened when I decided to score on the GUI. Who needs it? There are too many nerves to support him! This is how the console bittorrent client appeared. And then there was an interesting idea to separate the seated torrents from the swinging ones.

As a result, the program turned out to be divided into a swinging part, which can mount under-downloaded files, limit download speed and be an ordinary torrent client and a seed manager (seed-manager), which can distribute what has been downloaded, as well as limit the upload speed.


Interesting moments of development

  1. Sometimes, testing the download of torrents, I was surprised to find that the download speed exceeds the maximum speed of my router - 802.11G ~ 2MB / s. It reached 40-50 MB / s, which was basically impossible! I tried to blame everything on cash. However, it turned out that I had 2 clients running on my computer, which shared information with each other at the speed of reading the hard disk. One reads, the other writes. Subsequently, I used this for testing.

  2. Due to the fact that the client is a console, I had to deal with the completion signals. I added SIGTERM signal processing (signal for requesting completion of the process). During the debug, when I sent SIGTERM with my hands, it was successfully intercepted and processed. However, in practice, when I pressed ctrl-c in the terminal, the signal was not processed. It turned out that the terminal does not send SIGTERM, but SIGINT (a signal to stop the process by the user from the terminal). It struck me. It turns out that when you press ctrl-c, the signal is not “stop the process”, but “the user wants the process to stop”. Probably, one can come up with a deep sense of the difference between these processes, but it was rather unexpected and strange.

  3. Despite the fact that the client is a console, I would like some kind of GUI to remain. But after all, if the application is a console, then you want it to run in a bare console (TTY), but this is not possible with a GUI ... Or is it possible? To do this, you need to understand how graphics work in Linux.

    And it works like this: there is an X-server, there are clients. At some point, the client connects. The question is when does this happen? Experimentally (by gazing), I found out that the connection occurs at the time of creating QApplication . Accordingly, if graphics are needed, then I create QApplication , if not - QCoreApplication . Thus, the program can be launched both in the bare console and with a graphical mini-interface.

  4. Mixing Qt and libtorrent is pure hell. In the end, my nerves broke down, and I started trying to translate everything into Qt. Unfortunately, libtorrent uses std, so all my code is hung with QString :: fromStdString (string) and QString.toStdString (), as well as vector char conversions, QVector char, QByteArray, etc.

  5. During development, I encountered an unpleasant problem. At one point, slow peers are connected to the program and slowly begin to give the right pieces. At the same time, the average download speed is good, but the delays are unacceptable because of them. I tried to fight it. To some extent, the problem was solved due to the fact that the program searches for fast peers for the first 2 minutes and then limits their maximum number to five feasts. This allows most slow peers to be removed.

  6. So that after midnight there are no problems with time subtraction, the program implements the midnight () function, which checks every second whether it is not midnight now? Within 5 seconds after midnight, the program sausages - when updating the information on the screen, it clears all temporary counters, ignoring user requests. Such is a rare crutch. I wrote it 3 minutes before midnight.

Feature list

  1. If the torrent has just been published, then the ratio of the number of siders to leechers is too small. This entails almost complete inability to download such a torrent in a reasonable time. Therefore, the ability to "transform" into a regular torrent client has been added to QLiveBittorrent. To do this, press the 'a' key (English).

  2. When reading the mounted files, the programs fall into a stupor until the necessary pieces are downloaded. But they always come out of it when it happens. This is a feature of the program.

  3. Authoritative sources claim that on a 600KB / s channel a movie weighing 1.45G with an external audio track and subtitles is played without lags.

  4. If you press ctrl-c on the keyboard (or send SIGTERM), the program will start to finish - reset the data to the hard disk and send the information to the trackers. After resetting the data, you can press ctrl-c - then the information will not be sent to the trackers. This is done because I am very frustrated with programs that cannot be completed.

Sources

  1. Erty_Hackward - original idea
  2. ximaera - talked about how to parse parameters from the command line.
  3. article about boost :: program_options
  4. Libtorrent api is a good library, but the emotions from one-page documentation are non-transferable.
  5. github.com/qbittorrent/qBittorrent - read the source, but did not understand.

Download

Sources
ArchLinux (AUR)
linux-x86-64

Work requires libraries boost, Qt, libtorrent-rasterbar .
Settings are stored at ~ / .qlivebittorrent

UPD 00:30
I forgot to describe two more features:
  1. After downloading the torrent, it is automatically added to the list of seeding. If the seed-manager is already running, it will be added to the list within 10 minutes.
  2. If the torrent has been deleted, it will be removed from the list of seeding at the first request.


UPD 18:00
DAP-DarkneSS posted a version for openSUSE. Thank.
OpenSUSE version

UPD 23:15
In the laid out version, the GUI did not work. What is the error I did not understand, but the required crutch was found.
Files are reloaded.

UPD + month
DAP-DarkneSS has posted versions for Debian, Ubuntu and Fedora. Thank.
Debian and Ubuntu
Fedora

Source: https://habr.com/ru/post/185770/


All Articles