|
| 1 | +# Quick Start: Queue-Based Architecture |
| 2 | + |
| 3 | +## 🎯 What Was Done |
| 4 | + |
| 5 | +Implemented a **queue-based architecture** to eliminate the race condition in keycard communication. |
| 6 | + |
| 7 | +**Status:** ✅ **Core infrastructure complete** (Phase 2) |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 📁 What Files Were Created |
| 12 | + |
| 13 | +``` |
| 14 | +keycard-qt-status/ |
| 15 | +├── include/keycard-qt/ |
| 16 | +│ ├── card_command.h ← Command Pattern interface |
| 17 | +│ └── communication_manager.h ← Queue dispatcher |
| 18 | +├── src/ |
| 19 | +│ ├── card_command.cpp ← Command implementations |
| 20 | +│ └── communication_manager.cpp ← Manager implementation |
| 21 | +└── Documentation: |
| 22 | + ├── QUEUE_ARCHITECTURE_README.md ← Start here! |
| 23 | + ├── ARCHITECTURE_CHANGES.md ← Deep dive |
| 24 | + ├── INTEGRATION_GUIDE.md ← How to integrate |
| 25 | + ├── IMPLEMENTATION_STATUS.md ← Status tracking |
| 26 | + └── QUICK_START.md ← This file |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 🚀 How to Use (Quick Example) |
| 32 | + |
| 33 | +### 1. Create and Start Manager |
| 34 | + |
| 35 | +```cpp |
| 36 | +#include "keycard-qt/communication_manager.h" |
| 37 | + |
| 38 | +// Create |
| 39 | +auto manager = new Keycard::CommunicationManager(this); |
| 40 | + |
| 41 | +// Connect signals |
| 42 | +connect(manager, &Keycard::CommunicationManager::cardInitialized, |
| 43 | + this, &MyClass::onCardInit); |
| 44 | + |
| 45 | +// Start |
| 46 | +manager->start(channel, pairingStorage, passwordProvider); |
| 47 | +``` |
| 48 | +
|
| 49 | +### 2. Execute Commands (Async) |
| 50 | +
|
| 51 | +```cpp |
| 52 | +// Create command |
| 53 | +auto cmd = std::make_unique<Keycard::VerifyPINCommand>("123456"); |
| 54 | +
|
| 55 | +// Enqueue |
| 56 | +QUuid token = manager->enqueueCommand(std::move(cmd)); |
| 57 | +
|
| 58 | +// Wait for result signal |
| 59 | +connect(manager, &Keycard::CommunicationManager::commandCompleted, |
| 60 | + [](QUuid token, Keycard::CommandResult result) { |
| 61 | + if (result.success) { |
| 62 | + qDebug() << "Success:" << result.data; |
| 63 | + } else { |
| 64 | + qDebug() << "Error:" << result.error; |
| 65 | + } |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. Execute Commands (Sync) |
| 70 | + |
| 71 | +```cpp |
| 72 | +// Create and execute |
| 73 | +auto cmd = std::make_unique<Keycard::GetStatusCommand>(); |
| 74 | +Keycard::CommandResult result = manager->executeCommandSync(std::move(cmd)); |
| 75 | + |
| 76 | +if (result.success) { |
| 77 | + QVariantMap status = result.data.toMap(); |
| 78 | + int pinRetries = status["pinRetryCount"].toInt(); |
| 79 | +} |
| 80 | +``` |
| 81 | +
|
| 82 | +--- |
| 83 | +
|
| 84 | +## 🏗️ Architecture Overview |
| 85 | +
|
| 86 | +### The Problem (Before) |
| 87 | +``` |
| 88 | +Card Detected → SessionManager & CommandSet both try to initialize |
| 89 | + ↓ |
| 90 | + RACE CONDITION! |
| 91 | + ↓ |
| 92 | + Random failures |
| 93 | +``` |
| 94 | +
|
| 95 | +### The Solution (After) |
| 96 | +``` |
| 97 | +Card Detected |
| 98 | + ↓ |
| 99 | +CommunicationManager |
| 100 | + ↓ |
| 101 | +[Initialize Atomically] |
| 102 | + ↓ |
| 103 | +[Queue Commands] |
| 104 | + ↓ |
| 105 | +[Execute Serially] |
| 106 | + ↓ |
| 107 | +No races, predictable! |
| 108 | +``` |
| 109 | +
|
| 110 | +--- |
| 111 | +
|
| 112 | +## 📚 Where to Read Next |
| 113 | +
|
| 114 | +1. **Want overview?** → Read `QUEUE_ARCHITECTURE_README.md` |
| 115 | +2. **Want details?** → Read `ARCHITECTURE_CHANGES.md` |
| 116 | +3. **Want to integrate?** → Read `INTEGRATION_GUIDE.md` |
| 117 | +4. **Want status?** → Read `IMPLEMENTATION_STATUS.md` |
| 118 | +
|
| 119 | +--- |
| 120 | +
|
| 121 | +## ✅ What's Complete |
| 122 | +
|
| 123 | +- [x] CardCommand interface (Command Pattern) |
| 124 | +- [x] CommunicationManager (queue dispatcher) |
| 125 | +- [x] Communication thread (dedicated for card I/O) |
| 126 | +- [x] Atomic initialization sequence |
| 127 | +- [x] Both async and sync APIs |
| 128 | +- [x] Build system updated |
| 129 | +- [x] Comprehensive documentation |
| 130 | +
|
| 131 | +**Phase 2:** ✅ **COMPLETE** |
| 132 | +
|
| 133 | +--- |
| 134 | +
|
| 135 | +## 🔄 What's Next |
| 136 | +
|
| 137 | +### Phase 3: Integration (Pending) |
| 138 | +- [ ] Modify SessionManager to use CommunicationManager |
| 139 | +- [ ] Add feature flag to switch architectures |
| 140 | +- [ ] Keep backward compatibility |
| 141 | +
|
| 142 | +### Phase 4: Testing (Pending) |
| 143 | +- [ ] Unit tests |
| 144 | +- [ ] Integration tests |
| 145 | +- [ ] Stress tests |
| 146 | +
|
| 147 | +### Phase 5: Rollout (Pending) |
| 148 | +- [ ] Canary testing |
| 149 | +- [ ] Gradual rollout |
| 150 | +- [ ] Monitoring |
| 151 | +
|
| 152 | +--- |
| 153 | +
|
| 154 | +## 🔨 How to Build |
| 155 | +
|
| 156 | +```bash |
| 157 | +cd keycard-qt-status |
| 158 | +mkdir build && cd build |
| 159 | +cmake .. |
| 160 | +make |
| 161 | +``` |
| 162 | + |
| 163 | +Should compile without errors! |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## 💡 Key Concepts |
| 168 | + |
| 169 | +### CardCommand |
| 170 | +- Encapsulates one operation |
| 171 | +- Implements `execute(CommandSet*)` |
| 172 | +- Returns CommandResult |
| 173 | + |
| 174 | +### CommunicationManager |
| 175 | +- Manages command queue |
| 176 | +- Runs dedicated communication thread |
| 177 | +- Handles card initialization |
| 178 | +- Provides async & sync APIs |
| 179 | + |
| 180 | +### State Machine |
| 181 | +``` |
| 182 | +Idle → Initializing → Ready → Processing → Ready |
| 183 | + ↑ ↓ |
| 184 | + └──────────────────────────────────────────┘ |
| 185 | +``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## 📊 Impact |
| 190 | + |
| 191 | +### Before |
| 192 | +- ❌ Race conditions |
| 193 | +- ❌ Unpredictable failures |
| 194 | +- ❌ Corrupted state |
| 195 | + |
| 196 | +### After |
| 197 | +- ✅ No races (atomic init) |
| 198 | +- ✅ Predictable (serial execution) |
| 199 | +- ✅ Clean state management |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +## 🎓 Learn By Example |
| 204 | + |
| 205 | +See usage examples in: |
| 206 | +- `ARCHITECTURE_CHANGES.md` - Multiple examples |
| 207 | +- `INTEGRATION_GUIDE.md` - SessionManager integration |
| 208 | +- Source code comments - Inline examples |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## 🤝 How to Contribute |
| 213 | + |
| 214 | +### Adding New Commands |
| 215 | + |
| 216 | +1. **Declare in `card_command.h`:** |
| 217 | +```cpp |
| 218 | +class MyCommand : public CardCommand { |
| 219 | +public: |
| 220 | + MyCommand(params...); |
| 221 | + CommandResult execute(CommandSet*) override; |
| 222 | + QString name() const override { return "MY_COMMAND"; } |
| 223 | +}; |
| 224 | +``` |
| 225 | +
|
| 226 | +2. **Implement in `card_command.cpp`:** |
| 227 | +```cpp |
| 228 | +CommandResult MyCommand::execute(CommandSet* cmdSet) { |
| 229 | + // Your logic here |
| 230 | + bool ok = cmdSet->someOperation(); |
| 231 | + return ok ? CommandResult::fromSuccess(data) |
| 232 | + : CommandResult::fromError(error); |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +3. **Use it:** |
| 237 | +```cpp |
| 238 | +auto cmd = std::make_unique<MyCommand>(args); |
| 239 | +manager->enqueueCommand(std::move(cmd)); |
| 240 | +``` |
| 241 | +
|
| 242 | +--- |
| 243 | +
|
| 244 | +## 🐛 Troubleshooting |
| 245 | +
|
| 246 | +### Build Errors |
| 247 | +- Check CMakeLists.txt includes new files |
| 248 | +- Verify Qt6 Core is available |
| 249 | +- Check include paths |
| 250 | +
|
| 251 | +### Runtime Issues |
| 252 | +- Enable logging: `QLoggingCategory::setFilterRules("keycard.*=true")` |
| 253 | +- Check state transitions in logs |
| 254 | +- Verify card detection events |
| 255 | +
|
| 256 | +### Integration Issues |
| 257 | +- See `INTEGRATION_GUIDE.md` for step-by-step |
| 258 | +- Use feature flag for gradual rollout |
| 259 | +- Keep old code path for fallback |
| 260 | +
|
| 261 | +--- |
| 262 | +
|
| 263 | +## 📞 Need Help? |
| 264 | +
|
| 265 | +1. Check documentation (4 comprehensive files) |
| 266 | +2. Review source code comments |
| 267 | +3. Compare with Go implementation |
| 268 | +4. Ask the team! |
| 269 | +
|
| 270 | +--- |
| 271 | +
|
| 272 | +## 🎉 Summary |
| 273 | +
|
| 274 | +**What:** Queue-based architecture for keycard communication |
| 275 | +**Why:** Eliminate race condition between SessionManager and CommandSet |
| 276 | +**How:** Single communication thread + command queue + atomic init |
| 277 | +**Status:** Core infrastructure complete, ready for integration |
| 278 | +**Next:** Integrate with SessionManager and test thoroughly |
| 279 | +
|
| 280 | +**Result:** No more races, predictable behavior, maintainable code! |
| 281 | +
|
| 282 | +--- |
| 283 | +
|
| 284 | +**Last Updated:** December 16, 2024 |
| 285 | +**Phase:** 2 Complete, 3 Pending |
| 286 | +**Status:** ✅ Ready for Integration |
0 commit comments