Permalink
Browse files

Created a locking exception to be thrown when there are locking probl…

…ems.
  • Loading branch information...
1 parent 84b098e commit ede8a11f2d46d96612371ec9492660b893e58d9f @boenrobot boenrobot committed Mar 3, 2012
View
35 src/PEAR2/Net/Transmitter/LockException.php
@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * ~~summary~~
+ *
+ * ~~description~~
+ *
+ * PHP version 5
+ *
+ * @category Net
+ * @package PEAR2_Net_Transmitter
+ * @author Vasil Rangelov <boen.robot@gmail.com>
+ * @copyright 2011 Vasil Rangelov
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @version GIT: $Id$
+ * @link http://pear2.php.net/PEAR2_Net_Transmitter
+ */
+/**
+ * The namespace declaration.
+ */
+namespace PEAR2\Net\Transmitter;
+
+/**
+ * Exception thrown when something goes wrong when dealing with locks.
+ *
+ * @category Net
+ * @package PEAR2_Net_Transmitter
+ * @author Vasil Rangelov <boen.robot@gmail.com>
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @link http://pear2.php.net/PEAR2_Net_Transmitter
+ */
+class LockException extends \RuntimeException implements Exception
+{
+
+}
View
10 src/PEAR2/Net/Transmitter/Stream.php
@@ -116,6 +116,16 @@ public function isFresh()
}
/**
+ * Checks whether the wrapped stream is a persistent one.
+ *
+ * @return bool TRUE if the stream is a persistent one, FALSE otherwise.
+ */
+ public function isPersistent()
+ {
+ return $this->persist;
+ }
+
+ /**
* Sets the timeout for the stream.
*
* @param int $seconds Timeout in seconds.
View
69 src/PEAR2/Net/Transmitter/TcpClient.php
@@ -158,50 +158,62 @@ protected function createException($message, $code = 0)
* ones. Setting this to FALSE will make the function only obtain the locks
* which are not already obtained.
*
- * @return int The previous state or FALSE on failure.
+ * @return int The previous state or FALSE if the connection is not
+ * persistent or arguments are invalid.
*/
public function lock($direction = self::DIRECTION_ALL, $replace = false)
{
if ($this->persist && is_int($direction)) {
- $result = $old = self::$lockState[$this->uri];
+ $old = self::$lockState[$this->uri];
if ($direction & self::DIRECTION_SEND) {
if (($old & self::DIRECTION_SEND)
|| $this->shmHandler->lock(self::DIRECTION_SEND)
) {
- $result |= self::DIRECTION_SEND;
+ self::$lockState[$this->uri] |= self::DIRECTION_SEND;
} else {
- return false;
+ throw new LockException('Unable to obtain sending lock.');
+ }
+ } elseif ($replace) {
+ if (!($old & self::DIRECTION_SEND)
+ || $this->shmHandler->unlock(self::DIRECTION_SEND)
+ ) {
+ self::$lockState[$this->uri] &= ~self::DIRECTION_SEND;
+ } else {
+ throw new LockException('Unable to release sending lock.');
}
- } elseif ($replace && !($old & self::DIRECTION_SEND)
- || $this->shmHandler->unlock(self::DIRECTION_SEND)
- ) {
- $result &= ~self::DIRECTION_SEND;
- } else {
- return false;
}
try {
if ($direction & self::DIRECTION_RECEIVE) {
if (($old & self::DIRECTION_RECEIVE)
|| $this->shmHandler->lock(self::DIRECTION_RECEIVE)
) {
- $result |= self::DIRECTION_RECEIVE;
+ self::$lockState[$this->uri] |= self::DIRECTION_RECEIVE;
} else {
- throw new \Exception;
+ throw new LockException(
+ 'Unable to obtain receiving lock.'
+ );
+ }
+ } elseif ($replace) {
+ if (!($old & self::DIRECTION_RECEIVE)
+ || $this->shmHandler->unlock(self::DIRECTION_RECEIVE)
+ ) {
+ self::$lockState[$this->uri]
+ &= ~self::DIRECTION_RECEIVE;
+ } else {
+ throw new LockException(
+ 'Unable to release receiving lock.'
+ );
}
- } elseif ($replace && !($old & self::DIRECTION_RECEIVE)
- || $this->shmHandler->unlock(self::DIRECTION_RECEIVE)
- ) {
- $result &= ~self::DIRECTION_RECEIVE;
- } else {
- throw new \Exception;
}
- } catch(\Exception $e) {
- if ($direction & self::DIRECTION_SEND) {
+ } catch(LockException $e) {
+ if ($direction & self::DIRECTION_SEND
+ && !($old & self::DIRECTION_SEND)
+ ) {
$this->shmHandler->unlock(self::DIRECTION_SEND);
}
- return false;
+ throw $e;
}
return $old;
}
@@ -227,8 +239,9 @@ public function lock($direction = self::DIRECTION_ALL, $replace = false)
*/
public function send($contents, $offset = null, $length = null)
{
- $previousState = $this->lock(self::DIRECTION_SEND);
- if ($this->persist && false === $previousState) {
+ if (false === ($previousState = $this->lock(self::DIRECTION_SEND))
+ && $this->persist
+ ) {
throw $this->createException(
'Unable to obtain sending lock', 10
);
@@ -251,8 +264,9 @@ public function send($contents, $offset = null, $length = null)
*/
public function receive($length, $what = 'data')
{
- $previousState = $this->lock(self::DIRECTION_RECEIVE);
- if ($this->persist && false === $previousState) {
+ if (false === ($previousState = $this->lock(self::DIRECTION_RECEIVE))
+ && $this->persist
+ ) {
throw $this->createException(
'Unable to obtain receiving lock', 9
);
@@ -279,8 +293,9 @@ public function receive($length, $what = 'data')
public function receiveStream(
$length, FilterCollection $filters = null, $what = 'stream data'
) {
- $previousState = $this->lock(self::DIRECTION_RECEIVE);
- if ($this->persist && false === $previousState) {
+ if (false === ($previousState = $this->lock(self::DIRECTION_RECEIVE))
+ && $this->persist
+ ) {
throw $this->createException(
'Unable to obtain receiving lock', 9
);
View
2 tests/ClientTest.php
@@ -140,6 +140,8 @@ public function testPersistentClientConnection()
);
$this->assertTrue($this->client->isFresh());
$this->assertTrue($client->isFresh());
+ $this->assertTrue($this->client->isPersistent());
+ $this->assertTrue($client->isPersistent());
$this->assertEquals('t', $this->client->receive(1));
$this->assertFalse($this->client->isFresh());
$this->assertFalse($client->isFresh());
View
2 tests/phpunit.xml
@@ -45,7 +45,7 @@
</testsuites>
<filter>
<whitelist>
- <directory suffix=".php">../src/PEAR2/Net/Transmitter</directory>
+ <directory suffix=".php">../src/PEAR2/Net/Transmitter/</directory>
</whitelist>
</filter>
</phpunit>
View
2 tests/secondaryPeer.xml
@@ -36,7 +36,7 @@
</testsuites>
<filter>
<whitelist>
- <directory suffix=".php">../src/PEAR2/Net/Transmitter</directory>
+ <directory suffix=".php">../src/PEAR2/Net/Transmitter/</directory>
</whitelist>
</filter>
</phpunit>

0 comments on commit ede8a11

Please sign in to comment.