Virtual OS/2 International Consumer Education
VOICE Home Page: http://www.os2voice.org
July 2004

Newsletter Index
< Previous Page | Next Page >
Feature Index

editor@os2voice.org


Bittorrent and OS/2

By Alex Taylor © July 2004

Introduction

BitTorrent is rapidly growing in popularity as a peer-to-peer file sharing system (P2P). Like all P2P systems, BitTorrent can be used to download and distribute all kinds of files.

Unlike most P2P systems, BitTorrent does not 'live' entirely within its own application. The BitTorrent client typically installs itself as a helper application in your web browser. You use the browser to locate the file(s) you want, and then initiate the download by clicking on a link.

When you go to download a file using BitTorrent, the first thing that is downloaded is a small file with a name ending in .torrent which contains information (called 'metainfo') about the actual file you wish to download. The .torrent file is passed to the BitTorrent client which parses the metainfo to retrieve the file you want.

Every file downloaded through BitTorrent is divided up into 'pieces' of a relatively small size. BitTorrent works by 'trading' these pieces with other clients (peers) that already have, or are in the process of downloading, the same file. The general idea behind this system is that you cannot download a file without also uploading it thereby ensuring that demand does not outpace contribution.

BitTorrent has many uses both perfectly legitimate and otherwise. For instance, Linux vendor Mandrake has taken to distributing recent versions of its products via BitTorrent.

The obvious advantage of BitTorrent is that its distributed nature allows one to make potentially large files available for download without having to provide large amounts of bandwidth to cope with demand. The bandwidth is instead shared amongst all the downloaders.

The disadvantage is that, as a downloader, you must also devote a certain amount of your own bandwidth to uploading. For modem users this may be inconvenient; high-speed Internet users may not consider this a problem. Most BitTorrent implementations do allow you to throttle the upload speed.

Users whose Internet access is limited to a certain amount of data transferred each month would be well advised to keep a close eye on their upload and download totals when using BitTorrent.

Using BitTorrent on OS/2

There are already quite a number of BitTorrent implementations. Fortunately for us the original reference implementation (along with several of its derivatives) is implemented in Python: an interpreted language which is supported on most platforms, including OS/2.

Requirements

BitTorrent under OS/2 or eComStation needs a few things:

Installation Steps

  1. Unzip the Python files to a location of your choice. This creates a 'python234' (or similar) subdirectory in that location. For instance,

    unzip python-2.3.4-emx-bin-060604.zip -d c:\usr\local
    

    places the Python files in the directory c:\usr\local\python233.

    As far as using BitTorrent goes, unzipping is all you need to do to 'install' Python as long as you use BT_GET.CMD (or an equivalent script). No editing of CONFIG.SYS is necessary.

    If, however, you want to use Python with other applications, you can set up the environment and so on according to the Python README.

    Note that Python requires the EMX runtime so if you don't have that installed on your system, do it now.

  2. Unzip the BitTorrent package into a directory of your choice. This creates a subdirectory containing the BitTorrent files; for instance,

    unzip BitTorrent-3.4.2.zip -d e:\programs
    

    places the BitTorrent files in the directory E:\programs\BitTorrent-3.4.2. (For simplicity, I suggest renaming the directory to just BitTorrent or similar.)

  3. Place the launch script BT_GET.CMD somewhere on your system. It can go onto your PATH (recommended), in your BitTorrent directory, or anywhere, really.

    You must edit this file and modify the four constants at the top as appropriate for your system.
    The four constants are:

    pythdir
    The fully-qualified name of the directory containing the Python files.
    btdir
    The fully-qualified name of the directory containing the BitTorrent files.
    savedir
    The fully-qualified name of the directory where you want downloaded files to be saved.
    uploadspeed
    The maximum upload speed (in KB/s) to allow when sending to other computers. Users on dial-up connections probably want to set this to a low value.
  4. Go into your browser's Preferences (I assume you are using Mozilla; if not, adapt these directions for your own browser).

    Go to the Helper Applications page under the Navigator section. Select the New type button, and enter the following:

    MIME Type: application/x-bittorrent
    Description: BitTorrent
    Extension: torrent
    When a file of this type is encountered Select Open it with, and enter or select the fully-qualified name of the BT_GET.CMD file.

  5. If you have a firewall (or a broadband router that performs Network Address Translation), make sure it allows or forwards incoming TCP connections on ports 6881 to 6889 to your system.

    If these ports are blocked, BitTorrent downloads but not uploads. Due to the 'tit-for-tat' sharing system that BitTorrent uses, having the upload ports blocked causes your download speeds to be extremely slow.

Using BitTorrent

Once BitTorrent is set up, using it is extremely simple.

