ClientHandler.mainline.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /* $Id: ClientHandler.mainline.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 mainline-client
  17. */
  18. class ClientHandlerMainline extends ClientHandler
  19. {
  20. // public fields
  21. // mainline-bin
  22. var $mainlineBin = "";
  23. // =========================================================================
  24. // ctor
  25. // =========================================================================
  26. /**
  27. * ctor
  28. */
  29. function ClientHandlerMainline() {
  30. global $cfg;
  31. $this->type = "torrent";
  32. $this->client = "mainline";
  33. $this->binSystem = "python";
  34. $this->binSocket = "python";
  35. $this->binClient = "tfmainline.py";
  36. $this->mainlineBin = $cfg["docroot"]."bin/clients/mainline/tfmainline.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 mainline special-pre-start-checks
  55. // check to see if the path to the python script is valid
  56. if (!is_file($this->mainlineBin)) {
  57. $this->state = CLIENTHANDLER_STATE_ERROR;
  58. $msg = "path for tfmainline.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, "mainlineBin : ".$this->mainlineBin);
  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. // build the command-string
  83. // note : order of args must not change for ps-parsing-code in
  84. // RunningTransferMainline
  85. $this->command = "cd ".tfb_shellencode($this->savepath).";";
  86. $this->command .= " HOME=".tfb_shellencode($cfg["path"]);
  87. $this->command .= "; export HOME;";
  88. $this->command .= $this->umask;
  89. $this->command .= " nohup ";
  90. $this->command .= $this->nice;
  91. $this->command .= $cfg["pythonCmd"] . " -OO" . " " .tfb_shellencode($this->mainlineBin);
  92. $this->command .= " --tf_owner ".tfb_shellencode($this->owner);
  93. $this->command .= " --display_interval 1";
  94. $this->command .= " --save_incomplete_in ".tfb_shellencode($this->savepath);
  95. $this->command .= " --save_in ".tfb_shellencode($this->savepath);
  96. $this->command .= " --die_when_done ".tfb_shellencode($this->runtime);
  97. $this->command .= " --seed_limit ".tfb_shellencode($this->sharekill_param);
  98. $this->command .= ($this->drate != 0)
  99. ? " --max_download_rate " . tfb_shellencode($this->drate * 1024)
  100. : " --max_download_rate 125000000"; // 1 GBit local net = 125MB/s
  101. $this->command .= ($this->rate != 0)
  102. ? " --max_upload_rate " . tfb_shellencode($this->rate * 1024)
  103. : " --max_upload_rate 125000000"; // 1 GBit local net = 125MB/s
  104. $this->command .= " --max_uploads ".tfb_shellencode($this->maxuploads);
  105. $this->command .= " --minport ".tfb_shellencode($this->port);
  106. $this->command .= " --maxport ".tfb_shellencode($this->maxport);
  107. $this->command .= " --rerequest_interval ".tfb_shellencode($this->rerequest);
  108. $this->command .= " --max_initiate ".tfb_shellencode($this->maxcons);
  109. if ((!(empty($this->skip_hash_check))) && (getTorrentDataSize($this->transfer) > 0))
  110. $this->command .= " --no_check_hashes";
  111. if (strlen($cfg["btclient_mainline_options"]) > 0)
  112. $this->command .= " ".$cfg["btclient_mainline_options"];
  113. $this->command .= " ".tfb_shellencode($this->transferFilePath);
  114. $this->command .= " 1>> ".tfb_shellencode($this->transferFilePath.".log");
  115. $this->command .= " 2>> ".tfb_shellencode($this->transferFilePath.".log");
  116. $this->command .= " &";
  117. // start the client
  118. $this->_start();
  119. }
  120. /**
  121. * set upload rate of a transfer
  122. *
  123. * @param $transfer
  124. * @param $uprate
  125. * @param $autosend
  126. */
  127. function setRateUpload($transfer, $uprate, $autosend = false) {
  128. // set rate-field
  129. $this->rate = $uprate;
  130. // add command
  131. $nrate = ($uprate != 0)
  132. ? $uprate * 1024
  133. : 125000000; // 1 GBit local net = 125MB/s
  134. CommandHandler::add($transfer, "u".$nrate);
  135. // send command to client
  136. if ($autosend)
  137. CommandHandler::send($transfer);
  138. }
  139. /**
  140. * set download rate of a transfer
  141. *
  142. * @param $transfer
  143. * @param $downrate
  144. * @param $autosend
  145. */
  146. function setRateDownload($transfer, $downrate, $autosend = false) {
  147. // set rate-field
  148. $this->drate = $downrate;
  149. // add command
  150. $nrate = ($downrate != 0)
  151. ? $downrate * 1024
  152. : 125000000; // 1 GBit local net = 125MB/s
  153. CommandHandler::add($transfer, "d".$nrate);
  154. // send command to client
  155. if ($autosend)
  156. CommandHandler::send($transfer);
  157. }
  158. /**
  159. * set runtime of a transfer
  160. *
  161. * @param $transfer
  162. * @param $runtime
  163. * @param $autosend
  164. * @return boolean
  165. */
  166. function setRuntime($transfer, $runtime, $autosend = false) {
  167. // set runtime-field
  168. $this->runtime = $runtime;
  169. // add command
  170. CommandHandler::add($transfer, "r".(($this->runtime == "True") ? "1" : "0"));
  171. // send command to client
  172. if ($autosend)
  173. CommandHandler::send($transfer);
  174. }
  175. /**
  176. * set sharekill of a transfer
  177. *
  178. * @param $transfer
  179. * @param $sharekill
  180. * @param $autosend
  181. * @return boolean
  182. */
  183. function setSharekill($transfer, $sharekill, $autosend = false) {
  184. // set sharekill
  185. $this->sharekill = intval($sharekill);
  186. // recalc sharekill
  187. if ($this->_recalcSharekill() === false)
  188. return false;
  189. // add command
  190. CommandHandler::add($transfer, "s".$this->sharekill_param);
  191. // send command to client
  192. if ($autosend)
  193. CommandHandler::send($transfer);
  194. // return
  195. return true;
  196. }
  197. }
  198. ?>