1
0

ClientHandler.tornado.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* $Id: ClientHandler.tornado.php 2832 2007-04-08 10:31:44Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * class ClientHandler for tornado-client
  17. */
  18. class ClientHandlerTornado extends ClientHandler
  19. {
  20. // public fields
  21. // tornado-bin
  22. var $tornadoBin = "";
  23. // =========================================================================
  24. // ctor
  25. // =========================================================================
  26. /**
  27. * ctor
  28. */
  29. function ClientHandlerTornado() {
  30. global $cfg;
  31. $this->type = "torrent";
  32. $this->client = "tornado";
  33. $this->binSystem = "python";
  34. $this->binSocket = "python";
  35. $this->binClient = "tftornado.py";
  36. $this->tornadoBin = $cfg["docroot"]."bin/clients/tornado/tftornado.py";
  37. }
  38. // =========================================================================
  39. // public methods
  40. // =========================================================================
  41. /**
  42. * starts a client
  43. *
  44. * @param $transfer name of the transfer
  45. * @param $interactive (boolean) : is this a interactive startup with dialog ?
  46. * @param $enqueue (boolean) : enqueue ?
  47. */
  48. function start($transfer, $interactive = false, $enqueue = false) {
  49. global $cfg;
  50. // set vars
  51. $this->_setVarsForTransfer($transfer);
  52. // log
  53. $this->logMessage($this->client."-start : ".$transfer."\n", true);
  54. // do tornado special-pre-start-checks
  55. // check to see if the path to the python script is valid
  56. if (!is_file($this->tornadoBin)) {
  57. $this->state = CLIENTHANDLER_STATE_ERROR;
  58. $msg = "path for tftornado.py is not valid";
  59. AuditAction($cfg["constants"]["error"], $msg);
  60. $this->logMessage($msg."\n", true);
  61. array_push($this->messages, $msg);
  62. array_push($this->messages, "tornadoBin : ".$this->tornadoBin);
  63. // write error to stat
  64. $sf = new StatFile($this->transfer, $this->owner);
  65. $sf->time_left = 'Error';
  66. $sf->write();
  67. // return
  68. return false;
  69. }
  70. // init starting of client
  71. $this->_init($interactive, $enqueue, true, ($cfg['enable_sharekill'] == 1));
  72. // only continue if init succeeded (skip start / error)
  73. if ($this->state != CLIENTHANDLER_STATE_READY) {
  74. if ($this->state == CLIENTHANDLER_STATE_ERROR) {
  75. $msg = "Error after init (".$transfer.",".$interactive.",".$enqueue.",true,".$cfg['enable_sharekill'].")";
  76. array_push($this->messages , $msg);
  77. $this->logMessage($msg."\n", true);
  78. }
  79. // return
  80. return false;
  81. }
  82. // file-prio
  83. if ($cfg["enable_file_priority"])
  84. setFilePriority($transfer);
  85. // pythonCmd
  86. $pyCmd = $cfg["pythonCmd"] . " -OO";
  87. // build the command-string
  88. $skipHashCheck = "";
  89. if ((!(empty($this->skip_hash_check))) && (getTorrentDataSize($transfer) > 0))
  90. $skipHashCheck = " --check_hashes 0";
  91. $filePrio = "";
  92. if (@file_exists($this->transferFilePath.".prio")) {
  93. $priolist = explode(',', @file_get_contents($this->transferFilePath.".prio"));
  94. $priolist = implode(',', array_slice($priolist, 1, $priolist[0]));
  95. $filePrio = " --priority ".tfb_shellencode($priolist);
  96. }
  97. // build the command-string
  98. // note : order of args must not change for ps-parsing-code in
  99. // RunningTransferTornado
  100. $this->command = "cd ".tfb_shellencode($this->savepath).";";
  101. $this->command .= " HOME=".tfb_shellencode($cfg["path"]);
  102. $this->command .= "; export HOME;";
  103. $this->command .= $this->umask;
  104. $this->command .= " nohup ";
  105. $this->command .= $this->nice;
  106. $this->command .= $pyCmd . " " .tfb_shellencode($this->tornadoBin);
  107. $this->command .= " ".tfb_shellencode($this->runtime);
  108. $this->command .= " ".tfb_shellencode($this->sharekill_param);
  109. $this->command .= " ".tfb_shellencode($this->owner);
  110. $this->command .= " ".tfb_shellencode($this->transferFilePath);
  111. $this->command .= " --responsefile ".tfb_shellencode($this->transferFilePath);
  112. $this->command .= " --display_interval 1";
  113. $this->command .= " --max_download_rate ".tfb_shellencode($this->drate);
  114. $this->command .= " --max_upload_rate ".tfb_shellencode($this->rate);
  115. $this->command .= " --max_uploads ".tfb_shellencode($this->maxuploads);
  116. $this->command .= " --minport ".tfb_shellencode($this->port);
  117. $this->command .= " --maxport ".tfb_shellencode($this->maxport);
  118. $this->command .= " --rerequest_interval ".tfb_shellencode($this->rerequest);
  119. $this->command .= " --super_seeder ".tfb_shellencode($this->superseeder);
  120. $this->command .= " --max_connections ".tfb_shellencode($this->maxcons);
  121. $this->command .= $skipHashCheck;
  122. $this->command .= $filePrio;
  123. if (strlen($cfg["btclient_tornado_options"]) > 0)
  124. $this->command .= " ".$cfg["btclient_tornado_options"];
  125. $this->command .= " 1>> ".tfb_shellencode($this->transferFilePath.".log");
  126. $this->command .= " 2>> ".tfb_shellencode($this->transferFilePath.".log");
  127. $this->command .= " &";
  128. // start the client
  129. $this->_start();
  130. }
  131. /**
  132. * set upload rate of a transfer
  133. *
  134. * @param $transfer
  135. * @param $uprate
  136. * @param $autosend
  137. */
  138. function setRateUpload($transfer, $uprate, $autosend = false) {
  139. // set rate-field
  140. $this->rate = $uprate;
  141. // add command
  142. CommandHandler::add($transfer, "u".$uprate);
  143. // send command to client
  144. if ($autosend)
  145. CommandHandler::send($transfer);
  146. }
  147. /**
  148. * set download rate of a transfer
  149. *
  150. * @param $transfer
  151. * @param $downrate
  152. * @param $autosend
  153. */
  154. function setRateDownload($transfer, $downrate, $autosend = false) {
  155. // set rate-field
  156. $this->drate = $downrate;
  157. // add command
  158. CommandHandler::add($transfer, "d".$downrate);
  159. // send command to client
  160. if ($autosend)
  161. CommandHandler::send($transfer);
  162. }
  163. /**
  164. * set runtime of a transfer
  165. *
  166. * @param $transfer
  167. * @param $runtime
  168. * @param $autosend
  169. * @return boolean
  170. */
  171. function setRuntime($transfer, $runtime, $autosend = false) {
  172. // set runtime-field
  173. $this->runtime = $runtime;
  174. // add command
  175. CommandHandler::add($transfer, "r".(($this->runtime == "True") ? "1" : "0"));
  176. // send command to client
  177. if ($autosend)
  178. CommandHandler::send($transfer);
  179. }
  180. /**
  181. * set sharekill of a transfer
  182. *
  183. * @param $transfer
  184. * @param $sharekill
  185. * @param $autosend
  186. * @return boolean
  187. */
  188. function setSharekill($transfer, $sharekill, $autosend = false) {
  189. // set sharekill
  190. $this->sharekill = intval($sharekill);
  191. // recalc sharekill
  192. if ($this->_recalcSharekill() === false)
  193. return false;
  194. // add command
  195. CommandHandler::add($transfer, "s".$this->sharekill_param);
  196. // send command to client
  197. if ($autosend)
  198. CommandHandler::send($transfer);
  199. // return
  200. return true;
  201. }
  202. }
  203. ?>