Enhanced CTorrent and CTCS

Enhanced CTorrent is a BitTorrent client implemented in C++ to be lightweight and quick and is the continuation of its parent CTorrent.

Enhanced CTorrent has an average CPU load of 4.2% at a download rate of 100KB/s and an upload rate of 100KB/s on an ARM equiped Linkstation Pro.

=LSPro (arm9) - OpenLink=

Building from scratch
$ gunzip ctorrent-1.3.4-dnh2.2.tar.gz $ tar xvf ctorrent-1.3.4-dnh2.2.tar.gz $ ./configure $ make
 * Make sure you are comfotable using either LS-GL Custom Firmware Development or LS-GL Custom Firmware Development
 * Download the latest source distribution of Enhanced CTorrent.
 * If you are compiling in Scratchbox make sure to have  installed in for example  . Either copy it from your Linkstation directly to the Scratchbox environment by using   or install it from anywhere else.
 * Enhanced CTorrent has very little dependencies therefore you should be able to do a simple:

Note: If you get a "C++ compiler cannot create executables" error while running ./configure, you are missing a few packages essential for compiling applications in your machine. Run $ apt-get install build-essential to fix the problem.

This works in Freelink (someone check for openlink?)

Pre-built binary
A pre built ARM binary  is available from LSPro Temporary Upload Folder

=CTorrent Control Server (CTCS)= To control and manage your torrents from an external computer you can make use of the Web interface provided by the CTorrent Control Server (CTCS). This is a Perl script which needs no further configuration.

=Typical Setup= $ ctcs -p 2780 > /dev/null & $ ctorrent -S localhost:2780 torrent.torrent > /dev/null & $ ctorrent -S 127.0.0.1:2780 torrent.torrent > /dev/null & http://:2780
 * Make sure the binary ctorrent is available via your  environment varaiable.
 * Start the CTorrent Control Server and publish the server at port 2780:
 * Start  as a background process downloading   subscribing at the locally running CTCS server at port 2780 and ignoring any console output since we will be controlling it via a web-interface.
 * On some systems, you need to use 127.0.0.1 instead of localhost:
 * On the remote system open up a webbrowser and surf to:

For further information on how to start, stop and manage ctorrent please read the CTorrent user's guide.

=Automatic downloading of torrents= It is very desireable to start a torrent more userfriendly at a remote machine than to manually:
 * 1) Download the torrent on the remote machine to a Linkstation share.
 * 2) Do a remote login to the Linkstation.
 * 3) Start ctorrent with the correct command line parameters.

To semi-automate these tasks, the Bash script below will check a specified directory on your Linkstation for any newly added .torrent files and will spawn a new ctorrent process for it when the torrent was not already activated previously. This way you can start downloading a torrent simply by copying a .torrent file to a certain shared directory on your Linkstation.


 * 1) !/bin/bash


 * 1)                 Activate/deactivates torrents Bash shell script             #
 * 2) Author: Sander van Woensel
 * 3) Description: Starts and stops ctorrent for any newly added or removed
 * 4)              .torrent file found or not found in the path specified on the
 * 5)              command line.
 * 1) Description: Starts and stops ctorrent for any newly added or removed
 * 2)              .torrent file found or not found in the path specified on the
 * 3)              command line.

EXIT_ERROR=1 EXIT_OK=0
 * 1)                                  Constants                                  #
 * 1)                                  Constants                                  #

CTORRENT_PORT=2780

LOG_FILE="/var/log/$(basename $0 .sh).log"

ERROR_NO_CTORRENT="Cannot execute ctorrent. Make sure ctorrent is available via one of the\n      paths in your PATH environment variable." ERROR_INVALID_TORRENT_DIR="Invalid .torrent file directory specified."


 * 1)                                  Functions                                  #
 * 1)                                  Functions                                  #

function usage { printf "Usage: $0
 * 1) Prints script usage.

Spawns a ctorrent process for any new .torrent file in not already activated previously and stops all ctorrent processes from which the .torrent file has been removed fom.

path: Directory to check for new or removed .torrent files.

" }

function log {  echo "[$(date -R)] $@" >> "$LOG_FILE" }

function error {  printf "Error: $*\n\n" usage exit $EXIT_ERROR }

function wait_till_file_uploaded {  SIZE=-1 while CURRENT_SIZE=$(ls -ld "$1" | awk '{print $5}') && [ $CURRENT_SIZE != $SIZE ]; do     sleep 2 SIZE=$CURRENT_SIZE done }
 * 1) Waits until file ($1) does not change in size (and thus is fully copied).