First, you need to have some file that you want to download via BitTorrent. (Locating such files is beyond the scope of this article; presumably, you're setting BitTorrent up because there's some file or other that you want to download.)

When you want to download a file from some website, click on the link to download the .torrent file. If everything was configured correctly (in the steps above), a command window opens and runs BT_GET.CMD with the .torrent file. This initiates the BitTorrent download.


Click here for a larger image.

You can interrupt the download and close the client at any time by pressing [Q].

Note that BitTorrent is capable of resuming incomplete downloads. If you start to download a file which you already have part of, the BitTorrent client automatically picks up where it left off (as long as the file is still in the same directory, and hasn't been renamed).

If you have downloaded a .torrent file to your hard drive (instead of using your browser as above), you can initiate the download as well by running

bt_get <.torrent file>

assuming BT_GET.CMD is in the PATH or current directory.

Once the download has finished, the BitTorrent client window remains open until you press [Q] to quit. Leave the client running for a while even after the download is complete. It's considered polite since it allows others to continue downloading the file from you (which is only fair since you've just been downloading it from others). Apparently some sites have specific requirements to this effect; but even so, you should leave the client running for as long as you conveniently can.

Pitfalls

In general, this seems to work extremely well. I have observed occasional SYS3175 crashes in the BitTorrent client (or, more accurately, in Python/2), but these seem to be rare. In any case, if BitTorrent does crash while downloading, you simply reinitiate the download, and it resumes where it left off.

(There are some experimental updates on the Python/2 web site which may or may not address this problem. I have not tried them. See the note near the bottom of this page for more information.)


Appendix A: BT_GET.CMD

/*****************************************************************************
 * BT_GET.CMD  -  OS/2 BitTorrent Commandline Launcher                       *
 *****************************************************************************/

/* ------------------------------------------------------------------------- *
 * DIRECTORY CONSTANTS - EDIT AS APPROPRIATE FOR YOUR SYSTEM                 *
 * ------------------------------------------------------------------------- */

/* Directory where Python/2 files are installed (use forward slashes) */
pythdir = 'c:\usr\local\python233'

/* Directory where BitTorrent files are installed */
btdir   = 'e:\network\bittorrent'

/* Directory where downloaded files will be placed */
savedir = 'f:\tmp'


/* ------------------------------------------------------------------------- *
 * BITTORRENT CONSTANTS - EDIT AS APPROPRIATE FOR YOUR SYSTEM                *
 * ------------------------------------------------------------------------- */

/* Maximum upload speed when sending to peers */
uploadspeed = 25


/* ------------------------------------------------------------------------- *
 * CHANGES BELOW THIS POINT ARE NOT GENERALLY NECESSARY                      *
 * ------------------------------------------------------------------------- */

IF RxFuncQuery('SysSetExtLIBPATH') = 1 THEN
    CALL RxFuncAdd 'SysSetExtLIBPATH', 'REXXUTIL', 'SysSetExtLIBPATH'


/* Check the torrent filename */
PARSE ARG torrent
torrent = STRIP( torrent )
torrent = STRIP( torrent, 'B', '"')
IF torrent = '' THEN DO
    SAY 'Torrent file not specified.'
    RETURN
END

/* Determine the name of the actual file to download (for saving to) */
savename = TargetFileName( torrent )
IF savename \= '' THEN
    saveas = '--saveas' savedir'\'savename
ELSE
    saveas = ''

/* Print some notices */
SAY 'Python files:    ' pythdir
SAY 'BitTorrent files:' btdir
SAY 'Save directory:  ' savedir

/* Now set up the environment */
CALL SETLOCAL
CALL DIRECTORY btdir

/* Add Python to the system PATH and LIBPATH */
CALL SysSetExtLIBPATH pythdir, 'B'
path = VALUE('PATH',,'OS2ENVIRONMENT')
CALL VALUE 'PATH', pythdir';'path, 'OS2ENVIRONMENT'

/* Set the variables required by Python */
CALL VALUE 'PYTHONHOME', pythdir, 'OS2ENVIRONMENT'
path = pythdir'/Lib;'pythdir'/Lib/plat-os2emx;'
path = path || pythdir'/Lib/lib-dynload;'pythdir'/Lib/site-packages;'
CALL VALUE 'PYTHONPATH', path, 'OS2ENVIRONMENT'
CALL VALUE 'TERMINFO',   pythdir'/terminfo', 'OS2ENVIRONMENT'
CALL VALUE 'TERM',       'ansi', 'OS2ENVIRONMENT'

/* Launch the command-line BitTorrent downloader */
SAY
ADDRESS CMD 'python btdownloadcurses.py --max_upload_rate' uploadspeed saveas '"'torrent'"'

/* Restore the environment */
CALL ENDLOCAL

RETURN


