|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Arm Limited. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "ProjMgrIpcServer.h" |
| 8 | +#include "ProjMgr.h" |
| 9 | +#include "ProductInfo.h" |
| 10 | + |
| 11 | +#include "cproto.pb.h" |
| 12 | + |
| 13 | +#include <iostream> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +ContextItem m_globalContext; |
| 19 | + |
| 20 | +ProjMgrIpcServer::ProjMgrIpcServer(void) { |
| 21 | + // Reserved |
| 22 | +} |
| 23 | + |
| 24 | +ProjMgrIpcServer::~ProjMgrIpcServer(void) { |
| 25 | + // Reserved |
| 26 | +} |
| 27 | + |
| 28 | +ResponseEnvelope ProjMgrIpcServer::GetVersion(const std::string& payload) { |
| 29 | + ResponseEnvelope envelope; |
| 30 | + GetVersionResponse response; |
| 31 | + response.set_version(VERSION_STRING); |
| 32 | + envelope.set_status(true); |
| 33 | + envelope.set_payload(response.SerializeAsString()); |
| 34 | + return envelope; |
| 35 | +} |
| 36 | + |
| 37 | +ResponseEnvelope ProjMgrIpcServer::Shutdown(const std::string& payload) { |
| 38 | + ResponseEnvelope envelope; |
| 39 | + envelope.set_status(true); |
| 40 | + return envelope; |
| 41 | +} |
| 42 | + |
| 43 | +ResponseEnvelope ProjMgrIpcServer::LoadPacks(const std::string& payload) { |
| 44 | + ResponseEnvelope envelope; |
| 45 | + m_manager->GetWorker().LoadPacks(m_globalContext); |
| 46 | + envelope.set_status(true); |
| 47 | + return envelope; |
| 48 | +} |
| 49 | + |
| 50 | +ResponseEnvelope ProjMgrIpcServer::LoadSolution(const std::string& payload) { |
| 51 | + LoadSolutionRequest request; |
| 52 | + request.ParseFromString(payload); |
| 53 | + ResponseEnvelope envelope; |
| 54 | + envelope.set_status(true); |
| 55 | + return envelope; |
| 56 | +} |
| 57 | + |
| 58 | +ResponseEnvelope ProjMgrIpcServer::ListComponents(const std::string& payload) { |
| 59 | + RteComponentMap installedComponents = m_globalContext.rteActiveTarget->GetFilteredComponents(); |
| 60 | + ListComponentsResponse response; |
| 61 | + for (const auto& component : installedComponents) { |
| 62 | + response.add_component(component.first); |
| 63 | + } |
| 64 | + ResponseEnvelope envelope; |
| 65 | + envelope.set_payload(response.SerializeAsString()); |
| 66 | + envelope.set_status(true); |
| 67 | + return envelope; |
| 68 | +} |
| 69 | + |
| 70 | +std::string ProjMgrIpcServer::ProcessRequest(const std::string& requestBytes, bool& shutdown) { |
| 71 | + // Registry of requests identifiers mapped to handlers |
| 72 | + const unordered_map<int, function<ResponseEnvelope(const string&)>> m_requests = { |
| 73 | + { ID::SHUTDOWN , [&](const string& payload) { return Shutdown(payload); } }, |
| 74 | + { ID::GET_VERSION , [&](const string& payload) { return GetVersion(payload); } }, |
| 75 | + { ID::LOAD_PACKS , [&](const string& payload) { return LoadPacks(payload); } }, |
| 76 | + { ID::LOAD_SOLUTION , [&](const string& payload) { return LoadSolution(payload); } }, |
| 77 | + { ID::LIST_COMPONENTS , [&](const string& payload) { return ListComponents(payload); } }, |
| 78 | + }; |
| 79 | + |
| 80 | + // Parse request |
| 81 | + RequestEnvelope request; |
| 82 | + if (!request.ParseFromString(requestBytes)) { |
| 83 | + ResponseEnvelope errorResponse; |
| 84 | + errorResponse.set_id(ID::NONE); |
| 85 | + errorResponse.set_status(false); |
| 86 | + errorResponse.set_error("Failed to parse request"); |
| 87 | + return errorResponse.SerializeAsString(); |
| 88 | + } |
| 89 | + |
| 90 | + // Find the correct request handler |
| 91 | + auto id = m_requests.find(request.id()); |
| 92 | + if (id == m_requests.end()) { |
| 93 | + ResponseEnvelope errorResponse; |
| 94 | + errorResponse.set_id(ID::NONE); |
| 95 | + errorResponse.set_status(false); |
| 96 | + errorResponse.set_error("Unknown id: " + request.id()); |
| 97 | + return errorResponse.SerializeAsString(); |
| 98 | + } |
| 99 | + |
| 100 | + // Call the request handler |
| 101 | + auto response = id->second(request.payload()); |
| 102 | + response.set_id(request.id()); |
| 103 | + |
| 104 | + // Set shutdown flag |
| 105 | + shutdown = (request.id() == ID::SHUTDOWN); |
| 106 | + |
| 107 | + // Serialize response envelope |
| 108 | + return response.SerializeAsString(); |
| 109 | +} |
| 110 | + |
| 111 | +#ifdef _WIN32 |
| 112 | + #include <fcntl.h> |
| 113 | + #include <io.h> |
| 114 | +#endif |
| 115 | + |
| 116 | +bool ProjMgrIpcServer::Run(ProjMgr* manager) { |
| 117 | + |
| 118 | +#ifdef _WIN32 |
| 119 | + // Force binary mode (prevent \n to \r\n conversion) |
| 120 | + _setmode(_fileno(stdout), _O_BINARY); |
| 121 | + _setmode(_fileno(stdin), _O_BINARY); |
| 122 | +#endif |
| 123 | + |
| 124 | + GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 125 | + |
| 126 | + m_manager = manager; |
| 127 | + std::string serializedData; |
| 128 | + uint32_t size; |
| 129 | + bool shutdown = false; |
| 130 | + |
| 131 | + while (!shutdown) { |
| 132 | + // Read size (4 bytes) |
| 133 | + std::cin.read(reinterpret_cast<char*>(&size), sizeof(size)); |
| 134 | + |
| 135 | + // Read protobuf message |
| 136 | + serializedData.resize(size); |
| 137 | + std::cin.read(&serializedData[0], size); |
| 138 | + |
| 139 | + // Process request and get response |
| 140 | + auto responseData = ProcessRequest(serializedData, shutdown); |
| 141 | + |
| 142 | + // Send size |
| 143 | + uint32_t responseSize = responseData.size(); |
| 144 | + std::cout.write(reinterpret_cast<char*>(&responseSize), sizeof(responseSize)); |
| 145 | + |
| 146 | + // Send protobuf message |
| 147 | + std::cout.write(responseData.data(), responseSize); |
| 148 | + std::cout.flush(); |
| 149 | + } |
| 150 | + return true; |
| 151 | +} |
0 commit comments