[284] | 1 | <?php |
---|
| 2 | /***********************************************************************************\ |
---|
| 3 | * eGroupWare - Contacts Center * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * Written by: * |
---|
| 6 | * - Brian W. Bosh from Multi-threading strategies in PHP at * |
---|
| 7 | * http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html * |
---|
| 8 | * Adapted by: * |
---|
| 9 | * - Mário César Kolling <mario.kolling@serpro.gov.br> * |
---|
| 10 | * ----------------------------------------------------------------------------------* |
---|
| 11 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 12 | * under the terms of the GNU General Public License as published by the * |
---|
| 13 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 14 | * option) any later version. * |
---|
| 15 | \***********************************************************************************/ |
---|
| 16 | |
---|
| 17 | require 'ThreadUtility.inc.php'; |
---|
| 18 | |
---|
| 19 | /* |
---|
| 20 | * This is the class that abstracts the details of process management |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | class Thread { |
---|
| 24 | var $pref ; |
---|
| 25 | var $pipes; |
---|
| 26 | var $pid; |
---|
| 27 | var $stdout; |
---|
| 28 | //var $timeout; |
---|
| 29 | |
---|
| 30 | function Thread() { |
---|
| 31 | $this->pref = 0; |
---|
| 32 | $this->stdout = ""; |
---|
| 33 | $this->pipes = (array)NULL; |
---|
| 34 | //$this->timeout = 30000; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | function Create ($url, $env) { |
---|
| 38 | $t = new Thread; |
---|
| 39 | $descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w")); |
---|
[3395] | 40 | $t->pref = proc_open ("php -q ". PHPGW_SERVER_ROOT . "/contactcenter/inc/" . $url, $descriptor, $t->pipes, NULL, $env); |
---|
[284] | 41 | stream_set_blocking ($t->pipes[1], 0); |
---|
| 42 | stream_set_blocking ($t->pipes[2], 0); |
---|
| 43 | //usleep ($this->timeout); |
---|
| 44 | return $t; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | function isActive () { |
---|
| 48 | $status = proc_get_status($this->pref); |
---|
| 49 | return $status['running']; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | function close () { |
---|
| 53 | $r = proc_terminate($this->pref); |
---|
| 54 | $this->pref = NULL; |
---|
| 55 | return $r; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | function tell ($thought, $params = NULL) { |
---|
| 59 | fwrite ($this->pipes[0], $thought . "\n"); |
---|
| 60 | if (is_array ($params)) { |
---|
| 61 | foreach ($params as $param) { |
---|
| 62 | fwrite ($this->pipes[0], $param . "\n"); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | function readResponse() |
---|
| 68 | { |
---|
| 69 | $response = NULL; |
---|
| 70 | $read = array($this->pipes[1]); |
---|
| 71 | $write = NULL; |
---|
| 72 | $exception = NULL; |
---|
| 73 | |
---|
| 74 | if (stream_select($read, $write, $exception, 0) > 0){ |
---|
| 75 | $response = $this->listen(); |
---|
| 76 | return processresponse($response); |
---|
| 77 | } |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | return $response; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | function listen () { |
---|
| 85 | $buffer = $this->stdout; |
---|
| 86 | $this->stdout = ""; |
---|
| 87 | while ($r = fgets ($this->pipes[1], 1024)) { |
---|
| 88 | $buffer .= $r; |
---|
| 89 | } |
---|
| 90 | return $buffer; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | function getError () { |
---|
| 94 | $buffer = ""; |
---|
| 95 | while ($r = fgets ($this->pipes[2], 1024)) { |
---|
| 96 | $buffer .= $r; |
---|
| 97 | } |
---|
| 98 | return $buffer; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | ?> |
---|