1
0

FluxdServiceMod.Qmgr.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /* $Id: FluxdServiceMod.Qmgr.php 2943 2007-04-23 18:43:05Z 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. // class for the Fluxd-Service-module Qmgr
  16. class FluxdQmgr extends FluxdServiceMod
  17. {
  18. // =========================================================================
  19. // public static methods
  20. // =========================================================================
  21. /**
  22. * accessor for singleton
  23. *
  24. * @return FluxdQmgr
  25. */
  26. function getInstance() {
  27. global $instanceFluxdQmgr;
  28. // initialize if needed
  29. if (!isset($instanceFluxdQmgr))
  30. FluxdQmgr::initialize();
  31. return $instanceFluxdQmgr;
  32. }
  33. /**
  34. * initialize FluxdQmgr.
  35. */
  36. function initialize() {
  37. global $instanceFluxdQmgr;
  38. // create instance
  39. if (!isset($instanceFluxdQmgr))
  40. $instanceFluxdQmgr = new FluxdQmgr();
  41. }
  42. /**
  43. * getState
  44. *
  45. * @return state
  46. */
  47. function getState() {
  48. global $instanceFluxdQmgr;
  49. return (isset($instanceFluxdQmgr))
  50. ? $instanceFluxdQmgr->state
  51. : FLUXDMOD_STATE_NULL;
  52. }
  53. /**
  54. * getMessages
  55. *
  56. * @return array
  57. */
  58. function getMessages() {
  59. global $instanceFluxdQmgr;
  60. return (isset($instanceFluxdQmgr))
  61. ? $instanceFluxdQmgr->messages
  62. : array();
  63. }
  64. /**
  65. * getModState
  66. *
  67. * @return state
  68. */
  69. function getModState() {
  70. global $instanceFluxdQmgr;
  71. return (isset($instanceFluxdQmgr))
  72. ? $instanceFluxdQmgr->modstate
  73. : FLUXDMOD_STATE_NULL;
  74. }
  75. /**
  76. * isRunning
  77. *
  78. * @return boolean
  79. */
  80. function isRunning() {
  81. global $instanceFluxdQmgr;
  82. return (isset($instanceFluxdQmgr))
  83. ? ($instanceFluxdQmgr->modstate == FLUXDMOD_STATE_RUNNING)
  84. : false;
  85. }
  86. /**
  87. * getQueuedTransfers
  88. *
  89. * @param $user
  90. * @return string
  91. */
  92. function getQueuedTransfers($user = "") {
  93. global $instanceFluxdQmgr;
  94. return (isset($instanceFluxdQmgr))
  95. ? $instanceFluxdQmgr->instance_getQueuedTransfers($user)
  96. : "";
  97. }
  98. /**
  99. * countQueuedTransfers
  100. *
  101. * @param $user
  102. * @return int
  103. */
  104. function countQueuedTransfers($user = "") {
  105. global $instanceFluxdQmgr;
  106. return (isset($instanceFluxdQmgr))
  107. ? $instanceFluxdQmgr->instance_countQueuedTransfers($user)
  108. : 0;
  109. }
  110. /**
  111. * enqueue
  112. *
  113. * @param $transfer
  114. * @param $user
  115. */
  116. function enqueueTransfer($transfer, $user) {
  117. global $instanceFluxdQmgr;
  118. if (isset($instanceFluxdQmgr))
  119. $instanceFluxdQmgr->instance_enqueueTransfer($transfer, $user);
  120. }
  121. /**
  122. * dequeue
  123. *
  124. * @param $transfer
  125. * @param $user
  126. */
  127. function dequeueTransfer($transfer, $user) {
  128. global $instanceFluxdQmgr;
  129. if (isset($instanceFluxdQmgr))
  130. $instanceFluxdQmgr->instance_dequeueTransfer($transfer, $user);
  131. }
  132. // =========================================================================
  133. // ctor
  134. // =========================================================================
  135. /**
  136. * ctor
  137. */
  138. function FluxdQmgr() {
  139. // name
  140. $this->moduleName = "Qmgr";
  141. // initialize
  142. $this->instance_initialize();
  143. }
  144. // =========================================================================
  145. // public methods
  146. // =========================================================================
  147. /**
  148. * instance_getQueuedTransfers
  149. *
  150. * @param $user
  151. * @return string
  152. */
  153. function instance_getQueuedTransfers($user = "") {
  154. return ($this->modstate == FLUXDMOD_STATE_RUNNING)
  155. ? Fluxd::sendServiceCommand($this->moduleName, 'list-queue', 1)
  156. : "";
  157. }
  158. /**
  159. * instance_countQueuedTransfers
  160. *
  161. * @param $user
  162. * @return int
  163. */
  164. function instance_countQueuedTransfers($user = "") {
  165. return ($this->modstate == FLUXDMOD_STATE_RUNNING)
  166. ? Fluxd::sendServiceCommand($this->moduleName, 'count-queue', 1)
  167. : 0;
  168. }
  169. /**
  170. * instance_enqueue
  171. *
  172. * @param $transfer
  173. * @param $user
  174. */
  175. function instance_enqueueTransfer($transfer, $user) {
  176. if ($this->modstate == FLUXDMOD_STATE_RUNNING) {
  177. // send command
  178. Fluxd::sendServiceCommand($this->moduleName, 'enqueue;'.$transfer.';'.$user, 0);
  179. // just 2 sec... dont stress fluxd
  180. sleep(2);
  181. }
  182. }
  183. /**
  184. * instance_dequeue
  185. *
  186. * @param $transfer
  187. * @param $user
  188. */
  189. function instance_dequeueTransfer($transfer, $user) {
  190. global $cfg;
  191. if ($this->modstate == FLUXDMOD_STATE_RUNNING) {
  192. if (isTransferRunning($transfer)) {
  193. // transfer has been started...log
  194. AuditAction($cfg["constants"]["unqueued_transfer"], $transfer . "has been already started.");
  195. } else {
  196. // send command
  197. Fluxd::sendServiceCommand($this->moduleName, 'dequeue;'.$transfer.';'.$user, 0);
  198. // flag the transfer as stopped (in db)
  199. stopTransferSettings($transfer);
  200. // update the stat file.
  201. $this->_updateStatFile($transfer);
  202. // log
  203. AuditAction($cfg["constants"]["unqueued_transfer"], $transfer);
  204. // just 2 sec... dont stress fluxd
  205. sleep(2);
  206. }
  207. }
  208. }
  209. // =========================================================================
  210. // private methods
  211. // =========================================================================
  212. /**
  213. * _updateStatFile
  214. *
  215. * @param $transfer
  216. */
  217. function _updateStatFile($transfer) {
  218. global $transfers;
  219. $modded = 0;
  220. // create sf object
  221. $sf = new StatFile($transfer, getOwner($transfer));
  222. if ($sf->percent_done > 0 && $sf->percent_done < 100) {
  223. // has downloaded something at some point, mark it is incomplete
  224. $sf->running = "0";
  225. $sf->time_left = "Transfer Stopped";
  226. $modded++;
  227. }
  228. if ($modded == 0) {
  229. if ($sf->percent_done == 0 || $sf->percent_done == "") {
  230. // We are going to write a '2' on the front of the stat file so that it will be set back to New Status
  231. $sf->running = "2";
  232. $sf->time_left = "";
  233. $modded++;
  234. }
  235. }
  236. if ($modded == 0) {
  237. if ($sf->percent_done == 100) {
  238. // transfer was done and is now being stopped
  239. $sf->running = "0";
  240. $sf->time_left = "Download Succeeded!";
  241. $modded++;
  242. }
  243. }
  244. if ($modded == 0) {
  245. // hmmm this stat-file is quite strange... just rewrite it stopped.
  246. $sf->running = "0";
  247. $sf->time_left = "Transfer Stopped";
  248. }
  249. // Write out the new Stat File
  250. $sf->write();
  251. // set transfers-cache
  252. cacheTransfersSet();
  253. }
  254. }
  255. ?>