Back to index

d-push  2.0
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
StateManager Class Reference

List of all members.

Public Member Functions

 StateManager ()
 Constructor.
 SetDevice (&$device)
 Sets an ASDevice for the Statemanager to work with.
 GetSynchedFolders ()
 Returns an array will all synchronized folderids.
 GetSynchedFolderState ($folderid)
 Returns a folder state (SyncParameters) for a folder id.
 SetSynchedFolderState ($spa)
 Saves a folder state - SyncParameters object.
 GetNewSyncKey ($synckey)
 Gets the new sync key for a specified sync key.
 GetSyncState ($synckey)
 Gets the state for a specified synckey (uuid + counter)
 SetSyncState ($synckey, $syncstate, $folderid=false)
 Writes the sync state to a new synckey.
 GetSyncFailState ()
 Gets the failsave sync state for the current synckey.
 SetSyncFailState ($syncstate)
 Writes the failsave sync state for the current (old) synckey.
 GetBackendStorage ($type=self::BACKENDSTORAGE_PERMANENT)
 Gets the backendstorage data.
 SetBackendStorage ($data, $type=self::BACKENDSTORAGE_PERMANENT)
 Writes the backendstorage data.
 InitializeFolderCache ($folders)
 Initializes the HierarchyCache for legacy syncs this is for AS 1.0 compatibility: save folder information synched with GetHierarchy() handled by StateManager.

Static Public Member Functions

static LinkState (&$device, $newUuid, $folderid=false)
 
static StateManager methods
static UnLinkState (&$device, $folderid, $removeFromDevice=true, $retrieveUUIDFromDevice=true)
 UnLinks all states from a folder id Old states are removed assisting the StateMachine to get rid of old data.
static ParseStateKey ($synckey)
 Parses a SyncKey and returns UUID and counter.
static BuildStateKey ($uuid, $counter)
 Builds a SyncKey from a UUID and counter.

Public Attributes

const FIXEDHIERARCHYCOUNTER = 99999
const BACKENDSTORAGE_PERMANENT = 1
const BACKENDSTORAGE_STATE = 2

Private Member Functions

 loadHierarchyCache ()
 
private StateManager methods
 saveHierarchyCache ($forceSaving=false)
 Saves the HierarchyCacheState of the HierarchyChache if this is an hierarchy operation.
 getNewUuid ()
 Generates a new UUID.

Private Attributes

 $statemachine
 $device
 $hierarchyOperation = false
 $deleteOldStates = false
 $foldertype
 $uuid
 $oldStateCounter
 $newStateCounter
 $synchedFolders

Detailed Description

Definition at line 59 of file statemanager.php.


Member Function Documentation

static StateManager::BuildStateKey ( uuid,
counter 
) [static]

Builds a SyncKey from a UUID and counter.

Parameters:
string$uuid
int$counterpublic
Returns:
string syncKey
Exceptions:
StateInvalidException

Definition at line 450 of file statemanager.php.

                                                          {
        if(!preg_match('/^([0-9A-Za-z-]+)$/', $uuid, $matches))
            throw new StateInvalidException(sprintf("UUID '%s' is invalid", $uuid));

        return "{" . $uuid . "}" . $counter;
    }

Here is the caller graph for this function:

StateManager::GetBackendStorage ( type = self::BACKENDSTORAGE_PERMANENT)

Gets the backendstorage data.

Parameters:
int$typepermanent or state related storage

public

Returns:
mixed
Exceptions:
StateNotYetAvailableException,StateNotFoundException

Definition at line 285 of file statemanager.php.

                                                                            {
        if ($type == self::BACKENDSTORAGE_STATE) {
            if (!$this->uuid)
                throw new StateNotYetAvailableException();

            return $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::BACKENDSTORAGE, $this->uuid, $this->oldStateCounter, $this->deleteOldStates);
        }
        else {
            return $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::BACKENDSTORAGE, false, $this->device->GetFirstSyncTime());
        }
    }

Gets the new sync key for a specified sync key.

The new sync state must be associated to this sync key when calling SetSyncState()

Parameters:
string$synckeypublic
Returns:
string

Definition at line 176 of file statemanager.php.

                                     {
        if(!isset($synckey) || $synckey == "0" || $synckey == false) {
            $this->uuid = $this->getNewUuid();
            $this->newStateCounter = 1;
        }
        else {
            list($uuid, $counter) = self::ParseStateKey($synckey);
            $this->uuid = $uuid;
            $this->newStateCounter = $counter + 1;
        }

        return self::BuildStateKey($this->uuid, $this->newStateCounter);
    }

Here is the call graph for this function:

StateManager::getNewUuid ( ) [private]

Generates a new UUID.

private

Returns:
string

