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 21 #include <json/json.h> 22 23 #include "host/frontend/webrtc_operator/device_registry.h" 24 #include "host/frontend/webrtc_operator/server_config.h" 25 #include "host/frontend/webrtc_operator/signal_handler.h" 26 #include "host/libs/websocket/websocket_handler.h" 27 28 namespace cuttlefish { 29 class DeviceHandler; 30 class ClientHandler : public SignalHandler, 31 public std::enable_shared_from_this<ClientHandler> { 32 public: 33 ClientHandler(struct lws* wsi, DeviceRegistry* registry, 34 const ServerConfig& server_config); 35 void SendDeviceMessage(const Json::Value& message); 36 37 void OnClosed() override; 38 39 protected: 40 void handleMessage(const std::string& type, 41 const Json::Value& message) override; 42 43 private: 44 void handleConnectionRequest(const Json::Value& message); 45 void handleForward(const Json::Value& message); 46 47 std::weak_ptr<DeviceHandler> device_handler_; 48 // The device handler assigns this to each client to be able to differentiate 49 // them. 50 size_t client_id_; 51 }; 52 53 class ClientHandlerFactory : public WebSocketHandlerFactory { 54 public: 55 ClientHandlerFactory(DeviceRegistry* registry, 56 const ServerConfig& server_config); 57 std::shared_ptr<WebSocketHandler> Build(struct lws* wsi) override; 58 59 private: 60 DeviceRegistry* registry_; 61 const ServerConfig& server_config_; 62 }; 63 } // namespace cuttlefish 64