Upload Source/Plugin/InstanceRegistry.h
Browse files
Source/Plugin/InstanceRegistry.h
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <string>
|
| 4 |
+
#include <map>
|
| 5 |
+
#include <fstream>
|
| 6 |
+
#include <cstdio>
|
| 7 |
+
#include "json.hpp"
|
| 8 |
+
#include "Constants.h"
|
| 9 |
+
|
| 10 |
+
// Header-only instance registry.
|
| 11 |
+
// Stores {channelName: port} in a JSON file at /tmp/pluginbridge-registry.json
|
| 12 |
+
// Multiple PluginBridge instances write to this file (one per track).
|
| 13 |
+
// Atomic writes via write-to-temp + rename (POSIX rename is atomic on same filesystem).
|
| 14 |
+
|
| 15 |
+
namespace InstanceRegistry {
|
| 16 |
+
|
| 17 |
+
inline std::map<std::string, int> readAll()
|
| 18 |
+
{
|
| 19 |
+
std::map<std::string, int> result;
|
| 20 |
+
std::ifstream file(PluginBridgeConstants::kRegistryPath);
|
| 21 |
+
if (!file.is_open()) return result;
|
| 22 |
+
|
| 23 |
+
try
|
| 24 |
+
{
|
| 25 |
+
nlohmann::json j;
|
| 26 |
+
file >> j;
|
| 27 |
+
for (auto& [key, val] : j.items())
|
| 28 |
+
{
|
| 29 |
+
if (val.is_number_integer())
|
| 30 |
+
result[key] = val.get<int>();
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
catch (...) {}
|
| 34 |
+
|
| 35 |
+
return result;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
inline void registerInstance(const std::string& channelName, int port)
|
| 39 |
+
{
|
| 40 |
+
// Read existing
|
| 41 |
+
auto registry = readAll();
|
| 42 |
+
|
| 43 |
+
// Update entry
|
| 44 |
+
registry[channelName] = port;
|
| 45 |
+
|
| 46 |
+
// Write to temp file, then atomic rename
|
| 47 |
+
std::string tmpPath = std::string(PluginBridgeConstants::kRegistryPath) + ".tmp";
|
| 48 |
+
|
| 49 |
+
nlohmann::json j(registry);
|
| 50 |
+
std::ofstream tmp(tmpPath);
|
| 51 |
+
if (tmp.is_open())
|
| 52 |
+
{
|
| 53 |
+
tmp << j.dump(2);
|
| 54 |
+
tmp.close();
|
| 55 |
+
std::rename(tmpPath.c_str(), PluginBridgeConstants::kRegistryPath);
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
inline void unregisterInstance(const std::string& channelName)
|
| 60 |
+
{
|
| 61 |
+
auto registry = readAll();
|
| 62 |
+
|
| 63 |
+
auto it = registry.find(channelName);
|
| 64 |
+
if (it == registry.end()) return;
|
| 65 |
+
|
| 66 |
+
registry.erase(it);
|
| 67 |
+
|
| 68 |
+
std::string tmpPath = std::string(PluginBridgeConstants::kRegistryPath) + ".tmp";
|
| 69 |
+
|
| 70 |
+
if (registry.empty())
|
| 71 |
+
{
|
| 72 |
+
// Remove the file entirely
|
| 73 |
+
std::remove(PluginBridgeConstants::kRegistryPath);
|
| 74 |
+
return;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
nlohmann::json j(registry);
|
| 78 |
+
std::ofstream tmp(tmpPath);
|
| 79 |
+
if (tmp.is_open())
|
| 80 |
+
{
|
| 81 |
+
tmp << j.dump(2);
|
| 82 |
+
tmp.close();
|
| 83 |
+
std::rename(tmpPath.c_str(), PluginBridgeConstants::kRegistryPath);
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
} // namespace InstanceRegistry
|