| 1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "DataModel/LockableItem.h" |
| 22 |
#include "DataModel/Object.h" |
| 23 |
#include "Utils/Utils.h" |
| 24 |
|
| 25 |
using std::map; |
| 26 |
using std::stringstream; |
| 27 |
using std::string; |
| 28 |
|
| 29 |
namespace DataModel |
| 30 |
{ |
| 31 |
string LockableItem::Serialize() const |
| 32 |
{ |
| 33 |
return "lockstate=" + std::to_string(lockState) |
| 34 |
+ ";locobaseid=" + std::to_string(locoBaseIdentifier.GetObjectID()) |
| 35 |
+ ";locobasetype=" + std::to_string(locoBaseIdentifier.GetObjectType()); |
| 36 |
} |
| 37 |
|
| 38 |
bool LockableItem::Deserialize(const map<string, string> arguments) |
| 39 |
{ |
| 40 |
const LocoID locoID = Utils::Utils::GetIntegerMapEntry(arguments, "locoID", LocoNone); // FIXME: Remove later: 2024-02-08 |
| 41 |
locoBaseIdentifier.SetObjectID(Utils::Utils::GetIntegerMapEntry(arguments, "locobaseid", locoID)); |
| 42 |
locoBaseIdentifier.SetObjectType(static_cast<ObjectType>(Utils::Utils::GetIntegerMapEntry(arguments, "locobasetype", ObjectTypeLoco))); |
| 43 |
lockState = static_cast<LockState>(Utils::Utils::GetIntegerMapEntry(arguments, "lockState", LockStateFree)); // FIXME: Remove later: 2024-02-08 |
| 44 |
lockState = static_cast<LockState>(Utils::Utils::GetIntegerMapEntry(arguments, "lockstate", lockState)); |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
bool LockableItem::Reserve(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 49 |
{ |
| 50 |
std::lock_guard<std::mutex> Guard(lockMutex); |
| 51 |
if (this->locoBaseIdentifier == locoBaseIdentifier) |
| 52 |
{ |
| 53 |
if (lockState == LockStateFree) |
| 54 |
{ |
| 55 |
lockState = LockStateReserved; |
| 56 |
} |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
if (this->locoBaseIdentifier.IsSet() || (lockState != LockStateFree)) |
| 61 |
{ |
| 62 |
Object *object = dynamic_cast<Object*>(this); |
| 63 |
if (!object) |
| 64 |
{ |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
logger->Debug(Languages::TextIsNotFree, object->GetName()); |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
lockState = LockStateReserved; |
| 73 |
this->locoBaseIdentifier = locoBaseIdentifier; |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
bool LockableItem::Lock(Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 78 |
{ |
| 79 |
std::lock_guard<std::mutex> Guard(lockMutex); |
| 80 |
|
| 81 |
if (this->locoBaseIdentifier != locoBaseIdentifier) |
| 82 |
{ |
| 83 |
Object *object = dynamic_cast<Object*>(this); |
| 84 |
if (object == nullptr) |
| 85 |
{ |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
logger->Debug(Languages::TextIsNotFree, object->GetName()); |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if (lockState != LockStateReserved && lockState != LockStateHardLocked) |
| 94 |
{ |
| 95 |
Object *object = dynamic_cast<Object*>(this); |
| 96 |
if (object == nullptr) |
| 97 |
{ |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
logger->Debug(Languages::TextIsNotFree, object->GetName()); |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
lockState = LockStateHardLocked; |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
bool LockableItem::Release(__attribute__((unused)) Logger::Logger* logger, const ObjectIdentifier& locoBaseIdentifier) |
| 110 |
{ |
| 111 |
std::lock_guard<std::mutex> Guard(lockMutex); |
| 112 |
if (this->locoBaseIdentifier != locoBaseIdentifier && locoBaseIdentifier.IsSet()) |
| 113 |
{ |
| 114 |
return false; |
| 115 |
} |
| 116 |
this->locoBaseIdentifier.Clear(); |
| 117 |
lockState = LockStateFree; |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
} // namespace DataModel |
| 122 |
|