Skip to content

Commit

Permalink
grund-bus: Initial implementation of service locator.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 11, 2024
1 parent 6a3f37b commit 489bc45
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/libs/karm-io/impls.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ struct BufWriter :
public Seeker {

MutBytes _buf;
usize _pos;
usize _pos = 0;

BufWriter(MutBytes buf) : _buf(buf), _pos(0) {}
BufWriter(MutBytes buf) : _buf(buf) {}

Res<usize> seek(Seek seek) override {
_pos = seek.apply(_pos, sizeOf(_buf));
Expand Down
20 changes: 19 additions & 1 deletion src/libs/karm-sys/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Port : public Distinct<u64, struct _PortTag> {
};

constexpr Port Port::INVALID{0};
constexpr Port Port::BUS{1};
constexpr Port Port::BUS{Limits<u64>::MAX};

struct Header {
u64 seq;
Expand Down Expand Up @@ -66,6 +66,24 @@ struct Message {
return maybeHeader.unwrap().mid == Meta::idOf<T>();
}

template <typename T, typename... Args>
static Res<Message> pack(Port port, u64 seq, Args &&...args) {
Header header{seq, port, Meta::idOf<T>()};
T payload{std::forward<Args>(args)...};

Message msg;
Io::BufWriter reqBuf{msg.bytes};
Io::PackEmit reqPack{reqBuf};

try$(Io::pack(reqPack, header));
try$(Io::pack(reqPack, payload));

msg.len = try$(Io::tell(reqBuf));
msg.handlesLen = 0;

return Ok(msg);
}

template <typename T>
Res<T> unpack() {
Io::PackScan s{bytes, handles};
Expand Down
13 changes: 13 additions & 0 deletions src/srvs/grund-bus/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <karm-base/string.h>
#include <karm-sys/ipc.h>

namespace Grund::Bus {

struct Locate {
using Response = Sys::Port;
String id;
};

} // namespace Grund::Bus
25 changes: 25 additions & 0 deletions src/srvs/grund-bus/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <karm-base/size.h>
#include <karm-logger/logger.h>

#include "api.h"
#include "bus.h"

namespace Grund::Bus {
Expand Down Expand Up @@ -129,6 +130,30 @@ Res<> Service::dispatch(Sys::Message &msg) {
return _con.send(sub(msg.bytes, msg.len), sub(msg.handles, msg.handlesLen));
}

// MARK: Locator ---------------------------------------------------------------

Locator::Locator() {
_port = Sys::Port::BUS;
}

Str Locator::id() const {
return "grund-bus";
}

Res<> Locator::dispatch(Sys::Port from, Sys::Message &msg) {
if (msg.is<Locate>()) {
auto locate = try$(msg.unpack<Locate>());
for (auto &endpoint : _bus->_endpoints) {
if (endpoint->id() == locate.id){

return from.dispatch()
}
}
} else {
return Error::invalidInput("invalid message");
}
}

// MARK: Bus -------------------------------------------------------------------

Karm::Res<Strong<Bus>> Bus::create(Sys::Context &ctx) {
Expand Down
12 changes: 12 additions & 0 deletions src/srvs/grund-bus/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct Endpoint : public Meta::Static {

void attach(Bus &bus) { _bus = &bus; }

virtual Str id() const = 0;

virtual Res<> dispatch(Sys::Message &) { return Ok(); }

virtual Res<> activate(Sys::Context &) { return Ok(); }
Expand All @@ -43,13 +45,23 @@ struct Service : public Endpoint {
: _id{id}, _ipc{ipc}, _con{ipc, ""_url} {
}

Str id() const override { return _id; }

Res<> activate(Sys::Context &ctx) override;

Async::Task<> runAsync();

Res<> dispatch(Sys::Message &msg) override;
};

struct Locator : public Endpoint {
Locator();

Str id() const;

virtual Res<> dispatch(Sys::Message &);
};

struct Bus : public Meta::Static {
Sys::Context &_context;

Expand Down
1 change: 1 addition & 0 deletions src/srvs/grund-bus/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {

auto system = co_try$(Bus::create(ctx));

co_try$(system->attach(makeStrong<Locator>()));
co_try$(system->startService("grund-av"s));
co_try$(system->startService("grund-conf"s));
co_try$(system->startService("grund-device"s));
Expand Down
5 changes: 4 additions & 1 deletion src/srvs/grund-dns/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <karm-sys/entry.h>
#include <karm-sys/ipc.h>

#include "../grund-bus/api.h"
#include "../grund-echo/api.h"

namespace Grund::Dns {
Expand All @@ -9,7 +10,9 @@ Async::Task<> serv(Sys::Context &ctx) {
Sys::Ipc ipc = Sys::Ipc::create(ctx);

logDebug("sending nonsens to system");
auto res = co_trya$(ipc.callAsync<Echo::Request>({}, "nonsens"s));
auto echoPort = co_trya$(ipc.callAsync<Bus::Locate>(Sys::Port::BUS, "grund-echo"s));

auto res = co_trya$(ipc.callAsync<Echo::Request>(echoPort, "nonsens"s));
logDebug("received response from system: ", res);

logInfo("service started");
Expand Down

0 comments on commit 489bc45

Please sign in to comment.