bangalore-haskell-user-group / functorrent

A bit torrent client
GNU General Public License v3.0
18 stars 9 forks source link

Cleanup a few bad types #19

Closed jaseemabid closed 9 years ago

jaseemabid commented 9 years ago
  1. Change type InfoDict from Map BVal BVal to Map String BVal

The keys are human readable keys like 'name', 'announce' etc and String is the natural fit. This change also gets rid of a lot of packing unpacking business.

Example:

-infoHash :: Map BVal BVal -> ByteString
-infoHash m = let info = m ! Bstr (pack "info")
-             in (hash . pack . encode) info
+infoHash :: InfoDict -> ByteString
+infoHash m = hash . encode $ (m ! "info")
  1. Change type of encode from BVal -> String to BVal -> ByteString

Encoded values might not be ASCII safe in the first place. Also, it makes sense to be the inverse function of decode. encode . decode pair might be a good candidate for Quickcheck.

Other changes are consequences of the type change. Fixed a bunch of broken doctests in Bencode.

TIL haskell is kickass for refactoring!

jaseemabid commented 9 years ago

@vu3rdd So I'm guessing its ok. Merging in.

vu3rdd commented 9 years ago

@jaseemabid: I get an error when I run it past the torrent files. Parsing input file: data/ubuntu-14.10-desktop-amd64.iso.torrent Input File OK Trying to fetch peers: functorrent: Map.!: given key is not an element in the map functorrent: thread blocked indefinitely in an MVar operation

jaseemabid commented 9 years ago

This must be due to tracker returning an error. I just tried with the debian file and got d14:failure reason17:torrent not founde from tracker. decode parsed it fine to. Bdict (fromList [("failure reason",Bstr "torrent not found")]). getPeerResponse is not a total function and it messed up. I'll fix this in a while.

jaseemabid commented 9 years ago

@vu3rdd Fixed at https://github.com/vu3rdd/functorrent/commit/3d076eb8b041e4e556a12a85ba5cf2d1b82ccee6