TextFileReceiver.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2009 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012     final class TextFileReceiver implements MessageQueueReceiver
00013     {
00014         private $queue  = null;
00015         private $stream = null;
00016         
00020         public static function create()
00021         {
00022             return new self;
00023         }
00024         
00028         public function setQueue(MessageQueue $queue)
00029         {
00030             Assert::isInstance($queue, 'TextFileQueue');
00031             
00032             $this->queue = $queue;
00033             
00034             return $this;
00035         }
00036         
00040         public function getQueue()
00041         {
00042             return $this->queue;
00043         }
00044         
00048         public function receive($uTimeout = null)
00049         {
00050             if (!$this->queue)
00051                 throw new WrongStateException('you must set the queue first');
00052             
00053             if ($uTimeout && $this->getStream()->isEof())
00054                 usleep($uTimeout);
00055             
00056             $string = $this->getStream()->readString();
00057             
00058             if (!$string && $this->getStream()->isEof())
00059                 return null;
00060             
00061             $this->getQueue()->setOffset($this->getStream()->getOffset());
00062             
00063             $string = rtrim($string, PHP_EOL);
00064             
00065             $chunks = preg_split("/\t/", $string, 2);
00066             
00067             $time = isset($chunks[0]) ? $chunks[0] : null;
00068             $text = isset($chunks[1]) ? $chunks[1] : null;
00069             
00070             Assert::isNotNull($time);
00071             
00072             $result = TextMessage::create()->
00073                 setTimestamp(Timestamp::create($time))->
00074                 setText($text);
00075             
00076             return $result;
00077         }
00078         
00082         private function getStream()
00083         {
00084             if (!$this->stream) {
00085                 Assert::isNotNull($this->queue->getFileName());
00086                 
00087                 $this->stream = FileInputStream::create(
00088                     $this->queue->getFileName()
00089                 )->
00090                     seek($this->queue->getOffset());
00091             }
00092             
00093             return $this->stream;
00094         }
00095     }
00096 ?>