diff --git a/classes/Server/Storage/Abstract.php b/classes/Server/Storage/Abstract.php index 233ba3e..3407f4b 100644 --- a/classes/Server/Storage/Abstract.php +++ b/classes/Server/Storage/Abstract.php @@ -49,6 +49,13 @@ abstract class dropr_Server_Storage_Abstract const TYPE_FILE = 3; + /** + * Create a server storage instance + * + * @param string $type + * @param string $dsn + * @return dropr_Server_Storage_Abstract + */ public static function factory($type, $dsn) { $className = 'dropr_Server_Storage_' . ucfirst($type); @@ -67,16 +74,16 @@ abstract public function pollProcessed($messageId); /** * @return array */ - abstract public function getMessages($type = null, $limit = null); + abstract public function getMessages($channel = 'common', $limit = null); /** * Sets a message to processed state - the implementation must move it out * from the list of active messages to it's not in list of getMessages * anymore * - * @param pmq_Server_Message $message + * @param dropr_Server_Message $message * - * @throws pmq_Server_Exception + * @throws dropr_Server_Exception */ abstract public function setProcessed(dropr_Server_Message $message); diff --git a/classes/Server/Storage/Filesystem.php b/classes/Server/Storage/Filesystem.php index f89d5a9..e5492fd 100644 --- a/classes/Server/Storage/Filesystem.php +++ b/classes/Server/Storage/Filesystem.php @@ -81,22 +81,31 @@ protected function __construct($path) public function put(dropr_Server_Message $message) { $mHandle = $message->getMessage(); - if ($mHandle instanceof SplFileInfo) { - // XXX typ! + if ($mHandle instanceof SplFileInfo || is_string($mHandle)) { // xxx auslagern in eigene funktion - $src = $mHandle->getPathname(); $proc = $this->buildMessagePath($message, self::SPOOLDIR_TYPE_SPOOL); $done = $this->buildMessagePath($message, self::SPOOLDIR_TYPE_PROCESSED); - + if (file_exists ($proc) || file_exists ($done)) { + // the message has already been stored + // XXX write test! return; } - // sometimes php throws a warning but returns true and the file is moved - // .. maybe NFS issue so we have to use the @-operator - if (!@rename($src, $proc)) { - throw new dropr_Server_Exception("Could not save $src to $proc"); + if ($mHandle instanceof SplFileInfo) { + // handle is a file, move it + $src = $mHandle->getPathname(); + + // sometimes php throws a warning but returns true and the file is moved + // .. maybe NFS issue so we have to use the @-operator + if (!@rename($src, $proc)) { + throw new dropr_Server_Exception("Could not save $src to $proc"); + } + } elseif (is_string($mHandle)) { + if (!file_put_contents($proc, $mHandle)) { + throw new dropr_Server_Exception("Could not write content to $proc!"); + } } } else { throw new dropr_Server_Exception('not implemented'); diff --git a/tests/server/LocalFilesystemTransportTest.php b/tests/server/LocalFilesystemTransportTest.php new file mode 100644 index 0000000..44d3b43 --- /dev/null +++ b/tests/server/LocalFilesystemTransportTest.php @@ -0,0 +1,98 @@ + + * @author Boris Erdmann + * @copyright 2007-2008 Soenke Ruempler, Boris Erdmann + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + */ + +/** + * test example for bypassing the client queue for local delivery + * + * basically this is an inter process communication but with + * durability (messages are written to a durable storage) + * + * @author Soenke Ruempler + */ + +require dirname (__FILE__) . '/../../classes/dropr.php'; + +class LocalFilesystemTransportTest extends PHPUnit_Framework_TestCase +{ + /** + * @var dropr_Server_Storage_Filesystem + */ + private $storage; + + private $dir; + + public function setUp() + { + $this->dir = dirname (__FILE__) . '/testspool/server'; + $this->storage = dropr_Server_Storage_Abstract::factory('Filesystem', $this->dir); + } + + public function testPut() + { + $message = new dropr_Server_Message( + 'localhost', + uniqid(null, true), + $message = 'testmessage', + 'common', + 1, + time() + ); + + $this->storage->put($message); + + $messages = $this->storage->getMessages('common'); + + $this->assertEquals(1, count($messages)); + $this->assertEquals('testmessage', (string)$messages[0]); + + + } + + protected function tearDown() + { + // cleanup queue + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->dir)) as $f) { + unlink($f); + } + } +} diff --git a/tests/server/server.php b/tests/server/server.php deleted file mode 100644 index 1044251..0000000 --- a/tests/server/server.php +++ /dev/null @@ -1,27 +0,0 @@ -addDirectInvocationHandler($directClass); - -$server->handle(); - -class ServerDirectInvokeTest implements dropr_Server_DirectInvocation -{ - - public function invokeMessage(dropr_Server_Message $message) - { - echo $message; - return true; - } - -}