Definition at line 522 of file statemanager.php.

                                  {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                    mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
                    mt_rand( 0, 0x0fff ) | 0x4000,
                    mt_rand( 0, 0x3fff ) | 0x8000,
                    mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
    }

Here is the caller graph for this function:

Gets the failsave sync state for the current synckey.

public

Returns:
array/boolean false if not available

Definition at line 249 of file statemanager.php.

                                       {
        if (!$this->uuid)
            return false;

        try {
            return $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::FAILSAVE, $this->uuid, $this->oldStateCounter, $this->deleteOldStates);
        }
        catch (StateNotFoundException $snfex) {
            return false;
        }
    }

Returns an array will all synchronized folderids.

public

Returns:
array

Definition at line 109 of file statemanager.php.

                                        {
        $synched = array();
        foreach ($this->device->GetAllFolderIds() as $folderid) {
            $uuid = $this->device->GetFolderUUID($folderid);
            if ($uuid)
                $synched[] = $folderid;
        }
        return $synched;
    }

Returns a folder state (SyncParameters) for a folder id.

Parameters:
$folderidpublic
Returns:
SyncParameters

Definition at line 127 of file statemanager.php.

                                                     {
        // new SyncParameters are cached
        if (isset($this->synchedFolders[$folderid]))
            return $this->synchedFolders[$folderid];

        $uuid = $this->device->GetFolderUUID($folderid);
        if ($uuid) {
            try {
                $data = $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::FOLDERDATA, $uuid);
                if ($data !== false) {
                    $this->synchedFolders[$folderid] = $data;
                }
            }
            catch (StateNotFoundException $ex) { }
        }

        if (!isset($this->synchedFolders[$folderid]))
            $this->synchedFolders[$folderid] = new SyncParameters();

        return $this->synchedFolders[$folderid];
    }

Gets the state for a specified synckey (uuid + counter)

Parameters:
string$synckeypublic
Returns:
string
Exceptions:
StateInvalidException,StateNotFoundException

Definition at line 199 of file statemanager.php.

                                           {
        // No sync state for sync key '0'
        if($synckey == "0") {
            $this->oldStateCounter = 0;
            return "";
        }

        // Check if synckey is allowed and set uuid and counter
        list($this->uuid, $this->oldStateCounter) = self::ParseStateKey($synckey);

        // make sure the hierarchy cache is in place
        if ($this->hierarchyOperation)
            $this->loadHierarchyCache();

        // the state machine will discard any sync states before this one, as they are no longer required
        return $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::DEFTYPE, $this->uuid, $this->oldStateCounter, $this->deleteOldStates);
    }

Here is the call graph for this function:

Initializes the HierarchyCache for legacy syncs this is for AS 1.0 compatibility: save folder information synched with GetHierarchy() handled by StateManager.

Parameters:
string$foldersArray with folder information

public

Returns:
boolean

Definition at line 331 of file statemanager.php.

                                                    {
        if (!is_array($folders))
            return false;

        if (!isset($this->device))
            throw new FatalException("ASDevice not initialized");

        // redeclare this operation as hierarchyOperation
        $this->hierarchyOperation = true;

        // as there is no hierarchy uuid, we have to create one
        $this->uuid = $this->getNewUuid();
        $this->newStateCounter = self::FIXEDHIERARCHYCOUNTER;

        // initialize legacy HierarchCache
        $this->device->SetHierarchyCache($folders);

        // force saving the hierarchy cache!
        return $this->saveHierarchyCache(true);
    }

Here is the call graph for this function:

static StateManager::LinkState ( &$  device,
newUuid,
folderid = false 
) [static]


static StateManager methods

Links a folderid to the a UUID Old states are removed if an folderid is linked to a new UUID assisting the StateMachine to get rid of old data.

Parameters:
ASDevice$device
string$uuidthe uuid to link to
string$folderid(opt) if not set, hierarchy state is linked

public

Returns:
boolean

