0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-05-29 00:03:45 +02:00

ircd:Ⓜ️ Introduce the node and the node::room.

This commit is contained in:
Jason Volk 2018-03-05 08:53:48 -08:00
parent 17833dc22d
commit 63614107f8
5 changed files with 288 additions and 0 deletions

View file

@ -18,6 +18,7 @@ namespace ircd::m
extern struct user me;
extern struct room my_room;
extern struct node my_node;
extern struct room control;
extern struct log::log log;
extern std::list<ircd::net::listener> listeners;
@ -51,6 +52,7 @@ namespace ircd
#include "state.h"
#include "room.h"
#include "user.h"
#include "node.h"
#include "login.h"
#include "register.h"
#include "invite_3pid.h"

68
include/ircd/m/node.h Normal file
View file

@ -0,0 +1,68 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_M_NODE_H
namespace ircd::m
{
struct node;
extern const room nodes;
bool my(const node &);
bool exists(const id::node &);
node create(const id::node &, const json::members &args = {});
}
/// A node is an entity (lay: a server) participating in the matrix system. The
/// node may be a peer (ircd::server::peer) which is directly connected or it
/// may be indirect. The node may be a matrix "origin" or it may be a member
/// of a cluster perusing under the same origin.
///
/// First consider the node_id, which has the sigil ':'. A node which addresses
/// an origin as a whole has the mxid `::domain.tld` which has an empty
/// localpart. A node which is concerned with some entity within an origin has
/// an mxid `:somename:domain.tld`. This is essential for clustered multihoming
/// of our origin. Note that remote origins are supposed to be opaque, so there
/// is no real case for addressing a sub-entity other than ours.
///
struct ircd::m::node
{
struct room;
using id = m::id::node;
id node_id;
id::room room_id(const mutable_buffer &) const;
id::room::buf room_id() const;
node(const id &node_id)
:node_id{node_id}
{}
node() = default;
};
/// Every node has its own room acting as a database and log mechanism
/// for this node. This is similar to the user::room.
//
struct ircd::m::node::room
:m::room
{
m::node node;
id::room::buf room_id;
room(const m::node &node);
room(const m::node::id &node_id);
room() = default;
room(const room &) = delete;
room &operator=(const room &) = delete;
};

View file

@ -24,6 +24,10 @@ namespace ircd::m
static void join_ircd_room();
}
//
// my user
//
const ircd::m::user::id::buf
ircd_user_id
{
@ -36,6 +40,10 @@ ircd::m::me
ircd_user_id
};
//
// my room
//
const ircd::m::room::id::buf
ircd_room_id
{
@ -48,6 +56,26 @@ ircd::m::my_room
ircd_room_id
};
//
// my node
//
const ircd::m::node::id::buf
ircd_node_id
{
"", ircd::my_host() //TODO: hostname
};
ircd::m::node
ircd::m::my_node
{
ircd_node_id
};
//
// misc
//
const ircd::m::room::id::buf
control_room_id
{
@ -338,6 +366,111 @@ ircd::m::presence::valid_state(const string_view &state)
return function(state);
}
///////////////////////////////////////////////////////////////////////////////
//
// m/node.h
//
/// ID of the room which indexes all nodes (an instance of the room is
/// provided below).
const ircd::m::room::id::buf
nodes_room_id
{
"nodes", ircd::my_host()
};
/// The nodes room is the database of all nodes. It primarily serves as an
/// indexing mechanism and for top-level node related keys.
///
const ircd::m::room
ircd::m::nodes
{
nodes_room_id
};
ircd::m::node
ircd::m::create(const id::node &node_id,
const json::members &args)
{
using prototype = node (const id::node &, const json::members &);
static import<prototype> function
{
"s_node", "create_node"
};
assert(node_id);
return function(node_id, args);
}
bool
ircd::m::exists(const node::id &node_id)
{
using prototype = bool (const node::id &);
static import<prototype> function
{
"s_node", "exists__nodeid"
};
return function(node_id);
}
bool
ircd::m::my(const node &node)
{
return my(node.node_id);
}
//
// node
//
/// Generates a node-room ID into buffer; see room_id() overload.
ircd::m::id::room::buf
ircd::m::node::room_id()
const
{
ircd::m::id::room::buf buf;
return buf.assigned(room_id(buf));
}
/// This generates a room mxid for the "node's room" essentially serving as
/// a database mechanism for this specific node. This room_id is a hash of
/// the node's full mxid.
///
ircd::m::id::room
ircd::m::node::room_id(const mutable_buffer &buf)
const
{
assert(!empty(node_id));
const sha256::buf hash
{
sha256{node_id}
};
char b58[size(hash) * 2];
return
{
buf, b58encode(b58, hash), my_host()
};
}
//
// node::room
//
ircd::m::node::room::room(const m::node::id &node_id)
:room{m::node{node_id}}
{}
ircd::m::node::room::room(const m::node &node)
:node{node}
,room_id{node.room_id()}
{
static_cast<m::room &>(*this) = room_id;
}
///////////////////////////////////////////////////////////////////////////////
//
// m/user.h

View file

@ -45,11 +45,13 @@ s_moduledir = @moduledir@
s_console_la_SOURCES = s_console.cc
s_conf_la_SOURCES = s_conf.cc
s_control_la_SOURCES = s_control.cc
s_node_la_SOURCES = s_node.cc
s_module_LTLIBRARIES = \
s_console.la \
s_conf.la \
s_control.la \
s_node.la \
###
###############################################################################

83
modules/s_node.cc Normal file
View file

@ -0,0 +1,83 @@
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
using namespace ircd;
mapi::header
IRCD_MODULE
{
"Server nodes"
};
m::room
nodes_room
{
m::nodes.room_id
};
extern "C" m::node
create_node(const m::node::id &node_id,
const json::members &args)
{
const m::node node
{
node_id
};
const m::room::id::buf room_id
{
node.room_id()
};
//TODO: ABA
create(room_id, m::me.user_id);
send(nodes_room, m::me.user_id, "ircd.node", node_id, args);
return node;
}
extern "C" bool
exists__nodeid(const m::node::id &node_id)
{
return nodes_room.has("ircd.node", node_id);
}
static void
create_my_node_room(const m::event &)
{
create(m::my_node.room_id(), m::me.user_id);
}
const m::hook
create_my_node_hook
{
create_my_node_room,
{
{ "_site", "vm notify" },
{ "room_id", "!nodes" },
{ "type", "m.room.create" },
}
};
static void
create_nodes_room(const m::event &)
{
create(nodes_room, m::me.user_id);
}
const m::hook
create_nodes_hook
{
create_nodes_room,
{
{ "_site", "vm notify" },
{ "room_id", "!ircd" },
{ "type", "m.room.create" },
}
};