Node announce handling.

This commit is contained in:
Mark Qvist 2021-08-26 16:17:01 +02:00
parent 862f4835c7
commit ae3d2ef802
5 changed files with 52 additions and 7 deletions

View File

@ -26,6 +26,8 @@ class Conversation:
# Add the announce to the directory announce
# stream logger
# TODO: REMOVE
RNS.log("Received LXMF announce from: "+destination_hash_text)
app.directory.lxmf_announce_received(destination_hash, app_data)
@staticmethod

View File

@ -7,12 +7,24 @@ import RNS.vendor.umsgpack as msgpack
class Directory:
ANNOUNCE_STREAM_MAXLENGTH = 64
aspect_filter = "nomadnetwork.node"
@staticmethod
def received_announce(destination_hash, announced_identity, app_data):
app = nomadnet.NomadNetworkApp.get_shared_instance()
destination_hash_text = RNS.hexrep(destination_hash, delimit=False)
# TODO: REMOVE
RNS.log("Received node announce from: "+destination_hash_text)
app.directory.lxmf_announce_received(destination_hash, app_data)
def __init__(self, app):
self.directory_entries = {}
self.announce_stream = []
self.app = app
self.load_from_disk()
def save_to_disk(self):
try:
packed_list = []
@ -49,20 +61,31 @@ class Directory:
entries[e[0]] = DirectoryEntry(e[0], e[1], e[2], hosts_node)
self.directory_entries = entries
self.announce_stream = unpacked_directory["announce_stream"]
# TODO: Revert back to this simpler method instead of checking
# for the old format
# self.announce_stream = unpacked_directory["announce_stream"]
for entry in unpacked_directory["announce_stream"]:
RNS.log(str(entry))
if len(entry) < 4:
entry[3] = False
self.announce_stream.append(entry)
except Exception as e:
RNS.log("Could not load directory from disk. The contained exception was: "+str(e), RNS.LOG_ERROR)
def lxmf_announce_received(self, source_hash, app_data):
timestamp = time.time()
self.announce_stream.insert(0, (timestamp, source_hash, app_data))
self.announce_stream.insert(0, (timestamp, source_hash, app_data, False))
while len(self.announce_stream) > Directory.ANNOUNCE_STREAM_MAXLENGTH:
self.announce_stream.pop()
def node_announce_received(self, source_hash, app_data):
timestamp = time.time()
self.announce_stream.insert(0, (timestamp, source_hash, app_data))
self.announce_stream.insert(0, (timestamp, source_hash, app_data, True))
while len(self.announce_stream) > Directory.ANNOUNCE_STREAM_MAXLENGTH:
self.announce_stream.pop()

View File

@ -154,8 +154,6 @@ class NomadNetworkApp:
app_data=None
)
RNS.Transport.register_announce_handler(nomadnet.Conversation)
RNS.log("LXMF Router ready to receive on: "+RNS.prettyhexrep(self.lxmf_destination.hash))
self.directory = nomadnet.Directory.Directory(self)
@ -165,6 +163,9 @@ class NomadNetworkApp:
else:
self.node = None
RNS.Transport.register_announce_handler(nomadnet.Conversation)
RNS.Transport.register_announce_handler(nomadnet.Directory)
nomadnet.ui.spawn(self.uimode)
def set_display_name(self, display_name):

View File

@ -66,6 +66,8 @@ GLYPHS = {
("warning", "!", "\u26a0", "\uf12a"),
("info", "i", "\u2139", "\ufb4d"),
("divider1", "-", "\u2504", "\u2504"),
("peer", "P", "\U0001F464", "\uf415"),
("node", "N", "\U0001F5A5", "\uf502"),
("decoration_menu", "", "", " \uf93a"),
}

View File

@ -1,6 +1,7 @@
import RNS
import urwid
import nomadnet
import time
from datetime import datetime
from nomadnet.Directory import DirectoryEntry
from nomadnet.vendor.additional_urwid_widgets import IndicativeListBox, MODIFIER_KEY
@ -133,15 +134,26 @@ class AnnounceInfo(urwid.WidgetWrap):
class AnnounceStreamEntry(urwid.WidgetWrap):
def __init__(self, app, announce):
full_time_format = "%Y-%m-%d %H:%M:%S"
date_time_format = "%Y-%m-%d"
time_time_format = "%H:%M:%S"
short_time_format = "%Y-%m-%d %H:%M"
timestamp = announce[0]
source_hash = announce[1]
is_node = announce[3]
self.app = app
self.timestamp = timestamp
time_format = app.time_format
dt = datetime.fromtimestamp(self.timestamp)
ts_string = dt.strftime(time_format)
dtn = datetime.fromtimestamp(time.time())
g = self.app.ui.glyphs
if dt.strftime(date_time_format) == dtn.strftime(date_time_format):
ts_string = dt.strftime(time_time_format)
else:
ts_string = dt.strftime(short_time_format)
trust_level = self.app.directory.trust_level(source_hash)
display_str = self.app.directory.simplest_display_str(source_hash)
@ -166,7 +178,12 @@ class AnnounceStreamEntry(urwid.WidgetWrap):
style = "list_untrusted"
focus_style = "list_focus_untrusted"
widget = ListEntry(ts_string+" "+display_str)
if is_node:
type_symbol = g["node"]
else:
type_symbol = g["peer"]
widget = ListEntry(ts_string+" "+type_symbol+" "+display_str)
urwid.connect_signal(widget, "click", self.display_announce, announce)
self.display_widget = urwid.AttrMap(widget, style, focus_style)