Definition at line 369 of file statemanager.php.

                                                                            {
        $savedUuid = $device->GetFolderUUID($folderid);
        // delete 'old' states!
        if ($savedUuid != $newUuid) {
            // remove states but no need to notify device
            self::UnLinkState($device, $folderid, false);

            ZLog::Write(LOGLEVEL_DEBUG, sprintf("StateManager::linkState(#ASDevice, '%s','%s'): linked to uuid '%s'.", $newUuid, (($folderid === false)?'HierarchyCache':$folderid), $newUuid));
            return $device->SetFolderUUID($newUuid, $folderid);
        }
        return true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:


private StateManager methods

Loads the HierarchyCacheState and initializes the HierarchyChache if this is an hierarchy operation

private

Returns:
boolean
Exceptions:
StateNotFoundException

Definition at line 470 of file statemanager.php.

                                          {
        if (!$this->hierarchyOperation)
            return false;

        ZLog::Write(LOGLEVEL_DEBUG, sprintf("StateManager->loadHierarchyCache(): '%s-%s-%s-%d'", $this->device->GetDeviceId(), $this->uuid, IStateMachine::HIERARCHY, $this->oldStateCounter));

        // check if a full hierarchy sync might be necessary
        if ($this->device->GetFolderUUID(false) === false) {
            self::UnLinkState($this->device, false, false, $this->uuid);
            throw new StateNotFoundException("No hierarchy UUID linked to device. Requesting folder resync.");
        }

        $hierarchydata = $this->statemachine->GetState($this->device->GetDeviceId(), IStateMachine::HIERARCHY, $this->uuid , $this->oldStateCounter, $this->deleteOldStates);
        $this->device->SetHierarchyCache($hierarchydata);
        return true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static StateManager::ParseStateKey ( synckey) [static]

Parses a SyncKey and returns UUID and counter.

Parameters:
string$synckeypublic
Returns:
array uuid, counter
Exceptions:
StateInvalidException

Definition at line 432 of file statemanager.php.

                                                   {
        $matches = array();
        if(!preg_match('/^\{([0-9A-Za-z-]+)\}([0-9]+)$/', $synckey, $matches))
            throw new StateInvalidException(sprintf("SyncKey '%s' is invalid", $synckey));

        return array($matches[1], (int)$matches[2]);
    }

Here is the caller graph for this function:

StateManager::saveHierarchyCache ( forceSaving = false) [private]

Saves the HierarchyCacheState of the HierarchyChache if this is an hierarchy operation.

Parameters:
boolean$forceLoadindicates if the cache should be saved also if not a hierary operation

private

Returns:
boolean
Exceptions:
StateInvalidException

Definition at line 497 of file statemanager.php.

                                                              {
        if (!$this->hierarchyOperation && !$forceSaving)
            return false;

        // link the hierarchy cache again, if the UUID does not match the UUID saved in the devicedata
        if (($this->uuid != $this->device->GetFolderUUID() || $forceSaving) )
            self::LinkState($this->device, $this->uuid);

        // check all folders and deleted folders to update data of ASDevice and delete old states
        $hc = $this->device->getHierarchyCache();
        foreach ($hc->GetDeletedFolders() as $delfolder)
            self::UnLinkState($this->device, $delfolder->serverid);

        foreach ($hc->ExportFolders() as $folder)
            $this->device->SetFolderType($folder->serverid, $folder->type);

        return $this->statemachine->SetState($this->device->GetHierarchyCacheData(), $this->device->GetDeviceId(), IStateMachine::HIERARCHY, $this->uuid, $this->newStateCounter);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

StateManager::SetBackendStorage ( data,
type = self::BACKENDSTORAGE_PERMANENT 
)

Writes the backendstorage data.

Parameters:
mixed$data
int$typepermanent or state related storage

public

Returns:
int amount of bytes saved
Exceptions:
StateNotYetAvailableException,StateNotFoundException

Definition at line 307 of file statemanager.php.

                                                                                   {
        if ($type == self::BACKENDSTORAGE_STATE) {
        if (!$this->uuid)
            throw new StateNotYetAvailableException();

            // TODO serialization should be done in the StateMachine
            return $this->statemachine->SetState($data, $this->device->GetDeviceId(), IStateMachine::BACKENDSTORAGE, $this->uuid, $this->newStateCounter);
        }
        else {
            return $this->statemachine->SetState($data, $this->device->GetDeviceId(), IStateMachine::BACKENDSTORAGE, false, $this->device->GetFirstSyncTime());
        }
    }
StateManager::SetDevice ( &$  device)

Sets an ASDevice for the Statemanager to work with.

Parameters:
ASDevice$devicepublic
Returns:
boolean

Definition at line 98 of file statemanager.php.

                                        {
        $this->device = $device;
        return true;
    }

Writes the failsave sync state for the current (old) synckey.

Parameters:
mixed$syncstatepublic
Returns:
boolean

Definition at line 269 of file statemanager.php.

                                                 {
        if ($this->oldStateCounter == 0)
            return false;

        return $this->statemachine->SetState($syncstate, $this->device->GetDeviceId(), IStateMachine::FAILSAVE, $this->uuid, $this->oldStateCounter);
    }

Saves a folder state - SyncParameters object.

Parameters:
SyncParamerters$spapublic
Returns:
boolean

Definition at line 157 of file statemanager.php.

                                                {
        // make sure the current uuid is linked on the device for the folder.
        // if not, old states will be automatically removed and the new ones linked
        self::LinkState($this->device, $spa->GetUuid(), $spa->GetFolderId());

        $spa->SetReferencePolicyKey($this->device->GetPolicyKey());

        return $this->statemachine->SetState($spa, $this->device->GetDeviceId(), IStateMachine::FOLDERDATA, $spa->GetUuid());
    }

Here is the call graph for this function:

StateManager::SetSyncState ( synckey,
syncstate,
folderid = false 
)

Writes the sync state to a new synckey.

Parameters:
string$synckey
string$syncstate
string$folderid(opt) the synckey is associated with the folder - should always be set when performing CONTENT operations

public

Returns:
boolean
Exceptions:
StateInvalidException

Definition at line 228 of file statemanager.php.

                                                                          {
        $internalkey = self::BuildStateKey($this->uuid, $this->newStateCounter);
        if ($this->oldStateCounter != 0 && $synckey != $internalkey)
            throw new StateInvalidException(sprintf("Unexpected synckey value oldcounter: '%s' synckey: '%s' internal key: '%s'", $this->oldStateCounter, $synckey, $internalkey));

        // make sure the hierarchy cache is also saved
        if ($this->hierarchyOperation)
            $this->saveHierarchyCache();

        // announce this uuid to the device, while old uuid/states should be deleted
        self::LinkState($this->device, $this->uuid, $folderid);

        return $this->statemachine->SetState($syncstate, $this->device->GetDeviceId(), IStateMachine::DEFTYPE, $this->uuid, $this->newStateCounter);
    }

Here is the call graph for this function:

Constructor.

public

Definition at line 83 of file statemanager.php.

                                   {
        $this->statemachine = ZPush::GetStateMachine();
        $this->hierarchyOperation = ZPush::HierarchyCommand(Request::GetCommandCode());
        $this->deleteOldStates = (Request::GetCommandCode() === ZPush::COMMAND_SYNC || $this->hierarchyOperation);
        $this->synchedFolders = array();
    }

Here is the call graph for this function:

static StateManager::UnLinkState ( &$  device,
folderid,
removeFromDevice = true,
retrieveUUIDFromDevice = true 
) [static]

UnLinks all states from a folder id Old states are removed assisting the StateMachine to get rid of old data.

The UUID is then removed from the device

Parameters:
ASDevice$device
string$folderid
boolean$removeFromDeviceindicates if the device should be notified that the state was removed
boolean$retrieveUUIDFromDeviceindicates if the UUID should be retrieved from device. If not true this parameter will be used as UUID.

public

Returns:
boolean

Definition at line 397 of file statemanager.php.

                                                                                                                      {
        if ($retrieveUUIDFromDevice === true)
            $savedUuid = $device->GetFolderUUID($folderid);
        else
            $savedUuid = $retrieveUUIDFromDevice;

        if ($savedUuid) {
            ZLog::Write(LOGLEVEL_DEBUG, sprintf("StateManager::UnLinkState('%s'): saved state '%s' will be deleted.", $folderid, $savedUuid));
            ZPush::GetStateMachine()->CleanStates($device->GetDeviceId(), IStateMachine::DEFTYPE, $savedUuid, self::FIXEDHIERARCHYCOUNTER *2);
            ZPush::GetStateMachine()->CleanStates($device->GetDeviceId(), IStateMachine::FOLDERDATA, $savedUuid); // CPO
            ZPush::GetStateMachine()->CleanStates($device->GetDeviceId(), IStateMachine::FAILSAVE, $savedUuid, self::FIXEDHIERARCHYCOUNTER *2);
            ZPush::GetStateMachine()->CleanStates($device->GetDeviceId(), IStateMachine::BACKENDSTORAGE, $savedUuid, self::FIXEDHIERARCHYCOUNTER *2);

            // remove all messages which could not be synched before
            $device->RemoveIgnoredMessage($folderid, false);

            if ($folderid === false && $savedUuid !== false)
                ZPush::GetStateMachine()->CleanStates($device->GetDeviceId(), IStateMachine::HIERARCHY, $savedUuid, self::FIXEDHIERARCHYCOUNTER *2);
        }
        // delete this id from the uuid cache
        if ($removeFromDevice)
            return $device->SetFolderUUID(false, $folderid);
        else
            return true;
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

StateManager::$deleteOldStates = false [private]

Definition at line 69 of file statemanager.php.

Definition at line 67 of file statemanager.php.

Definition at line 71 of file statemanager.php.

Definition at line 68 of file statemanager.php.

Definition at line 74 of file statemanager.php.

Definition at line 73 of file statemanager.php.

Definition at line 66 of file statemanager.php.

Definition at line 75 of file statemanager.php.

Definition at line 72 of file statemanager.php.

Definition at line 63 of file statemanager.php.

Definition at line 64 of file statemanager.php.

Definition at line 60 of file statemanager.php.


The documentation for this class was generated from the following file: