Merge "[WebRTC] Add an extra endpoint in signaling server to list devices"
diff --git a/host/frontend/gcastv2/signaling_server/Android.bp b/host/frontend/gcastv2/signaling_server/Android.bp
index 0600b74..5490fe6 100644
--- a/host/frontend/gcastv2/signaling_server/Android.bp
+++ b/host/frontend/gcastv2/signaling_server/Android.bp
@@ -25,6 +25,7 @@
         "client_handler.cpp",
         "device_registry.cpp",
         "device_handler.cpp",
+        "device_list_handler.cpp",
         "server_config.cpp",
         "server.cpp",
         "signal_handler.cpp",
diff --git a/host/frontend/gcastv2/signaling_server/device_list_handler.cpp b/host/frontend/gcastv2/signaling_server/device_list_handler.cpp
new file mode 100644
index 0000000..ac946ae
--- /dev/null
+++ b/host/frontend/gcastv2/signaling_server/device_list_handler.cpp
@@ -0,0 +1,38 @@
+//
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "host/frontend/gcastv2/signaling_server/device_list_handler.h"
+
+namespace cvd {
+
+DeviceListHandler::DeviceListHandler(const DeviceRegistry& registry)
+    : registry_(registry) {}
+
+int DeviceListHandler::handleMessage(uint8_t /*header_byte*/,
+                                     const uint8_t* /*msg*/,
+                                     size_t /*len*/) {
+  // ignore the message, just send the reply
+  Json::Value reply(Json::ValueType::arrayValue);
+
+  for (const auto& id : registry_.ListDeviceIds()) {
+    reply.append(id);
+  }
+  Json::FastWriter json_writer;
+  auto replyAsString = json_writer.write(reply);
+  sendMessage(replyAsString.c_str(), replyAsString.size());
+  return -1;  // disconnect
+}
+
+}  // namespace cvd
diff --git a/host/frontend/gcastv2/signaling_server/device_list_handler.h b/host/frontend/gcastv2/signaling_server/device_list_handler.h
new file mode 100644
index 0000000..0d099f1
--- /dev/null
+++ b/host/frontend/gcastv2/signaling_server/device_list_handler.h
@@ -0,0 +1,38 @@
+//
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <json/json.h>
+
+#include "host/frontend/gcastv2/https/include/https/WebSocketHandler.h"
+#include "host/frontend/gcastv2/signaling_server/device_registry.h"
+
+namespace cvd {
+
+class DeviceListHandler : public WebSocketHandler {
+ public:
+  DeviceListHandler(const DeviceRegistry& registry);
+
+ protected:
+  int handleMessage(uint8_t, const uint8_t*, size_t) override;
+
+ private:
+  const DeviceRegistry& registry_;
+};
+}  // namespace cvd
diff --git a/host/frontend/gcastv2/signaling_server/device_registry.cpp b/host/frontend/gcastv2/signaling_server/device_registry.cpp
index d7e9e3e..f5dc362 100644
--- a/host/frontend/gcastv2/signaling_server/device_registry.cpp
+++ b/host/frontend/gcastv2/signaling_server/device_registry.cpp
@@ -60,4 +60,12 @@
   return device_handler;
 }
 
+std::vector<std::string> DeviceRegistry::ListDeviceIds() const {
+  std::vector<std::string> ret;
+  for (const auto& entry: devices_) {
+    ret.push_back(entry.first);
+  }
+  return ret;
+}
+
 }  // namespace cvd
diff --git a/host/frontend/gcastv2/signaling_server/device_registry.h b/host/frontend/gcastv2/signaling_server/device_registry.h
index 3e7e919..0d2a46b 100644
--- a/host/frontend/gcastv2/signaling_server/device_registry.h
+++ b/host/frontend/gcastv2/signaling_server/device_registry.h
@@ -20,6 +20,7 @@
 #include <map>
 #include <memory>
 #include <string>
+#include <vector>
 
 #include <json/json.h>
 
@@ -33,8 +34,9 @@
                       std::weak_ptr<DeviceHandler> device_handler);
   void UnRegisterDevice(const std::string& device_id);
 
-  std::shared_ptr<DeviceHandler> GetDevice(
-      const std::string& device_id);
+  std::shared_ptr<DeviceHandler> GetDevice(const std::string& device_id);
+
+  std::vector<std::string> ListDeviceIds() const;
 
  private:
   std::map<std::string, std::weak_ptr<DeviceHandler>> devices_;
diff --git a/host/frontend/gcastv2/signaling_server/server.cpp b/host/frontend/gcastv2/signaling_server/server.cpp
index c028951..8fdf4c1 100644
--- a/host/frontend/gcastv2/signaling_server/server.cpp
+++ b/host/frontend/gcastv2/signaling_server/server.cpp
@@ -32,8 +32,9 @@
 #include "host/frontend/gcastv2/webrtc/include/webrtc/STUNMessage.h"
 
 #include "host/frontend/gcastv2/signaling_server/client_handler.h"
-#include "host/frontend/gcastv2/signaling_server/device_registry.h"
 #include "host/frontend/gcastv2/signaling_server/device_handler.h"
+#include "host/frontend/gcastv2/signaling_server/device_list_handler.h"
+#include "host/frontend/gcastv2/signaling_server/device_registry.h"
 #include "host/frontend/gcastv2/signaling_server/server_config.h"
 
 DEFINE_int32(http_server_port, 8443, "The port for the http server.");
@@ -107,6 +108,13 @@
                                      &device_registry, server_config));
       });
 
+  // This is non-standard utility endpoint, it's the simplest way for clients to
+  // obtain the ids of registered devices.
+  httpd->addWebSocketHandlerFactory("/list_devices", [&device_registry] {
+    return std::make_pair(
+        0, std::make_shared<cvd::DeviceListHandler>(device_registry));
+  });
+
   httpd->run();
   run_loop->run();