function activate_new {  PROCESSING=$(ps -df | awk '$8 ~ /ctorrent/ {for(i=11; i<=NF; i++) { printf $i" ";}}') # Start new torrents found in $1. # Take into account file names with spaces (read command reads one full line at the time). ls -d1 *.torrent *.tor 2>/dev/null | while read FILE; do     if [ -f "$FILE" ] && [ -r "$FILE" ]; then # If file is NOT found in the currently processing file list, then # spawn a ctorrent process. # Uses grep -F to interpret $FILE as a fixed string ACTIVATED=$(echo "$PROCESSING" | grep -F "$FILE") if [ "x$ACTIVATED" == "x" ]; then wait_till_file_uploaded "$FILE" log "Spawning ctorrent: ctorrent -S localhost:$CTORRENT_PORT \"$FILE\" &" ctorrent -S localhost:$CTORRENT_PORT "$FILE" > /dev/null 2>> "$LOG_FILE" & fi     fi   done }
 * 1) Spawn ctorrent for every new torrent file found.

function deactivate_removed {
 * 1) Kills every ctorrent process from which the .torrent file has been removed.

ps -df | awk '$8 ~ /ctorrent/ { printf $2" "; for(i=11; i<=NF; i++) { printf $i" ";}; printf "\n";}' | while read PID FILE; do     # If the .torrent file belonging to the currently activated ctorrent process has been # removed, stop the ctorrent process. if [ ! -f "$FILE" ]; then log "Terminating ctorrent for \"$FILE\": kill -TERM $PID" # Send the terminate signal to the correct ctorrent process (clean shutdown). kill -TERM $PID 2>> "$LOG_FILE" fi  done }


 * 1)                                    Main                                     #
 * 1)                                    Main                                     #

if [ $# != 1 ] || [ "$1" == -h ] || [ "$1" == --help ]; then usage exit $EXIT_OK fi
 * 1) Did the user requested the help text?

if [ "x$(which ctorrent)" == "x" ]; then error "$ERROR_NO_CTORRENT" fi
 * 1) Check if ctorrent can be executed.

if [ $# != 1 ] || [ ! -d "$1" ] || [ ! -r "$1" ]; then error "$ERROR_INVALID_TORRENT_DIR" fi
 * 1) Check if the specified directory is available and can be read.

cd "$1"
 * 1) Change directory to the .torrent directory. This removes the need
 * 2) to concatenate any paths when executing ls ,etc.

activate_new "$1" deactivate_removed "$1"
 * 1) All tests succeeded; spawn ctorrent.

Installation
*/05 * * * * /etc/cron/cron.d/activate_torrents.sh /mnt/disk1/ /torrents
 * Login your Linkstation as "root" via ssh or telnet.
 * Copy the above text in a new file on your linkstation and save it as
 * Make sure the script is executable for the user "root":
 * 1) chmod u+x /etc/cron/cron.d/activate_torrents.sh
 * Add the script to the cron table so it gets executed at a specified interval. The following command will open up the cron table in your default editor:
 * 1) crontab -e
 * Add the line mentioned below to the end of the crontab file:
 * This will check for new .torrent files in the directory  every 5 minutes and will start a new ctorrent session when necessary.
 * On your computer copy a .torrent file to the share:, wait 5 minutes and check if the ctorrent process started downloading by executing:
 * 1) ps -df | grep ctorrent
 * You can also check CTCS by browsing to:.

Starting CTCS automatically
Thanks to User:Kaiten for the example using sshd


 * Create a file in /etc/init.d named ctcs.sh:

vi /etc/init.d/ctcs.sh


 * The file should contain the following. If you put ctcs somewhere other than /usr/local/bin/, than you will need to change the contents accordingly:


 * 1) ! /bin/sh
 * 2) Start/Stop/Restart the ctorrent control server

test -f /usr/local/bin/ctcs || exit 0

case "$1" in start) echo -n "Start services: ctcs"        /usr/local/bin/ctcs -p 2780 > /dev/null &        ;; stop)   echo -n "Stop services: ctcs" killall ctcs ;; restart)       $0 stop        $0 start        ;;        exit 1        ;; esac exit 0
 * )     echo "Usage: $0 start|stop|restart"


 * Save the file and quit. Then change the file so that it is executable:

chmod 0755 /etc/init.d/ctcs.sh


 * Now you will need to edit the main startup script to add this. If you have not already, back up rcS:

cp /etc/init.d/rcS /etc/init.d/rcS.bak


 * Now to edit:

vi /etc/init.d/rcS


 * Add ctcs.sh to the line containing:

echo "** step3 **" for cmd in micon_setup.sh atalk.sh ftpd.sh httpd.sh smb.sh clientUtil_servd.sh lsprcvd.sh daemonwatch.sh cron.sh logchkd.sh checkconfig.sh nfs.sh modules.sh set_quota.sh sshd.sh do       exec_sh $cmd done


 * Like this:

echo "** step3 **" for cmd in micon_setup.sh atalk.sh ftpd.sh httpd.sh smb.sh clientUtil_servd.sh lsprcvd.sh daemonwatch.sh cron.sh logchkd.sh checkconfig.sh nfs.sh modules.sh set_quota.sh sshd.sh ctcs.sh do       exec_sh $cmd done


 * Now, ctcs will launch on startup. It can be accessed from http://linkstationIP:2780/

=Related articles=
 * Mldonkey - multi-network file-sharing client