0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd:Ⓜ️ Rename most vm.notify hooks to vm.effect; Refactor vm::accept related.

This commit is contained in:
Jason Volk 2018-10-06 22:17:46 -07:00
parent e2bc5a5245
commit 9aa6e2b768
22 changed files with 249 additions and 153 deletions

View file

@ -19,12 +19,10 @@ namespace ircd::m::vm
struct opts;
struct copts;
struct eval;
struct accepted;
enum fault :uint;
using fault_t = std::underlying_type<fault>::type;
extern log::log log;
extern ctx::shared_view<accepted> accept;
extern uint64_t current_sequence;
extern const opts default_opts;
extern const copts default_copts;
@ -244,21 +242,6 @@ struct ircd::m::vm::copts
bool infolog_postcommit {false};
};
struct ircd::m::vm::accepted
:m::event
{
ctx::ctx *context;
const vm::opts *opts;
const vm::copts *copts;
const event::conforms *report;
shared_buffer<mutable_buffer> strung;
accepted(const m::event &event,
const vm::opts *const &opts,
const vm::copts *const &copts,
const event::conforms *const &report);
};
struct ircd::m::vm::error
:m::error
{

View file

@ -480,10 +480,6 @@ ircd::m::vm::log
"vm", 'v'
};
decltype(ircd::m::vm::accept)
ircd::m::vm::accept
{};
decltype(ircd::m::vm::current_sequence)
ircd::m::vm::current_sequence
{};
@ -699,22 +695,6 @@ ircd::m::vm::eval::operator()(const event &event)
return ret;
}
//
// accepted
//
ircd::m::vm::accepted::accepted(const m::event &event,
const vm::opts *const &opts,
const vm::copts *const &copts,
const event::conforms *const &report)
:m::event{event}
,context{ctx::current}
,opts{opts}
,copts{copts}
,report{report}
{
}
///////////////////////////////////////////////////////////////////////////////
//
// m/keys.h

View file

@ -253,16 +253,16 @@ room_alias_fetch(const mutable_buffer &out,
return response;
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_create_alias_room
{
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
},
[](const m::event &)
[](const m::event &, m::vm::eval &)
{
m::create(alias_room_id, m::me.user_id);
}

View file

@ -328,7 +328,8 @@ put__presence_status(client &client,
}
static void
handle_my_presence_changed(const m::event &event)
handle_my_presence_changed(const m::event &event,
m::vm::eval &eval)
{
if(!my(event))
return;
@ -349,12 +350,12 @@ handle_my_presence_changed(const m::event &event)
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
my_presence_changed
{
handle_my_presence_changed,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "ircd.presence" },
}
};

View file

@ -366,7 +366,8 @@ handle_my_profile_changed__avatar_url(const m::event &event)
}
static void
handle_my_profile_changed(const m::event &event)
handle_my_profile_changed(const m::event &event,
m::vm::eval &eval)
{
if(!my(event))
return;
@ -389,12 +390,12 @@ handle_my_profile_changed(const m::event &event)
return handle_my_profile_changed__avatar_url(event);
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
my_profile_changed
{
handle_my_profile_changed,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "ircd.profile" },
}
};

View file

@ -323,7 +323,8 @@ post__publicrooms_remote(client &client,
}
static void
_create_public_room(const m::event &)
_create_public_room(const m::event &,
m::vm::eval &)
{
m::create(public_room_id, m::me.user_id);
}
@ -331,12 +332,12 @@ _create_public_room(const m::event &)
/// Create the public rooms room at the appropriate time on startup.
/// The startup event chosen here is when @ircd joins the !ircd room,
/// which is a fundamental notification toward the end of init.
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_create_public_hook
{
_create_public_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
}

View file

