libudx: Move ServiceDispatcher from libpdx_uds to libpdx
The two separate implementations of ServiceDispatcher for UDS and ServiceFS
are practically identical. Moved the implementation into the base libpdx lib,
and removed the transport-specific versions.
Bug: None
Test: `m -j32` succeeds; pdx_tests, pdx_servicefs_tests, pdx_uds_tests pass
Change-Id: I2344f4f23bc3da27eb7b192344844b87660e8610
diff --git a/libs/vr/libpdx/Android.bp b/libs/vr/libpdx/Android.bp
index f55e994..8fce140 100644
--- a/libs/vr/libpdx/Android.bp
+++ b/libs/vr/libpdx/Android.bp
@@ -5,12 +5,15 @@
"-Wall",
"-Wextra",
"-Werror",
+ "-DLOG_TAG=\"libpdx\"",
+ "-DTRACE=0",
],
export_include_dirs: ["private"],
local_include_dirs: ["private"],
srcs: [
"client.cpp",
"service.cpp",
+ "service_dispatcher.cpp",
"status.cpp",
],
}
diff --git a/libs/vr/libpdx/client.cpp b/libs/vr/libpdx/client.cpp
index bfa2d87..a01c4d6 100644
--- a/libs/vr/libpdx/client.cpp
+++ b/libs/vr/libpdx/client.cpp
@@ -1,6 +1,5 @@
#include "pdx/client.h"
-#define LOG_TAG "ServiceFramework"
#include <log/log.h>
#include <pdx/trace.h>
diff --git a/libs/vr/libpdx/mock_tests.cpp b/libs/vr/libpdx/mock_tests.cpp
index 76fd154..4143837 100644
--- a/libs/vr/libpdx/mock_tests.cpp
+++ b/libs/vr/libpdx/mock_tests.cpp
@@ -3,7 +3,6 @@
#include <pdx/mock_client_channel_factory.h>
#include <pdx/mock_message_reader.h>
#include <pdx/mock_message_writer.h>
-#include <pdx/mock_service_dispatcher.h>
#include <pdx/mock_service_endpoint.h>
TEST(MockTypes, Instantiation) {
@@ -15,6 +14,5 @@
android::pdx::MockMessageReader message_reader;
android::pdx::MockOutputResourceMapper output_resource_mapper;
android::pdx::MockMessageWriter message_writer;
- android::pdx::MockServiceDispatcher service_dispatcher;
android::pdx::MockEndpoint endpoint;
}
diff --git a/libs/vr/libpdx/private/pdx/mock_service_dispatcher.h b/libs/vr/libpdx/private/pdx/mock_service_dispatcher.h
deleted file mode 100644
index 9b51d30..0000000
--- a/libs/vr/libpdx/private/pdx/mock_service_dispatcher.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef ANDROID_PDX_MOCK_SERVICE_DISPATCHER_H_
-#define ANDROID_PDX_MOCK_SERVICE_DISPATCHER_H_
-
-#include <gmock/gmock.h>
-#include <pdx/service_dispatcher.h>
-
-namespace android {
-namespace pdx {
-
-class MockServiceDispatcher : public ServiceDispatcher {
- public:
- MOCK_METHOD1(AddService, int(const std::shared_ptr<Service>& service));
- MOCK_METHOD1(RemoveService, int(const std::shared_ptr<Service>& service));
- MOCK_METHOD0(ReceiveAndDispatch, int());
- MOCK_METHOD1(ReceiveAndDispatch, int(int timeout));
- MOCK_METHOD0(EnterDispatchLoop, int());
- MOCK_METHOD1(SetCanceled, void(bool cancel));
- MOCK_CONST_METHOD0(IsCanceled, bool());
-};
-
-} // namespace pdx
-} // namespace android
-
-#endif // ANDROID_PDX_MOCK_SERVICE_DISPATCHER_H_
diff --git a/libs/vr/libpdx/private/pdx/mock_service_endpoint.h b/libs/vr/libpdx/private/pdx/mock_service_endpoint.h
index e741d4a..7f829e7 100644
--- a/libs/vr/libpdx/private/pdx/mock_service_endpoint.h
+++ b/libs/vr/libpdx/private/pdx/mock_service_endpoint.h
@@ -66,6 +66,7 @@
MOCK_METHOD0(AllocateMessageState, void*());
MOCK_METHOD1(FreeMessageState, void(void* state));
MOCK_METHOD0(Cancel, Status<void>());
+ MOCK_CONST_METHOD0(epoll_fd, int());
};
} // namespace pdx
diff --git a/libs/vr/libpdx/private/pdx/service_dispatcher.h b/libs/vr/libpdx/private/pdx/service_dispatcher.h
index c5e342a..bd27000 100644
--- a/libs/vr/libpdx/private/pdx/service_dispatcher.h
+++ b/libs/vr/libpdx/private/pdx/service_dispatcher.h
@@ -2,6 +2,11 @@
#define ANDROID_PDX_SERVICE_DISPATCHER_H_
#include <memory>
+#include <mutex>
+#include <unordered_map>
+#include <vector>
+
+#include <pdx/file_handle.h>
namespace android {
namespace pdx {
@@ -15,7 +20,10 @@
*/
class ServiceDispatcher {
public:
- virtual ~ServiceDispatcher() = default;
+ // Get a new instance of ServiceDispatcher, or return nullptr if init failed.
+ static std::unique_ptr<ServiceDispatcher> Create();
+
+ ~ServiceDispatcher();
/*
* Adds a service to the list of services handled by this dispatcher. This
@@ -24,7 +32,7 @@
*
* Returns 0 on success; -EEXIST if the service was already added.
*/
- virtual int AddService(const std::shared_ptr<Service>& service) = 0;
+ int AddService(const std::shared_ptr<Service>& service);
/*
* Removes a service from this dispatcher. This will fail if any threads are
@@ -33,7 +41,7 @@
* Returns 0 on success; -ENOENT if the service was not previously added;
* -EBUSY if there are threads in the dispatcher.
*/
- virtual int RemoveService(const std::shared_ptr<Service>& service) = 0;
+ int RemoveService(const std::shared_ptr<Service>& service);
/*
* Receive and dispatch one set of messages. Multiple threads may enter this
@@ -42,14 +50,14 @@
* cycle, requiring an external loop. This is useful when other work needs
* to be done in the service dispatch loop.
*/
- virtual int ReceiveAndDispatch() = 0;
+ int ReceiveAndDispatch();
/*
* Same as above with timeout in milliseconds. A negative value means
* infinite timeout, while a value of 0 means return immediately if no
* messages are available to receive.
*/
- virtual int ReceiveAndDispatch(int timeout) = 0;
+ int ReceiveAndDispatch(int timeout);
/*
* Receive and dispatch messages until canceled. When more than one thread
@@ -58,19 +66,39 @@
* hands Message instances (via move assignment) over to a queue of threads
* (or perhaps one of several) to handle.
*/
- virtual int EnterDispatchLoop() = 0;
+ int EnterDispatchLoop();
/*
* Sets the canceled state of the dispatcher. When canceled is true, any
* threads blocked waiting for messages will return. This method waits until
* all dispatch threads have exited the dispatcher.
*/
- virtual void SetCanceled(bool cancel) = 0;
+ void SetCanceled(bool cancel);
/*
* Gets the canceled state of the dispatcher.
*/
- virtual bool IsCanceled() const = 0;
+ bool IsCanceled() const;
+
+ private:
+ ServiceDispatcher();
+
+ // Internal thread accounting.
+ int ThreadEnter();
+ void ThreadExit();
+
+ std::mutex mutex_;
+ std::condition_variable condition_;
+ std::atomic<bool> canceled_{false};
+
+ std::vector<std::shared_ptr<Service>> services_;
+
+ int thread_count_ = 0;
+ LocalHandle event_fd_;
+ LocalHandle epoll_fd_;
+
+ ServiceDispatcher(const ServiceDispatcher&) = delete;
+ void operator=(const ServiceDispatcher&) = delete;
};
} // namespace pdx
diff --git a/libs/vr/libpdx/private/pdx/service_endpoint.h b/libs/vr/libpdx/private/pdx/service_endpoint.h
index 28bd6bc..d581894 100644
--- a/libs/vr/libpdx/private/pdx/service_endpoint.h
+++ b/libs/vr/libpdx/private/pdx/service_endpoint.h
@@ -136,6 +136,10 @@
// Cancels the endpoint, unblocking any receiver threads waiting for a
// message.
virtual Status<void> Cancel() = 0;
+
+ // Returns an fd that can be used with epoll() to wait for incoming messages
+ // from this endpoint.
+ virtual int epoll_fd() const = 0;
};
} // namespace pdx
diff --git a/libs/vr/libpdx/service.cpp b/libs/vr/libpdx/service.cpp
index fab4770..1d3b62a 100644
--- a/libs/vr/libpdx/service.cpp
+++ b/libs/vr/libpdx/service.cpp
@@ -1,4 +1,3 @@
-#define LOG_TAG "ServiceFramework"
#include "pdx/service.h"
#include <fcntl.h>
@@ -10,8 +9,6 @@
#include <pdx/trace.h>
-#define TRACE 0
-
namespace android {
namespace pdx {
diff --git a/libs/vr/libpdx_uds/service_dispatcher.cpp b/libs/vr/libpdx/service_dispatcher.cpp
similarity index 83%
rename from libs/vr/libpdx_uds/service_dispatcher.cpp
rename to libs/vr/libpdx/service_dispatcher.cpp
index 2c52578..b112fa3 100644
--- a/libs/vr/libpdx_uds/service_dispatcher.cpp
+++ b/libs/vr/libpdx/service_dispatcher.cpp
@@ -1,26 +1,25 @@
-#include "uds/service_dispatcher.h"
+#include <pdx/service_dispatcher.h>
#include <errno.h>
#include <log/log.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
-#include "pdx/service.h"
-#include "uds/service_endpoint.h"
+#include <pdx/service.h>
+#include <pdx/service_endpoint.h>
static const int kMaxEventsPerLoop = 128;
namespace android {
namespace pdx {
-namespace uds {
-std::unique_ptr<pdx::ServiceDispatcher> ServiceDispatcher::Create() {
+std::unique_ptr<ServiceDispatcher> ServiceDispatcher::Create() {
std::unique_ptr<ServiceDispatcher> dispatcher{new ServiceDispatcher()};
if (!dispatcher->epoll_fd_ || !dispatcher->event_fd_) {
dispatcher.reset();
}
- return std::move(dispatcher);
+ return dispatcher;
}
ServiceDispatcher::ServiceDispatcher() {
@@ -70,18 +69,14 @@
}
int ServiceDispatcher::AddService(const std::shared_ptr<Service>& service) {
- if (service->endpoint()->GetIpcTag() != Endpoint::kIpcTag)
- return -EINVAL;
-
std::lock_guard<std::mutex> autolock(mutex_);
- auto* endpoint = static_cast<Endpoint*>(service->endpoint());
epoll_event event;
event.events = EPOLLIN;
event.data.ptr = service.get();
- if (epoll_ctl(epoll_fd_.Get(), EPOLL_CTL_ADD, endpoint->epoll_fd(), &event) <
- 0) {
+ if (epoll_ctl(epoll_fd_.Get(), EPOLL_CTL_ADD, service->endpoint()->epoll_fd(),
+ &event) < 0) {
ALOGE("Failed to add service to dispatcher because: %s\n", strerror(errno));
return -errno;
}
@@ -91,9 +86,6 @@
}
int ServiceDispatcher::RemoveService(const std::shared_ptr<Service>& service) {
- if (service->endpoint()->GetIpcTag() != Endpoint::kIpcTag)
- return -EINVAL;
-
std::lock_guard<std::mutex> autolock(mutex_);
// It's dangerous to remove a service while other threads may be using it.
@@ -101,16 +93,15 @@
return -EBUSY;
epoll_event dummy; // See BUGS in man 2 epoll_ctl.
-
- auto* endpoint = static_cast<Endpoint*>(service->endpoint());
- if (epoll_ctl(epoll_fd_.Get(), EPOLL_CTL_DEL, endpoint->epoll_fd(), &dummy) <
- 0) {
+ if (epoll_ctl(epoll_fd_.Get(), EPOLL_CTL_DEL, service->endpoint()->epoll_fd(),
+ &dummy) < 0) {
ALOGE("Failed to remove service from dispatcher because: %s\n",
strerror(errno));
return -errno;
}
- services_.remove(service);
+ services_.erase(std::remove(services_.begin(), services_.end(), service),
+ services_.end());
return 0;
}
@@ -139,7 +130,7 @@
Service* service = static_cast<Service*>(events[i].data.ptr);
ALOGI_IF(TRACE, "Dispatching message: fd=%d\n",
- static_cast<Endpoint*>(service->endpoint())->epoll_fd());
+ service->endpoint()->epoll_fd());
service->ReceiveAndDispatch();
}
}
@@ -171,7 +162,7 @@
Service* service = static_cast<Service*>(events[i].data.ptr);
ALOGI_IF(TRACE, "Dispatching message: fd=%d\n",
- static_cast<Endpoint*>(service->endpoint())->epoll_fd());
+ service->endpoint()->epoll_fd());
service->ReceiveAndDispatch();
}
}
@@ -197,6 +188,5 @@
bool ServiceDispatcher::IsCanceled() const { return canceled_; }
-} // namespace uds
} // namespace pdx
} // namespace android
diff --git a/libs/vr/libpdx_default_transport/private/servicefs/pdx/default_transport/service_dispatcher.h b/libs/vr/libpdx_default_transport/private/servicefs/pdx/default_transport/service_dispatcher.h
deleted file mode 100644
index 158871c..0000000
--- a/libs/vr/libpdx_default_transport/private/servicefs/pdx/default_transport/service_dispatcher.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef ANDROID_PDX_DEFAULT_TRANSPORT_SERVICEFS_SERVICE_DISPATCHER_H_
-#define ANDROID_PDX_DEFAULT_TRANSPORT_SERVICEFS_SERVICE_DISPATCHER_H_
-
-#include <servicefs/service_dispatcher.h>
-
-namespace android {
-namespace pdx {
-namespace default_transport {
-
-using ServiceDispatcher = ::android::pdx::servicefs::ServiceDispatcher;
-
-} // namespace default_transport
-} // namespace pdx
-} // namespace android
-
-
-#endif // ANDROID_PDX_DEFAULT_TRANSPORT_SERVICEFS_SERVICE_DISPATCHER_H_
diff --git a/libs/vr/libpdx_default_transport/private/uds/pdx/default_transport/service_dispatcher.h b/libs/vr/libpdx_default_transport/private/uds/pdx/default_transport/service_dispatcher.h
deleted file mode 100644
index 7cb7a80..0000000
--- a/libs/vr/libpdx_default_transport/private/uds/pdx/default_transport/service_dispatcher.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef ANDROID_PDX_DEFAULT_TRANSPORT_UDS_SERVICE_DISPATCHER_H_
-#define ANDROID_PDX_DEFAULT_TRANSPORT_UDS_SERVICE_DISPATCHER_H_
-
-#include <uds/service_dispatcher.h>
-
-namespace android {
-namespace pdx {
-namespace default_transport {
-
-using ServiceDispatcher = ::android::pdx::uds::ServiceDispatcher;
-
-} // namespace default_transport
-} // namespace pdx
-} // namespace android
-
-
-#endif // ANDROID_PDX_DEFAULT_TRANSPORT_UDS_SERVICE_DISPATCHER_H_
diff --git a/libs/vr/libpdx_uds/Android.bp b/libs/vr/libpdx_uds/Android.bp
index 82a5ea7..d0b7cab 100644
--- a/libs/vr/libpdx_uds/Android.bp
+++ b/libs/vr/libpdx_uds/Android.bp
@@ -16,7 +16,6 @@
"client_channel_factory.cpp",
"client_channel.cpp",
"ipc_helper.cpp",
- "service_dispatcher.cpp",
"service_endpoint.cpp",
],
static_libs: [
diff --git a/libs/vr/libpdx_uds/client_channel_tests.cpp b/libs/vr/libpdx_uds/client_channel_tests.cpp
index 7c3c68a..1560030 100644
--- a/libs/vr/libpdx_uds/client_channel_tests.cpp
+++ b/libs/vr/libpdx_uds/client_channel_tests.cpp
@@ -13,6 +13,7 @@
#include <pdx/client.h>
#include <pdx/rpc/remote_method.h>
#include <pdx/service.h>
+#include <pdx/service_dispatcher.h>
#include <uds/client_channel_factory.h>
#include <uds/service_endpoint.h>
@@ -81,7 +82,7 @@
auto endpoint = Endpoint::CreateFromSocketFd(LocalHandle{});
endpoint->RegisterNewChannelForTests(std::move(channel_socket));
service_ = TestService::Create(std::move(endpoint));
- dispatcher_ = android::pdx::uds::ServiceDispatcher::Create();
+ dispatcher_ = ServiceDispatcher::Create();
dispatcher_->AddService(service_);
dispatch_thread_ = std::thread(
std::bind(&ServiceDispatcher::EnterDispatchLoop, dispatcher_.get()));
diff --git a/libs/vr/libpdx_uds/private/uds/service_dispatcher.h b/libs/vr/libpdx_uds/private/uds/service_dispatcher.h
deleted file mode 100644
index 23af4f4..0000000
--- a/libs/vr/libpdx_uds/private/uds/service_dispatcher.h
+++ /dev/null
@@ -1,55 +0,0 @@
-#ifndef ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_
-#define ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_
-
-#include <list>
-#include <memory>
-#include <mutex>
-#include <unordered_map>
-
-#include <pdx/file_handle.h>
-#include <pdx/service_dispatcher.h>
-
-namespace android {
-namespace pdx {
-namespace uds {
-
-class ServiceDispatcher : public pdx::ServiceDispatcher {
- public:
- // Get a new instance of ServiceDispatcher, or return nullptr if init failed.
- static std::unique_ptr<pdx::ServiceDispatcher> Create();
-
- ~ServiceDispatcher() override;
- int AddService(const std::shared_ptr<Service>& service) override;
- int RemoveService(const std::shared_ptr<Service>& service) override;
- int ReceiveAndDispatch() override;
- int ReceiveAndDispatch(int timeout) override;
- int EnterDispatchLoop() override;
- void SetCanceled(bool cancel) override;
- bool IsCanceled() const override;
-
- private:
- ServiceDispatcher();
-
- // Internal thread accounting.
- int ThreadEnter();
- void ThreadExit();
-
- std::mutex mutex_;
- std::condition_variable condition_;
- std::atomic<bool> canceled_{false};
-
- std::list<std::shared_ptr<Service>> services_;
-
- int thread_count_ = 0;
- LocalHandle event_fd_;
- LocalHandle epoll_fd_;
-
- ServiceDispatcher(const ServiceDispatcher&) = delete;
- void operator=(const ServiceDispatcher&) = delete;
-};
-
-} // namespace uds
-} // namespace pdx
-} // namespace android
-
-#endif // ANDROID_PDX_UDS_SERVICE_DISPATCHER_H_
diff --git a/libs/vr/libpdx_uds/private/uds/service_endpoint.h b/libs/vr/libpdx_uds/private/uds/service_endpoint.h
index 368891c..a163812 100644
--- a/libs/vr/libpdx_uds/private/uds/service_endpoint.h
+++ b/libs/vr/libpdx_uds/private/uds/service_endpoint.h
@@ -12,7 +12,6 @@
#include <pdx/service.h>
#include <pdx/service_endpoint.h>
#include <uds/channel_event_set.h>
-#include <uds/service_dispatcher.h>
namespace android {
namespace pdx {
@@ -105,7 +104,7 @@
// socket file descriptor.
Status<void> RegisterNewChannelForTests(LocalHandle channel_fd);
- int epoll_fd() const { return epoll_fd_.Get(); }
+ int epoll_fd() const override { return epoll_fd_.Get(); }
private:
struct ChannelData {
diff --git a/libs/vr/libpdx_uds/remote_method_tests.cpp b/libs/vr/libpdx_uds/remote_method_tests.cpp
index 3109753..3f25776 100644
--- a/libs/vr/libpdx_uds/remote_method_tests.cpp
+++ b/libs/vr/libpdx_uds/remote_method_tests.cpp
@@ -15,9 +15,9 @@
#include <pdx/rpc/remote_method.h>
#include <pdx/rpc/serializable.h>
#include <pdx/service.h>
+#include <pdx/service_dispatcher.h>
#include <uds/client_channel.h>
#include <uds/client_channel_factory.h>
-#include <uds/service_dispatcher.h>
#include <uds/service_endpoint.h>
using android::pdx::BorrowedHandle;
@@ -561,7 +561,7 @@
void SetUp() override {
// Create a dispatcher to handle messages to services.
- dispatcher_ = android::pdx::uds::ServiceDispatcher::Create();
+ dispatcher_ = android::pdx::ServiceDispatcher::Create();
ASSERT_NE(nullptr, dispatcher_);
// Start the message dispatch loop in a separate thread.
diff --git a/libs/vr/libpdx_uds/service_framework_tests.cpp b/libs/vr/libpdx_uds/service_framework_tests.cpp
index 2943239..5943b0a 100644
--- a/libs/vr/libpdx_uds/service_framework_tests.cpp
+++ b/libs/vr/libpdx_uds/service_framework_tests.cpp
@@ -16,10 +16,10 @@
#include <pdx/client.h>
#include <pdx/file_handle.h>
#include <pdx/service.h>
+#include <pdx/service_dispatcher.h>
#include <private/android_filesystem_config.h>
#include <uds/client_channel.h>
#include <uds/client_channel_factory.h>
-#include <uds/service_dispatcher.h>
#include <uds/service_endpoint.h>
using android::pdx::BorrowedChannelHandle;
@@ -425,7 +425,7 @@
void SetUp() override {
// Create a dispatcher to handle messages to services.
- dispatcher_ = android::pdx::uds::ServiceDispatcher::Create();
+ dispatcher_ = android::pdx::ServiceDispatcher::Create();
ASSERT_NE(nullptr, dispatcher_);
// Start the message dispatch loop in a separate thread.
diff --git a/libs/vr/libvrflinger/include/dvr/vr_flinger.h b/libs/vr/libvrflinger/include/dvr/vr_flinger.h
index 145852e..6c1995e 100644
--- a/libs/vr/libvrflinger/include/dvr/vr_flinger.h
+++ b/libs/vr/libvrflinger/include/dvr/vr_flinger.h
@@ -4,7 +4,7 @@
#include <thread>
#include <memory>
-#include <pdx/default_transport/service_dispatcher.h>
+#include <pdx/service_dispatcher.h>
#include <vr/vr_manager/vr_manager.h>
namespace android {
diff --git a/libs/vr/libvrflinger/vr_flinger.cpp b/libs/vr/libvrflinger/vr_flinger.cpp
index b2dc1d8..41526ee 100644
--- a/libs/vr/libvrflinger/vr_flinger.cpp
+++ b/libs/vr/libvrflinger/vr_flinger.cpp
@@ -18,8 +18,6 @@
#include <sys/prctl.h>
#include <sys/resource.h>
-#include <pdx/default_transport/service_dispatcher.h>
-
#include <functional>
#include "DisplayHardware/ComposerHal.h"
@@ -76,7 +74,7 @@
request_display_callback_ = request_display_callback;
- dispatcher_ = android::pdx::default_transport::ServiceDispatcher::Create();
+ dispatcher_ = android::pdx::ServiceDispatcher::Create();
CHECK_ERROR(!dispatcher_, error, "Failed to create service dispatcher.");
display_service_ =