Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add meta proto service for service discovery #543

Draft
wants to merge 40 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4c3eb9f
Add meta proto service for service discovery
oguzcanoguz Jan 25, 2024
cdc8fb3
Update protobuf/meta_services/ServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
adb0092
Update protobuf/meta_services/ServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
f15b249
Update protobuf/meta_services/ServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
d5d9dc2
Update protobuf/meta_services/test/TestServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
cf77db0
Update protobuf/meta_services/test/TestServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
9622fe2
Update protobuf/meta_services/ServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
b6cc192
Update protobuf/meta_services/ServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
bbc7075
Update protobuf/meta_services/test/TestServiceDiscoveryEcho.cpp
oguzcanoguz Jan 25, 2024
b9239e9
Apply suggestions from code review
oguzcanoguz Jan 25, 2024
2850b82
Add service changed notification to ServviceDiscovery
oguzcanoguz Jan 29, 2024
151e119
Merge remote-tracking branch 'origin/main' into feature/proto_service…
oguzcanoguz Apr 23, 2024
e8ca6d6
ServiceDiscoveryEcho refactor header
oguzcanoguz Apr 23, 2024
2dadfb7
Implement StartMethod and NotifyServiceChanges in ServiceDiscoveryEcho
oguzcanoguz Apr 25, 2024
2e575b3
Refactor ServiceDiscoveryTest
oguzcanoguz Apr 26, 2024
0db7aa5
Fix function static storage in ServiceDiscoveryEcho
oguzcanoguz Apr 26, 2024
ca8bdaa
Solve functio storage problem in ServiceDiscoveryTest
oguzcanoguz Apr 26, 2024
914754e
Make EchoConsole accept recursive directories for protobuf files
oguzcanoguz Apr 30, 2024
a2dd642
Start with PeerServiceDiscoverer
oguzcanoguz May 1, 2024
f0fc274
PeerServiceDiscovererTest: Add iterative service discovery
oguzcanoguz May 3, 2024
a8a24b8
PeerServiceDiscovererTest contd
oguzcanoguz May 6, 2024
df27790
Merge commit 'e9afc63890f512f66b28fe2b409de5b9fe471934'
oguzcanoguz Jun 7, 2024
0068cc6
Start with providing id range in NotifyServicesChanged
oguzcanoguz Aug 19, 2024
3685020
Add notifying service changes with a range
oguzcanoguz Sep 6, 2024
5db6745
Make service changes amnesiac
oguzcanoguz Sep 6, 2024
d5519bd
remove ServiceDiscoveryStarted from PeerServiceDiscovery
oguzcanoguz Oct 4, 2024
067ec70
Refactor PeerServiceDiscovererObserver
oguzcanoguz Oct 4, 2024
9118a61
PeerServiceDiscoverer: Start service discovery on construction
oguzcanoguz Oct 4, 2024
abfa695
PeerServiceDiscoverer: Handle service updated notification
oguzcanoguz Oct 4, 2024
e2c95a7
PeerServiceDiscovererTest: WIP Streamlining services changed tests
oguzcanoguz Oct 11, 2024
5a0a2be
Refactor PeerServiceDiscovery tests
oguzcanoguz Oct 28, 2024
a89a492
PeerServiceDiscovery: Discovery can end with a service or no service
oguzcanoguz Nov 1, 2024
6543e14
Start with a test echo server that can be run in devcontainer
oguzcanoguz Nov 4, 2024
a30b880
fix cmakelists
oguzcanoguz Nov 5, 2024
a471b1d
Try to instantiate PeerServiceDiscovery in echo console
oguzcanoguz Nov 11, 2024
62a871f
Fix: Use echo only when the connection observer is attached to a conn…
oguzcanoguz Nov 13, 2024
50d9170
ServiceDiscoveryEcho: Optimize FirstServiceSupported
oguzcanoguz Nov 13, 2024
138fc45
Merge branch 'temp' into feature/proto_service_discovery
oguzcanoguz Nov 13, 2024
161fcb6
trace discovered services
oguzcanoguz Nov 14, 2024
f65605a
Start working on attaching EchoConsole to Echo as a Service
oguzcanoguz Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions protobuf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(echo)
add_subdirectory(protoc_echo_plugin)
add_subdirectory(protoc_echo_plugin_csharp)
add_subdirectory(protoc_echo_plugin_java)
add_subdirectory(meta_services)
14 changes: 14 additions & 0 deletions protobuf/echo/Echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

namespace services
{
ServiceId::ServiceId(uint32_t id)
: id(id)
{}

Service::Service(Echo& echo, uint32_t serviceId)
: ServiceId(serviceId)
, infra::Observer<Service, Echo>(echo)
{}

uint32_t ServiceIdAccess::GetId(const ServiceId& serviceId)
{
return serviceId.id;
}

void Service::MethodDone()
{
Rpc().ServiceDone();
Expand Down
31 changes: 27 additions & 4 deletions protobuf/echo/Echo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,43 @@
#include "protobuf/echo/EchoErrorPolicy.hpp"
#include "protobuf/echo/Proto.hpp"
#include "protobuf/echo/Serialization.hpp"
#include <cstdint>

namespace services
{
class Echo;
class ServiceId;
class Service;
class ServiceProxy;

class Service
: public infra::Observer<Service, Echo>
class ServiceIdAccess
{
public:
using infra::Observer<Service, Echo>::Observer;
static uint32_t GetId(const ServiceId& serviceId);
};

class ServiceId
{
friend class ServiceIdAccess;

public:
ServiceId(uint32_t id);

virtual bool AcceptsService(uint32_t id) const
{
return id == this->id;
}

virtual bool AcceptsService(uint32_t id) const = 0;
private:
const uint32_t id;
};

class Service
: public ServiceId
, public infra::Observer<Service, Echo>
{
public:
Service(Echo& echo, uint32_t serviceId);

void MethodDone();
virtual infra::SharedPtr<MethodDeserializer> StartMethod(uint32_t serviceId, uint32_t methodId, uint32_t size, const EchoErrorPolicy& errorPolicy) = 0;
Expand Down
2 changes: 1 addition & 1 deletion protobuf/echo/ServiceForwarder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace services
{
ServiceForwarderBase::ServiceForwarderBase(Echo& echo, Echo& forwardTo)
: Service(echo)
: Service(echo, 0)
, ServiceProxy(forwardTo, 0)
{}

Expand Down
13 changes: 5 additions & 8 deletions protobuf/echo/test_doubles/ServiceStub.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "protobuf/echo/test_doubles/ServiceStub.hpp"
#include <cstdint>

namespace services
{
Expand Down Expand Up @@ -26,15 +27,10 @@ namespace services
return value;
}

ServiceStub::ServiceStub(Echo& echo)
: Service(echo)
ServiceStub::ServiceStub(Echo& echo, uint32_t serviceId)
: Service(echo, serviceId)
{}

bool ServiceStub::AcceptsService(uint32_t id) const
{
return id == serviceId;
}

infra::SharedPtr<MethodDeserializer> ServiceStub::StartMethod(uint32_t serviceId, uint32_t methodId, uint32_t size, const services::EchoErrorPolicy& errorPolicy)
{
switch (methodId)
Expand All @@ -55,8 +51,9 @@ namespace services
}
}

ServiceStubProxy::ServiceStubProxy(services::Echo& echo)
ServiceStubProxy::ServiceStubProxy(services::Echo& echo, uint32_t serviceId)
: services::ServiceProxy(echo, maxMessageSize)
, serviceId(serviceId)
{}

void ServiceStubProxy::Method(uint32_t value)
Expand Down
14 changes: 8 additions & 6 deletions protobuf/echo/test_doubles/ServiceStub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "protobuf/echo/Echo.hpp"
#include "gmock/gmock.h"
#include <cstdint>

namespace services
{
Expand Down Expand Up @@ -37,9 +38,7 @@ namespace services
: public services::Service
{
public:
ServiceStub(Echo& echo);

bool AcceptsService(uint32_t id) const override;
ServiceStub(Echo& echo, uint32_t serviceId = defaultServiceId);

MOCK_METHOD(void, Method, (uint32_t value));
MOCK_METHOD(void, MethodNoParameter, ());
Expand All @@ -48,7 +47,7 @@ namespace services
infra::SharedPtr<MethodDeserializer> StartMethod(uint32_t serviceId, uint32_t methodId, uint32_t size, const services::EchoErrorPolicy& errorPolicy) override;

public:
static const uint32_t serviceId = 1;
static constexpr uint32_t defaultServiceId = 1;
static const uint32_t idMethod = 1;
static const uint32_t idMethodNoParameter = 3;
static const uint32_t maxMessageSize = 18;
Expand All @@ -61,20 +60,23 @@ namespace services
: public services::ServiceProxy
{
public:
ServiceStubProxy(services::Echo& echo);
ServiceStubProxy(services::Echo& echo, uint32_t serviceId = defaultServiceId);

public:
void Method(uint32_t value);
void MethodNoParameter();

public:
static constexpr uint32_t serviceId = 1;
static constexpr uint32_t defaultServiceId = 1;
static constexpr uint32_t idMethod = 1;
static const uint32_t idMethodNoParameter = 3;
static constexpr uint32_t maxMessageSize = 18;

public:
using MethodTypeList = infra::List<Message, EmptyMessage>;

private:
const uint32_t serviceId;
};
}

Expand Down
20 changes: 20 additions & 0 deletions protobuf/meta_services/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_library(protobuf.meta_services ${EMIL_EXCLUDE_FROM_ALL} STATIC)

target_include_directories(protobuf.meta_services PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../..>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

protocol_buffer_csharp(protobuf.meta_services ServiceDiscovery.proto)
protocol_buffer_java(protobuf.meta_services ServiceDiscovery.proto)
protocol_buffer_echo_all(protobuf.meta_services ServiceDiscovery.proto)

target_sources(protobuf.meta_services PRIVATE
PeerServiceDiscoverer.cpp
PeerServiceDiscoverer.hpp
ServiceDiscoveryEcho.cpp
ServiceDiscoveryEcho.hpp
)

add_subdirectory(test)

92 changes: 92 additions & 0 deletions protobuf/meta_services/PeerServiceDiscoverer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "protobuf/meta_services/PeerServiceDiscoverer.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "infra/util/Function.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MegaLinter] reported by reviewdog 🐶

Suggested change
#include "infra/util/Function.hpp"
#include "protobuf/meta_services/PeerServiceDiscoverer.hpp"
#include "infra/util/Function.hpp"

#include <cstdint>
#include <limits>

namespace application
{
PeerServiceDiscovererEcho::PeerServiceDiscovererEcho(services::Echo& echo)
: service_discovery::ServiceDiscoveryProxy(echo)
, service_discovery::ServiceDiscoveryResponse(echo)
{
DiscoverPeerServices();
}

void PeerServiceDiscovererEcho::NoServiceSupported()
{
NotifyObservers([this](auto& observer)
{
observer.ServicesDiscovered(services.range());
});

MethodDone();
}

void PeerServiceDiscovererEcho::FirstServiceSupported(uint32_t id)
{
services.push_back(id);

if (id == SearchRangeEnd())
NotifyObservers([this](auto& observer)
{
observer.ServicesDiscovered(services.range());
});
else
RequestSend([this]
{
FindFirstServiceInRange(services.back() + 1, SearchRangeEnd());
});

MethodDone();
}

void PeerServiceDiscovererEcho::ServicesChanged(uint32_t startServiceId, uint32_t endServiceId)
{
searchRange = ServiceRange{ startServiceId, endServiceId };

StartDiscovery();

MethodDone();
}

void PeerServiceDiscovererEcho::DiscoverPeerServices()
{
searchRange = serviceRangeMax;

RequestSend([this]
{
NotifyServiceChanges(true);
StartDiscovery();
});
}

void PeerServiceDiscovererEcho::ClearUpdatedServices()
{
services.erase(std::remove_if(services.begin(), services.end(), [this](uint32_t serviceId)
{
return serviceId >= SearchRangeBegin() && serviceId <= SearchRangeEnd();
}),
services.end());
}

void PeerServiceDiscovererEcho::StartDiscovery()
{
ClearUpdatedServices();

RequestSend([this]
{
FindFirstServiceInRange(SearchRangeBegin(), SearchRangeEnd());
});
}

uint32_t PeerServiceDiscovererEcho::SearchRangeBegin() const
{
return std::get<0>(searchRange);
}

uint32_t PeerServiceDiscovererEcho::SearchRangeEnd() const
{
return std::get<1>(searchRange);
}
}
55 changes: 55 additions & 0 deletions protobuf/meta_services/PeerServiceDiscoverer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef PROTOBUF_ECHO_PEER_SERVICE_DISCOVERER_HPP
#define PROTOBUF_ECHO_PEER_SERVICE_DISCOVERER_HPP

#include "generated/echo/ServiceDiscovery.pb.hpp"
#include "infra/syntax/CppFormatter.hpp"
#include "infra/util/BoundedVector.hpp"
#include "infra/util/MemoryRange.hpp"
#include "infra/util/Observer.hpp"
#include "protobuf/echo/Echo.hpp"
#include <cstdint>
#include <tuple>

namespace application
{
class PeerServiceDiscovererEcho;

class PeerServiceDiscoveryObserver
: public infra::SingleObserver<PeerServiceDiscoveryObserver, PeerServiceDiscovererEcho>
{
public:
using infra::SingleObserver<PeerServiceDiscoveryObserver, PeerServiceDiscovererEcho>::SingleObserver;

virtual void ServicesDiscovered(infra::MemoryRange<uint32_t> services) = 0;
};

class PeerServiceDiscovererEcho
: public service_discovery::ServiceDiscoveryProxy
, public service_discovery::ServiceDiscoveryResponse
, public infra::Subject<PeerServiceDiscoveryObserver>
{
public:
explicit PeerServiceDiscovererEcho(services::Echo& echo);

void NoServiceSupported() override;
void FirstServiceSupported(uint32_t id) override;
void ServicesChanged(uint32_t startServiceId, uint32_t endServiceId) override;

private:
using ServiceRange = std::tuple<uint32_t, uint32_t>;
static constexpr ServiceRange serviceRangeMax = std::make_tuple(0, std::numeric_limits<uint32_t>::max());

private:
void DiscoverPeerServices();
void StartDiscovery();
void ClearUpdatedServices();
uint32_t SearchRangeBegin() const;
uint32_t SearchRangeEnd() const;

private:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MegaLinter] reported by reviewdog 🐶

Suggested change
private:
private:

infra::BoundedVector<uint32_t>::WithMaxSize<10> services;
ServiceRange searchRange;
};
}

#endif
40 changes: 40 additions & 0 deletions protobuf/meta_services/ServiceDiscovery.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

import "EchoAttributes.proto";

package service_discovery;
option java_package = "com.philips.emil.protobufEcho";
option java_outer_classname = "ServiceDiscoveryProto";

message Uint32Value {
uint32 value = 1;
}

message BoolValue
{
bool value = 1;
}

message ServiceRange
{
uint32 startServiceId = 1;
uint32 endServiceId = 2;
}

service ServiceDiscovery
{
option (service_id) = 1000;

rpc FindFirstServiceInRange(ServiceRange) returns (Nothing) { option (method_id) = 1; }
rpc NotifyServiceChanges(BoolValue) returns (Nothing) { option (method_id) = 2; }
}

service ServiceDiscoveryResponse
{
option (service_id) = 1001;

rpc FirstServiceSupported(Uint32Value) returns (Nothing) { option (method_id) = 1; }
rpc NoServiceSupported(Nothing) returns (Nothing) { option (method_id) = 2; }
rpc ServicesChanged(ServiceRange) returns (Nothing) { option (method_id) = 3; }
}

Loading