@ -130,29 +130,59 @@ catch(const bad_lex_cast &e)
};
}
//
// longpoll
//
decltype(ircd::m::sync::longpoll::notified)
ircd::m::sync::longpoll::notified
{
handle_notify,
{
{ "_site", "vm.notify" },
}
};
void
ircd::m::sync::longpoll::handle_notify(const m::event &event,
m::vm::eval &eval)
{
assert(eval.opts);
if(!eval.opts->notify_clients)
return;
if(!polling)
return;
queue.emplace_back(eval);
dock.notify_all();
}
void
ircd::m::sync::longpoll::poll(client &client,
const args &args)
try
{
std::unique_lock<decltype(m::vm::accept)> lock
++polling;
const unwind unpoll{[]
{
m::vm::accept
};
--polling;
}};
while(1)
{
auto &accepted
{
m::vm::accept.wait_until(lock, args.timesout)
};
assert(accepted.opts);
if(!accepted.opts->notify_clients)
dock.wait_until(args.timesout);
if(queue.empty())
continue;
if(handle(client, args, accepted))
return;
const auto &a(queue.front());
const unwind pop{[]
{
queue.pop_front();
}};
if(handle(client, args, a))
break;
}
}
catch(const ctx::timeout &e)
@ -178,7 +208,7 @@ catch(const ctx::timeout &e)
bool
ircd::m::sync::longpoll::handle(client &client,
const args &args,
const vm::accepted &event)
const accepted &event)
{
const auto &room_id
{
@ -197,7 +227,7 @@ ircd::m::sync::longpoll::handle(client &client,
bool
ircd::m::sync::longpoll::handle(client &client,
const args &args,
const vm::accepted &event,
const accepted &event,
const m::room &room)
{
const m::user::id &user_id
@ -261,7 +291,7 @@ ircd::m::sync::longpoll::sync_rooms(client &client,
const m::user::id &user_id,
const m::room &room,
const args &args,
const vm::accepted &event)
const accepted &event)
{
std::vector<std::string> r;
std::vector<json::member> m;
@ -290,7 +320,7 @@ std::string
ircd::m::sync::longpoll::sync_room(client &client,
const m::room &room,
const args &args,
const vm::accepted &accepted)
const accepted &accepted)
{
const auto &since
{
@ -302,12 +332,12 @@ ircd::m::sync::longpoll::sync_room(client &client,
if(defined(json::get<"event_id"_>(event)))
{
json::strung strung(event);
if(accepted.copts && accepted.copts->client_txnid)
if(!!accepted.client_txnid)
strung = json::insert(strung, json::member
{
"unsigned", json::members
{
{ "transaction_id", accepted.copts->client_txnid }
{ "transaction_id", accepted.client_txnid }
}
});
@ -401,6 +431,10 @@ ircd::m::sync::longpoll::sync_room(client &client,
return json::strung(body);
}
//
// linear
//
bool
ircd::m::sync::linear::handle(client &client,
shortpoll &sp,
@ -578,6 +612,10 @@ ircd::m::sync::linear::handle(client &client,
return true;
}
//
// polylog
//
bool
ircd::m::sync::polylog::handle(client &client,
shortpoll &sp,

View file

@ -25,11 +25,44 @@ namespace ircd::m::sync
namespace ircd::m::sync::longpoll
{
static std::string sync_room(client &, const m::room &, const args &, const vm::accepted &);
static std::string sync_rooms(client &, const m::user::id &, const m::room &, const args &, const vm::accepted &);
static bool handle(client &, const args &, const vm::accepted &, const m::room &);
static bool handle(client &, const args &, const vm::accepted &);
struct accepted
:m::event
{
json::strung strung;
std::string client_txnid;
accepted(const m::vm::eval &eval)
:strung
{
*eval.event_
}
,client_txnid
{
eval.copts?
eval.copts->client_txnid:
string_view{}
}
{
const json::object object{this->strung};
static_cast<m::event &>(*this) = m::event{object};
}
accepted(accepted &&) = default;
accepted(const accepted &) = delete;
};
size_t polling {0};
std::deque<accepted> queue;
ctx::dock dock;
static std::string sync_room(client &, const m::room &, const args &, const accepted &);
static std::string sync_rooms(client &, const m::user::id &, const m::room &, const args &, const accepted &);
static bool handle(client &, const args &, const accepted &, const m::room &);
static bool handle(client &, const args &, const accepted &);
static void poll(client &, const args &);
static void handle_notify(const m::event &, m::vm::eval &);
extern m::hookfn<m::vm::eval &> notified;
}
namespace ircd::m::sync::linear

View file

@ -4163,8 +4163,8 @@ console_cmd__stage__broadcast(opt &out, const string_view &line)
{
const m::vm::opts opts;
const m::event event{stage.at(i)};
m::vm::accepted a{event, &opts, nullptr, &opts.report};
m::vm::accept(a);
//m::vm::accepted a{event, &opts, nullptr, &opts.report};
//m::vm::accept(a);
}
return true;
@ -4477,12 +4477,14 @@ console_cmd__event__sign(opt &out, const string_view &line)
if(op == "accept")
{
const m::vm::opts opts;
/*
m::vm::accepted a
{
event, &opts, nullptr, &opts.report
};
m::vm::accept(a);
*/
}
else if(op == "eval")
{
@ -7462,8 +7464,8 @@ console_cmd__feds__resend(opt &out, const string_view &line)
};
const m::vm::opts opts;
m::vm::accepted a{event, &opts, nullptr, &opts.report};
m::vm::accept(a);
// m::vm::accepted a{event, &opts, nullptr, &opts.report};
// m::vm::accept(a);
return true;
}

View file

@ -26,6 +26,8 @@ static void send(const m::event &, const m::room::id &room_id);
static void send(const m::event &);
static void send_worker();
static void handle_notify(const m::event &, m::vm::eval &);
context
sender
{
@ -51,32 +53,57 @@ IRCD_MODULE
}
};
std::deque<std::string>
notified_queue;
ctx::dock
notified_dock;
m::hookfn<m::vm::eval &>
notified
{
handle_notify,
{
{ "_site", "vm.notify" },
}
};
void
handle_notify(const m::event &event,
m::vm::eval &eval)
{
if(!my(event))
return;
assert(eval.opts);
if(!eval.opts->notify_servers)
return;
notified_queue.emplace_back(json::strung{event});
notified_dock.notify_all();
}
void
send_worker()
{
// In order to synchronize with the vm core, this context has to
// maintain this shared_lock at all times. If this is unlocked we
// can miss an event being broadcast.
std::unique_lock<decltype(m::vm::accept)> lock
{
m::vm::accept
};
while(1) try
{
// reference to the event on the inserter's stack
const auto &event
notified_dock.wait([]
{
m::vm::accept.wait(lock)
return !notified_queue.empty();
});
const unwind pop{[]
{
assert(!notified_queue.empty());
notified_queue.pop_front();
}};
const m::event event
{
json::object{notified_queue.front()}
};
if(!my(event))
continue;
assert(event.opts);
if(!event.opts->notify_servers)
continue;
send(event);
}
catch(const std::exception &e)

View file

@ -29,7 +29,8 @@ alias_room
};
void
_changed_aliases(const m::event &event)
_changed_aliases(const m::event &event,
m::vm::eval &)
{
const m::room::id &room_id
{
@ -61,12 +62,12 @@ _changed_aliases(const m::event &event)
}
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_changed_aliases_hookfn
{
_changed_aliases,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.aliases" },
}
};

View file

@ -29,7 +29,8 @@ alias_room
};
void
_changed_canonical_alias(const m::event &event)
_changed_canonical_alias(const m::event &event,
m::vm::eval &)
{
const m::room::alias &alias
{
@ -57,12 +58,12 @@ _changed_canonical_alias(const m::event &event)
};
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_changed_canonical_alias_hookfn
{
_changed_canonical_alias,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.canonical_alias" },
}
};

View file

@ -48,7 +48,8 @@ _can_create_room_hookfn
};
static void
_created_room(const m::event &event)
_created_room(const m::event &event,
m::vm::eval &)
{
const m::room::id &room_id
{
@ -64,12 +65,12 @@ _created_room(const m::event &event)
send(m::my_room, at<"sender"_>(event), "ircd.room", room_id, json::object{});
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_created_room_hookfn
{
_created_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.create" },
}
};

View file

@ -137,7 +137,8 @@ visible(const m::event &event,
}
static void
_changed_visibility(const m::event &event)
_changed_visibility(const m::event &event,
m::vm::eval &)
{
log::info
{
@ -149,12 +150,12 @@ _changed_visibility(const m::event &event)
};
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_changed_visibility_hookfn
{
_changed_visibility,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.history_visibility" },
}
};

View file

@ -17,7 +17,8 @@ IRCD_MODULE
};
static void
_changed_rules(const m::event &event)
_changed_rules(const m::event &event,
m::vm::eval &)
{
const m::user::id &sender
{
@ -40,12 +41,12 @@ _changed_rules(const m::event &event)
send(public_room, sender, "ircd.room", room_id, json::strung{event});
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_changed_rules_hookfn
{
_changed_rules,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.join_rules" },
}
};

View file

@ -17,7 +17,8 @@ IRCD_MODULE
};
static void
affect_user_room(const m::event &event)
affect_user_room(const m::event &event,
m::vm::eval &eval)
{
const auto &room_id
{
@ -43,11 +44,11 @@ affect_user_room(const m::event &event)
send(user_room, sender, "ircd.member", room_id, at<"content"_>(event));
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
affect_user_room_hookfn
{
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.member" },
},
affect_user_room
@ -72,16 +73,17 @@ _can_join_room_hookfn
};
static void
_join_room(const m::event &event)
_join_room(const m::event &event,
m::vm::eval &eval)
{
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
_join_room_hookfn
{
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "type", "m.room.member" },
{ "membership", "join" },
},

View file

@ -148,19 +148,26 @@ catch(const std::exception &e)
};
}
const m::hookfn<>
static void
handle_conf_updated(const m::event &event,
m::vm::eval &)
{
conf_updated(event);
}
const m::hookfn<m::vm::eval &>
conf_updated_hook
{
conf_updated,
handle_conf_updated,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!conf" },
{ "type", "ircd.conf.item" },
}
};
static void
init_conf_items(const m::event &)
init_conf_items()
{
const m::room::state state
{
@ -189,12 +196,19 @@ init_conf_item(conf::item<> &item)
});
}
const m::hookfn<>
static void
handle_init_conf_items(const m::event &,
m::vm::eval &eval)
{
init_conf_items();
}
const m::hookfn<m::vm::eval &>
init_conf_items_hook
{
init_conf_items,
handle_init_conf_items,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.member" },
{ "membership", "join" },
@ -228,7 +242,8 @@ catch(const std::exception &e)
}
static void
create_conf_room(const m::event &)
create_conf_room(const m::event &,
m::vm::eval &)
{
m::create(conf_room_id, m::me.user_id);
@ -240,12 +255,12 @@ create_conf_room(const m::event &)
}
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_conf_room_hook
{
create_conf_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
}
@ -274,7 +289,7 @@ rehash_conf(const bool &existing)
void
reload_conf()
{
init_conf_items(m::event{});
init_conf_items();
}
void

View file

@ -41,7 +41,8 @@ _cmd__die(const m::event &event,
}
static void
command_control(const m::event &event)
command_control(const m::event &event,
m::vm::eval &)
noexcept try
{
const auto &content
@ -105,12 +106,12 @@ catch(const std::exception &e)
notice(control_room, e.what());
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
command_control_hook
{
command_control,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!control" },
{ "type", "m.room.message" },
{ "content",
@ -121,7 +122,8 @@ command_control_hook
};
static void
create_control_room(const m::event &)
create_control_room(const m::event &,
m::vm::eval &)
{
create(control_room_id, m::me.user_id);
join(control_room, m::me.user_id);
@ -134,12 +136,12 @@ create_control_room(const m::event &)
notice(control_room, m::me.user_id, "I am the daemon. You can talk to me in this room by highlighting me.");
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_control_hook
{
create_control_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
}

View file

@ -17,7 +17,7 @@ extern "C" bool verify__keys(const m::keys &) noexcept;
extern "C" void get__keys(const string_view &server, const string_view &key_id, const m::keys::closure &);
extern "C" bool query__keys(const string_view &query_server, const m::keys::queries &, const m::keys::closure_bool &);
extern "C" void create_my_key(const m::event &);
extern "C" void create_my_key(const m::event &, m::vm::eval &);
static void init_my_ed25519();
static void init_my_tls_crt();
extern "C" void init_my_keys();
@ -276,19 +276,20 @@ init_my_ed25519()
};
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_my_key_hook
{
create_my_key,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", m::my_node.room_id() },
{ "type", "m.room.create" },
}
};
void
create_my_key(const m::event &)
create_my_key(const m::event &,
m::vm::eval &)
{
const json::members verify_keys_
{{

View file

@ -80,18 +80,19 @@ init_listeners()
//
static void
create_listener(const m::event &event)
create_listener(const m::event &event,
m::vm::eval &)
{
load_listener(event);
}
/// Hook for a new listener description being sent.
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_listener_hook
{
create_listener,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "ircd.listen" },
}

View file

@ -49,34 +49,36 @@ exists__nodeid(const m::node::id &node_id)
}
static void
create_my_node_room(const m::event &)
create_my_node_room(const m::event &,
m::vm::eval &)
{
create(m::my_node.room_id(), m::me.user_id);
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_my_node_hook
{
create_my_node_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!nodes" },
{ "type", "m.room.create" },
}
};
static void
create_nodes_room(const m::event &)
create_nodes_room(const m::event &,
m::vm::eval &)
{
create(nodes_room, m::me.user_id);
}
const m::hookfn<>
const m::hookfn<m::vm::eval &>
create_nodes_hook
{
create_nodes_room,
{
{ "_site", "vm.notify" },
{ "_site", "vm.effect" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
}

View file

@ -13,7 +13,8 @@ namespace ircd::m::vm
extern hook::site<eval &> commit_hook; ///< Called when this server issues event
extern hook::site<eval &> fetch_hook; ///< Called to resolve dependencies
extern hook::site<eval &> eval_hook; ///< Called when evaluating event
extern hook::site<> notify_hook; ///< Called after successful evaluation
extern hook::site<eval &> notify_hook; ///< Called to broadcast successful eval
extern hook::site<eval &> effect_hook; ///< Called to apply effects of eval
static void write_commit(eval &);
static fault _eval_edu(eval &, const event &);
@ -58,6 +59,12 @@ ircd::m::vm::notify_hook
{ "name", "vm.notify" }
};
decltype(ircd::m::vm::effect_hook)
ircd::m::vm::effect_hook
{
{ "name", "vm.effect" }
};
//
// init
//
@ -466,16 +473,11 @@ try
if(ret != fault::ACCEPT)
return ret;
vm::accepted accepted
{
event, &opts, eval.copts, &report
};
if(opts.notify)
notify_hook(event, eval);
if(opts.effects)
notify_hook(event);
if(opts.notify)
vm::accept(accepted);
effect_hook(event, eval);
if(opts.debuglog_accept)
log::debug