Give access to the list of known peers in the client

Expose the set of peers known by a Client instance through a getPeers()
method. Also changed the name of the SharingPeer.isBound() method to
isConnected() which is easier to understand.

Thanks to Jimmy <jra@alcor.se> for the suggestion.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix parsing of the numwant announce request URL parameter (fixes #19)

The numwant parameter was not correctly considered as one of the numeric
announce request URL parameters and thus not converted to a numeric
value in the map of message parameters in TrackerService, which lead to
a downstream parsing error in HTTPAnnounceRequestMessage that resulted
in HTTP 400 Bad Request errors with clients that passed a numwant
parameter.

Thanks to Carlos Prados and Anatoli Vladev for tracking this bug down.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix: bad announce response crafting (fixes #21)

Signed-off-by: Maxime Petazzoni <maxime.petazzoni@bulix.org>
Fix another large file support issue on writes (closes #22)

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Actually randomize peers returned by tracker (closes #23)

Thanks to Le Wang <larry.wang@gmail.com> for finding this.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix possible out of memory error while hashing large files

When hashing a file, the hashes were accumulated into a list of Future
results and only turned into their string concatenation at the end of
the hashing. When hashing large files with a large number of pieces,
this would quickly consume a lot of memory. This change introduces a
frequent accumulation of the result hashes from the hashing threads into
their string representation as the hashing goes.

Also added progress log messages during hashing.

Inspired by a fix by Pavel Sher (http://github.com/pavelsher).

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix bitfield message length calculation

Inspired by a fix by Pavel Sher.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
ttorrent point-release 1.1.3

Mark a new minor release with the recent fixes and announce
improvements. Next major release is expected to contain the UDP tracker
implementation.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix another potential out of memory issue when hashing large files

The previous fix dealt with the creation of .torrent files. This one
actually fixes hashing done by a client when analyzing existing data.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fix logging message formatting

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Change logging level for peer connection issue

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Fixed RateComparator to never return 0

The rate comparator could return 0, but since Rate doesn't implement
equals() it would lead to strange behavior when building ordered sets
using a RateComparator. This could lead to the number of bound peers
available for unchoked to be incorrect which could prevent the start of
the download when the swarm gets large.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
ttorrent point-release 1.1.4

Fixed memory issues when hashing large files, tweaked some logging
messages and logging levels and fixed RateComparator.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Switch to NIO non-blocking sockets

Using blocking sockets meant that to stop a PeerExchange one had to
forcefully interrupt the incoming and outgoing communication threads.
Unfortunately if that interruption came while the incoming thread was
reading or writing on the underlying FileStorage's FileChannel, it would
get immediately closed, forcing the client to stop while only one peer
had disconnected.

This rather large changeset introduces the switch to NIO, non-blocking
sockets for the peer exchanges so that the two threads of each peer
exchange don't need to be forcefully interrupted. The socket channel is
simply closed and their reads fail gracefully at a controlled time.

It also allows to do pure zero-copy interaction with these sockets
through the use of direct byte buffers.

This change also contains a fix for the allocated size of BitTorrent
keep-alive messages, prevents seeders for initiating outbound
connections (it's not their role) and fixes a potential dead-lock on the
peer exchange lock in some forceful peer exchange unbinding situations.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Attempt to read the error message returned by the tracker (fixes #26)

When the tracker returns an error code, we were not trying to read the
tracker error message (B-encoded) from the error stream, if it was
available.

This change makes sure we get the correct error message from the tracker
in error scenarios, and makes sure all streams are correctly closed when
we do so to avoid resource leaks.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
Merge pull request #1 from turn/master

Sync with turn/torrent