| 144 | } |
| 145 | |
| 146 | void RpcServer::HandleClient(ipc::UnixSocketConnection client) { |
| 147 | pthread_setname_np(pthread_self(), "rpc-handler"); |
| 148 | const std::string line = client.ReadLine(); |
| 149 | if (line.empty()) return; |
| 150 | auto request = nlohmann::json::parse(line, nullptr, false); |
| 151 | if (request.is_discarded()) { |
| 152 | auto response = Error("bad_json", "request is not valid JSON").dump() + "\n"; |
| 153 | (void)client.Send(response); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (request.value("type", "") == "vm.console.attach") { |
| 158 | std::string error; |
| 159 | if (!runtime_manager_.AttachConsole(request.value("vm_id", ""), &client, &error)) { |
| 160 | auto response = Error("console_attach_failed", error).dump() + "\n"; |
| 161 | (void)client.Send(response); |
| 162 | return; |
| 163 | } |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (request.value("type", "") == "vm.logs.follow") { |
| 168 | std::string error; |
| 169 | if (!runtime_manager_.AttachLogFollower(request.value("vm_id", ""), &client, &error)) { |
| 170 | auto response = Error("logs_follow_failed", error).dump() + "\n"; |
| 171 | (void)client.Send(response); |
| 172 | return; |
| 173 | } |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | auto response = HandleRequest(request).dump(); |
| 178 | response.push_back('\n'); |
| 179 | (void)client.Send(response); |
| 180 | } |
| 181 | |
| 182 | nlohmann::json RpcServer::HandleRequest(const nlohmann::json& request) { |
| 183 | const std::string type = request.value("type", ""); |
nothing calls this directly
no test coverage detected