1 // 2 // Copyright (C) 2020 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #pragma once 17 18 #include <memory> 19 #include <string> 20 #include <vector> 21 22 #include <json/json.h> 23 24 #include "host/frontend/webrtc_operator/device_registry.h" 25 #include "host/frontend/webrtc_operator/server_config.h" 26 #include "host/frontend/webrtc_operator/signal_handler.h" 27 #include "host/libs/websocket/websocket_handler.h" 28 29 namespace cuttlefish { 30 31 class ClientHandler; 32 33 class DeviceHandler : public SignalHandler, 34 public std::enable_shared_from_this<DeviceHandler> { 35 public: 36 DeviceHandler(struct lws* wsi, DeviceRegistry* registry, 37 const ServerConfig& server_config); 38 device_info()39 Json::Value device_info() const { return device_info_; } 40 41 size_t RegisterClient(std::shared_ptr<ClientHandler> client_handler); 42 void SendClientMessage(size_t client_id, const Json::Value& message); 43 void SendClientDisconnectMessage(size_t client_id); 44 45 void OnClosed() override; 46 47 protected: 48 void handleMessage(const std::string& type, 49 const Json::Value& message) override; 50 51 private: 52 void HandleRegistrationRequest(const Json::Value& message); 53 void HandleForward(const Json::Value& message); 54 55 std::string device_id_; 56 Json::Value device_info_; 57 std::vector<std::weak_ptr<ClientHandler>> clients_; 58 }; 59 60 class DeviceHandlerFactory : public WebSocketHandlerFactory { 61 public: 62 DeviceHandlerFactory(DeviceRegistry* registry, 63 const ServerConfig& server_config); 64 std::shared_ptr<WebSocketHandler> Build(struct lws* wsi) override; 65 66 private: 67 DeviceRegistry* registry_; 68 const ServerConfig& server_config_; 69 }; 70 } // namespace cuttlefish 71