AJAX_Locking
[ class tree: AJAX_Locking ] [ index: AJAX_Locking ] [ all elements ]

Source for file SharedMemory.php

Documentation is available at SharedMemory.php

  1. <?php
  2. require_once('Driver.php');
  3. require_once('System/SharedMemory.php');
  4.  
  5. /**
  6. * A driver that use System_SharedMemory as data store
  7. */
  8. class AJAX_Locking_Driver_SharedMemory extends AJAX_Locking_Driver
  9. {
  10. var $shared;
  11.  
  12. /**
  13. * Constructor
  14. *
  15. * @return AJAX_Locking_Driver
  16. */
  17. function AJAX_Locking_Driver_SharedMemory($type = false, $options = false, $timeout = false)
  18. {
  19. parent::AJAX_Locking_Driver($timeout);
  20.  
  21. $this->shared = &System_SharedMemory::factory($type, $options);
  22. }
  23.  
  24. /**
  25. * Locks an object
  26. *
  27. * @param mixed $user id of the user who wants to lock
  28. * @param string $type type/classname of the object
  29. * @param mixed $id id of the object
  30. * @return boolean true if the unlock was successfull, false otherwise
  31. */
  32. function lock($user, $type, $id)
  33. {
  34. $key = $this->_getKey($type, $id);
  35. $value = $this->shared->get($key);
  36. if (empty($value)) {
  37. // object is unlocked: lock it
  38. $value = $this->_getValue($user, $type, $id);
  39. $this->shared->set($key, $value);
  40. return true;
  41. } else {
  42. // object is locked: check if lock expired
  43. list($owner, $type, $id, $time) = $this->_parseValue($value);
  44. if (time() - $time > $this->timeout) {
  45. // lock expired: remove it and lock
  46. // $this->shared->rm($key);
  47. $this->shared->set($key, $value);
  48. return true;
  49. } else {
  50. // object properly locked by (another?) user
  51. return ($owner == $user);
  52. }
  53. }
  54.  
  55. }
  56.  
  57. /**
  58. * Unlocks an object
  59. *
  60. * @param mixed $user id of the user who wants to unlock
  61. * @param string $type type/classname of the object
  62. * @param mixed $id id of the object
  63. * @return boolean true if the unlock was successfull, false otherwise
  64. */
  65. function unlock($user, $type, $id)
  66. {
  67. $key = $this->_getKey($type, $id);
  68. $value = $this->shared->get($key);
  69. if (empty($value)) {
  70. // object was not locked
  71. return false;
  72. } else {
  73. // check if user is owner of the lock
  74. list($owner, $type, $id, $time) = $this->_parseValue($value);
  75. if ($user == $owner) {
  76. // unlock it
  77. $this->shared->rm($key);
  78. return true;
  79. } else {
  80. // user cannot unlock it
  81. return false;
  82. }
  83. }
  84. }
  85.  
  86. /**
  87. * Returns the status of the object (lock or unlocked)
  88. *
  89. * @param mixed $user id of the user who wants to know the object's status
  90. * @param string $type type/classname of the object
  91. * @param mixed $id id of the object
  92. * @return string the status and the owner of the object
  93. */
  94. function status($user, $type, $id)
  95. {
  96. $key = $this->_getKey($type, $id);
  97. $value = $this->shared->get($key);
  98. if (empty($value)) {
  99. // object is unlocked
  100. return AJAX_LOCKING_UNLOCKED . '~' . 'noboby';
  101. } else {
  102. // object is locked:
  103. // 1. check if current user is owner of the lock
  104. // 2. check if lock expired
  105. list($owner, $type, $id, $time) = $this->_parseValue($value);
  106. $timeout = (time() - $time > $this->timeout);
  107. if ($owner == $user) {
  108. if ($timeout) {
  109. // lock expired: remove it and inform it's ower
  110. $this->shared->rm($key);
  111. return AJAX_LOCKING_TIMEOUT . '~' . $owner;
  112. } else {
  113. return AJAX_LOCKING_OWNED . '~' . $owner;
  114. }
  115. } else {
  116. if ($timeout) {
  117. // lock expired: remove it
  118. $this->shared->rm($key);
  119. return AJAX_LOCKING_UNLOCKED . '~' . 'nobody';
  120. } else {
  121. return AJAX_LOCKING_LOCKED . '~' . $owner;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. ?>

Documentation generated on Tue, 13 Feb 2007 21:42:45 +0100 by phpDocumentor 1.3.0RC3