00001 <?php 00002 00003 /* 00004 * This file is part of the PEIP package. 00005 * (c) 2010 Timo Michna <timomichna/yahoo.de> 00006 * 00007 * For the full copyright and license information, please view the LICENSE 00008 * file that was distributed with this source code. 00009 */ 00010 00022 class PEIP_ABS_Pollable_Channel 00023 extends PEIP_ABS_Interceptable_Message_Channel 00024 implements PEIP_INF_Pollable_Channel { 00025 00026 protected 00027 $messages = array(); 00028 00029 00035 protected function doSend(PEIP_INF_Message $message){ 00036 $this->messages[] = $message; 00037 } 00038 00039 00045 public function receive($timeout = -1){ 00046 $this->getInterceptorDispatcher()->notify('preReceive', array($this)); 00047 $message = NULL; 00048 if($timeout == 0){ 00049 $message = $this->getMessage(); 00050 }elseif($timeout < 0){ 00051 while(!$message = $this->getMessage()){ 00052 00053 } 00054 }else{ 00055 $time = time() + $timeout; 00056 while(($time > time()) && !$message = $this->getMessage()){ 00057 00058 } 00059 } 00060 $this->getInterceptorDispatcher()->notify('preReceive', array($this)); 00061 return $message; 00062 } 00063 00064 00069 protected function getMessage(){ 00070 return array_shift($this->messages); 00071 } 00072 00073 00078 public function clear(){ 00079 $this->messages = array(); 00080 } 00081 00082 00088 public function purge(PEIP_INF_Message_Selector $selector){ 00089 foreach($this->messages as $key=>$message){ 00090 if(!$selector->acceptMessage($message)){ 00091 unset($this->messages[$key]); 00092 } 00093 } 00094 return $this->messages; 00095 } 00096 00097 00098 }