Difference between revisions of "Enhanced CTorrent and CTCS"

From NAS-Central Buffalo - The Linkstation Wiki
Jump to: navigation, search
(added ==Starting CTCS automatically==)
m
Line 17: Line 17:
  
 
==Pre-built binary==
 
==Pre-built binary==
A pre built ARM binary <code>ctorrent</code> is available from [http://downloads.linkstationwiki.net/Uploads/OldUploads/LS_Pro_temporary/Binaries/ LSPro Temporary Upload Folder]
+
A pre built ARM binary <code>ctorrent</code> is available from [http://downloads.nas-central.org/Uploads/OldUploads/LS_Pro_temporary/Binaries/ LSPro Temporary Upload Folder]
  
 
=CTorrent Control Server (CTCS)=
 
=CTorrent Control Server (CTCS)=

Revision as of 02:24, 11 September 2007

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.

Contents

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
  • If you are compiling in Scratchbox make sure to have libstdc++.so installed in for example /usr/lib. Either copy it from your Linkstation directly to the Scratchbox environment by using scp or install it from anywhere else.
  • Enhanced CTorrent has very little dependencies therefore you should be able to do a simple:
$ ./configure
$ make

Pre-built binary

A pre built ARM binary ctorrent 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

  • Make sure the binary ctorrent is available via your PATH environment varaiable.
  • Start the CTorrent Control Server and publish the server at port 2780:
$ ctcs -p 2780 > /dev/null &
  • Start ctorrent as a background process downloading torrent.torrent 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.
$ ctorrent -S localhost:2780 torrent.torrent > /dev/null &
  • On the remote system open up a webbrowser and surf to:
http://<linkstation-ip>:2780


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.

#!/bin/bash

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

#-----------------------------------------------------------------------------#
#                                  Constants                                  #
#-----------------------------------------------------------------------------#
EXIT_ERROR=1
EXIT_OK=0

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."

#-----------------------------------------------------------------------------#
#                                  Functions                                  #
#-----------------------------------------------------------------------------#

# Prints script usage.
function usage
{
printf "Usage: $0 <path>

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

     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
}

# Waits until file ($1) does not change in size (and thus is fully copied).
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
}

# Spawn ctorrent for every new torrent file found.
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
   
}

# Kills every ctorrent process from which the .torrent file has been removed.
function deactivate_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
}

#-----------------------------------------------------------------------------#
#                                    Main                                     #
#-----------------------------------------------------------------------------#

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

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

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

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

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


Installation

  • Login your Linkstation as "root" via ssh or telnet.
  • Copy the above text in a new file on your linkstation and save it as /etc/cron/cron.d/activate_torrents.sh
  • Make sure the script is executable for the user "root":
# 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:
# crontab -e
  • Add the line mentioned below to the end of the crontab file:
*/05 * * * * /etc/cron/cron.d/activate_torrents.sh /mnt/disk1/<share>/torrents
  • This will check for new .torrent files in the directory /mnt/disk1/<share>/torrents every 5 minutes and will start a new ctorrent session when necessary.
  • On your computer copy a .torrent file to the share: <share>/torrents, wait 5 minutes and check if the ctorrent process started downloading by executing:
# ps -df | grep ctorrent
  • You can also check CTCS by browsing to: http://<linkstation-ip>:2780.


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:
#! /bin/sh
# 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
        ;;
*)      echo "Usage: $0 start|stop|restart"
        exit 1
        ;;
esac
exit 0
  • 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