/*****************************************************************************
 * TargetFileName()                                                          *
 *                                                                           *
 * Does a quick-and-dirty parse of the .torrent file to determine the        *
 * recommended save filename.                                                *
 *                                                                           *
 * INPUT:  filename of the .torrent file                                     *
 * OUTPUT: save filename parsed from .torrent, or '' if a problem occurred   *
 *****************************************************************************/
TargetFileName: PROCEDURE
    PARSE ARG torrent
    IF torrent = '' THEN RETURN ''

    CALL LINEIN torrent, 1, 0
    DO WHILE LINES( torrent ) > 0
        in = LINEIN( torrent )
        PARSE VAR in . '4:name' size ':' data
        IF ( size \= '') & ( data \= '') THEN
            fndata = SUBSTR( data, 1, size )
        ELSE
            fndata = ''
        IF fndata \= '' THEN LEAVE
    END
    CALL STREAM torrent, 'C', 'CLOSE'

RETURN fndata

Appendix B: Alternate BT_GET.CMD for fully-installed Python

If you have fully installed Python/2 by adding the PATH, LIBPATH and environment entries to CONFIG.SYS and rebooting (necessary on Warp 3 and pre-4.5 Warp 4 FixPak levels), you should use BT_GET2.CMD (or the equivalent) instead of the one above:

/*****************************************************************************
 * BT_GET.CMD  -  OS/2 BitTorrent Commandline Launcher                       *
 * (This script requires Python/2 to be configured in CONFIG.SYS)            *
 *****************************************************************************/

/* ------------------------------------------------------------------------- *
 * DIRECTORY CONSTANTS - EDIT AS APPROPRIATE FOR YOUR SYSTEM                 *
 * ------------------------------------------------------------------------- */

/* Directory where BitTorrent files are installed */
btdir   = 'e:\network\bittorrent'

/* Directory where downloaded files will be placed */
savedir = 'f:\tmp'


/* ------------------------------------------------------------------------- *
 * BITTORRENT CONSTANTS - EDIT AS APPROPRIATE FOR YOUR SYSTEM                *
 * ------------------------------------------------------------------------- */

/* Maximum upload speed when sending to peers */
uploadspeed = 25


/* ------------------------------------------------------------------------- *
 * CHANGES BELOW THIS POINT ARE NOT GENERALLY NECESSARY                      *
 * ------------------------------------------------------------------------- */

/* Check the torrent filename */
PARSE ARG torrent
torrent = STRIP( torrent )
torrent = STRIP( torrent, 'B', '"')
IF torrent = '' THEN DO
    SAY 'Torrent file not specified.'
    RETURN
END

/* Determine the name of the actual file to download (for saving to) */
savename = TargetFileName( torrent )
IF savename \= '' THEN
    saveas = '--saveas' savedir'\'savename
ELSE
    saveas = ''

/* Print some notices */
SAY 'BitTorrent files:' btdir
SAY 'Save directory:  ' savedir
SAY


/* Launch the command-line BitTorrent downloader */
od = DIRECTORY( btdir )
ADDRESS CMD 'python btdownloadcurses.py --max_upload_rate' uploadspeed saveas '"'torrent'"'
CALL DIRECTORY odir

RETURN


/*****************************************************************************
 * TargetFileName()                                                          *
 *                                                                           *
 * Does a quick-and-dirty parse of the .torrent file to determine the        *
 * recommended save filename.                                                *
 *                                                                           *
 * INPUT:  filename of the .torrent file                                     *
 * OUTPUT: save filename parsed from .torrent, or '' if a problem occurred   *
 *****************************************************************************/
TargetFileName: PROCEDURE
    PARSE ARG torrent
    IF torrent = '' THEN RETURN ''

    CALL LINEIN torrent, 1, 0
    DO WHILE LINES( torrent ) > 0
        in = LINEIN( torrent )
        PARSE VAR in . '4:name' size ':' data
        IF ( size \= '') & ( data \= '') THEN
            fndata = SUBSTR( data, 1, size )
        ELSE
            fndata = ''
        IF fndata \= '' THEN LEAVE
    END
    CALL STREAM torrent, 'C', 'CLOSE'

RETURN fndata

References:

Bittorrent
Developer:
Price: Freeware

This article was originally inspired by a Usenet article by dink: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=qvaxlnqqnpbz.hnaiz30.pminews%40news.mi.comcast.giganews.com. I have modified the procedure extensively to make it more flexible.

The official BitTorrent web page: http://bitconjurer.org/BitTorrent.
Brian's BitTorrent FAQ & Guide: http://btfaq.com
The OS/2 port of Python: http://www.pcug.org.au/~andymac/python.html


Feature Index
editor@os2voice.org
< Previous Page | Newsletter Index | Next Page >
VOICE Home Page: http://www.os2voice.org