Commit c67826b8 authored by skummer's avatar skummer

ZP-425 #comment adding GetAllStatesForDevice() and returning boolean for...

ZP-425 #comment adding GetAllStatesForDevice() and returning boolean for Un/LinkUserDevice() - implementing these changes to the FileStateMachine #time 3h

git-svn-id: https://z-push.org/svn/z-push/trunk@1699 b7dd7b3b-3a3c-0410-9da9-bee62a6cc5b5
parent 3d576c16
...@@ -205,11 +205,12 @@ class FileStateMachine implements IStateMachine { ...@@ -205,11 +205,12 @@ class FileStateMachine implements IStateMachine {
* @param string $devid * @param string $devid
* *
* @access public * @access public
* @return array * @return boolean indicating if the user was added or not (existed already)
*/ */
public function LinkUserDevice($username, $devid) { public function LinkUserDevice($username, $devid) {
include_once("simplemutex.php"); include_once("simplemutex.php");
$mutex = new SimpleMutex(); $mutex = new SimpleMutex();
$changed = false;
// exclusive block // exclusive block
if ($mutex->Block()) { if ($mutex->Block()) {
...@@ -220,8 +221,6 @@ class FileStateMachine implements IStateMachine { ...@@ -220,8 +221,6 @@ class FileStateMachine implements IStateMachine {
else else
$users = array(); $users = array();
$changed = false;
// add user/device to the list // add user/device to the list
if (!isset($users[$username])) { if (!isset($users[$username])) {
$users[$username] = array(); $users[$username] = array();
...@@ -241,6 +240,7 @@ class FileStateMachine implements IStateMachine { ...@@ -241,6 +240,7 @@ class FileStateMachine implements IStateMachine {
$mutex->Release(); $mutex->Release();
} }
return $changed;
} }
/** /**
...@@ -250,11 +250,12 @@ class FileStateMachine implements IStateMachine { ...@@ -250,11 +250,12 @@ class FileStateMachine implements IStateMachine {
* @param string $devid * @param string $devid
* *
* @access public * @access public
* @return array * @return boolean
*/ */
public function UnLinkUserDevice($username, $devid) { public function UnLinkUserDevice($username, $devid) {
include_once("simplemutex.php"); include_once("simplemutex.php");
$mutex = new SimpleMutex(); $mutex = new SimpleMutex();
$changed = false;
// exclusive block // exclusive block
if ($mutex->Block()) { if ($mutex->Block()) {
...@@ -265,8 +266,6 @@ class FileStateMachine implements IStateMachine { ...@@ -265,8 +266,6 @@ class FileStateMachine implements IStateMachine {
else else
$users = array(); $users = array();
$changed = false;
// is this user listed at all? // is this user listed at all?
if (isset($users[$username])) { if (isset($users[$username])) {
if (isset($users[$username][$devid])) { if (isset($users[$username][$devid])) {
...@@ -290,6 +289,7 @@ class FileStateMachine implements IStateMachine { ...@@ -290,6 +289,7 @@ class FileStateMachine implements IStateMachine {
$mutex->Release(); $mutex->Release();
} }
return $changed;
} }
/** /**
...@@ -367,6 +367,54 @@ class FileStateMachine implements IStateMachine { ...@@ -367,6 +367,54 @@ class FileStateMachine implements IStateMachine {
return $status; return $status;
} }
/**
* Returns all available states for a device id
*
* @param string $devid the device id
*
* @access public
* @return array(mixed)
*/
public function GetAllStatesForDevice($devid) {
$out = array();
$devdir = $this->getDirectoryForDevice($devid) . "/$devid-";
foreach (glob($devdir . "*", GLOB_NOSORT) as $devdata) {
// cut the device dir away and split into parts
$parts = explode("-", substr($devdata, strlen($devdir)));
$state = array('type' => false, 'counter' => false, 'uuid' => false);
if (isset($parts[0]) && $parts[0] == IStateMachine::DEVICEDATA)
$state['type'] = IStateMachine::DEVICEDATA;
if (isset($parts[0]) && strlen($parts[0]) == 8 &&
isset($parts[1]) && strlen($parts[1]) == 4 &&
isset($parts[2]) && strlen($parts[2]) == 4 &&
isset($parts[3]) && strlen($parts[3]) == 4 &&
isset($parts[4]) && strlen($parts[4]) == 12)
$state['uuid'] = $parts[0]."-".$parts[1]."-".$parts[2]."-".$parts[3]."-".$parts[4];
if (isset($parts[5]) && is_numeric($parts[5])) {
$state['counter'] = $parts[5];
$state['type'] = ""; // default
}
if (isset($parts[5])) {
if (is_int($parts[5]))
$state['counter'] = $parts[5];
else if (in_array($parts[5], array(IStateMachine::FOLDERDATA, IStateMachine::FAILSAVE, IStateMachine::HIERARCHY, IStateMachine::BACKENDSTORAGE)))
$state['type'] = $parts[5];
}
if (isset($parts[6]) && is_numeric($parts[6]))
$state['counter'] = $parts[6];
$out[] = $state;
}
return $out;
}
/**---------------------------------------------------------------------------------------------------------- /**----------------------------------------------------------------------------------------------------------
* Private FileStateMachine stuff * Private FileStateMachine stuff
......
...@@ -141,7 +141,7 @@ interface IStateMachine { ...@@ -141,7 +141,7 @@ interface IStateMachine {
* @param string $devid * @param string $devid
* *
* @access public * @access public
* @return array * @return boolean indicating if the user was added or not (existed already)
*/ */
public function LinkUserDevice($username, $devid); public function LinkUserDevice($username, $devid);
...@@ -152,7 +152,7 @@ interface IStateMachine { ...@@ -152,7 +152,7 @@ interface IStateMachine {
* @param string $devid * @param string $devid
* *
* @access public * @access public
* @return array * @return boolean
*/ */
public function UnLinkUserDevice($username, $devid); public function UnLinkUserDevice($username, $devid);
...@@ -184,6 +184,16 @@ interface IStateMachine { ...@@ -184,6 +184,16 @@ interface IStateMachine {
* @return boolean * @return boolean
*/ */
public function SetStateVersion($version); public function SetStateVersion($version);
/**
* Returns all available states for a device id
*
* @param string $devid the device id
*
* @access public
* @return array(mixed)
*/
public function GetAllStatesForDevice($devid);
} }
?> ?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment