-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ Copyright (C) 2024 Claudio Carraro [email protected] | |
|
||
struct EnableReactiveMsg <: RembusMsg | ||
id::UInt128 | ||
msg_from::Float64 | ||
end | ||
|
||
#= | ||
|
@@ -241,10 +242,26 @@ function persist_messages(router) | |
close(db) | ||
end | ||
|
||
function start_reactive(twin::Twin) | ||
function start_reactive(twin::Twin, from_msg::Float64) | ||
# get the files with never sent messages | ||
allfiles = msg_files(twin.router) | ||
files = filter(t -> parse(Int, t) > twin.mark, allfiles) | ||
nowts = time() | ||
mdir = Rembus.messages_dir(twin.router) | ||
files = filter(allfiles) do fn | ||
if parse(Int, fn) <= twin.mark | ||
# the mesage was already delivered in a previous online session | ||
# of the component | ||
return false | ||
else | ||
ftime = mtime(joinpath(mdir, fn)) | ||
delta = nowts - ftime | ||
if delta * 1_000_000 > from_msg | ||
@info "skipping msg $fn: mtime: $(unix2datetime(ftime)) ($delta secs from now)" | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
twin.reactive = true | ||
if twin.hasname | ||
for fn in files | ||
|
@@ -1018,11 +1035,15 @@ function twin_task(self, twin) | |
if isa(twin.socket, WebSockets.WebSocket) | ||
close(twin.socket, WebSockets.CloseFrameBody(1008, "unexpected twin close")) | ||
end | ||
# forward the message counter to the last message received when online | ||
# because these messages get already a chance to be delivered. | ||
if twin.reactive | ||
twin.mark = twin.router.mcounter | ||
end | ||
end | ||
@debug "[$twin] task done" | ||
end | ||
|
||
|
||
#= | ||
handle_ack_timeout(tim, twin, msg, msgid) | ||
|
@@ -2214,7 +2235,7 @@ function broker(self, router) | |
#TBD: manage errors | ||
response = ResMsg(msg.content.id, STS_SUCCESS, nothing) | ||
put!(msg.twchannel.process.inbox, response) | ||
start_reactive(msg.twchannel) | ||
start_reactive(msg.twchannel, msg.content.msg_from) | ||
end | ||
else | ||
@warn "unknown message: $msg" | ||
|
@@ -2231,6 +2252,7 @@ function broker(self, router) | |
end | ||
filter!(router.id_twin) do (id, tw) | ||
cleanup(tw, router) | ||
return true | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
include("../utils.jl") | ||
|
||
using Dates | ||
|
||
mutable struct Ctx | ||
topic_1::Int | ||
end | ||
|
||
topic_1(ctx) = ctx.topic_1 += 1 | ||
|
||
function publish_msg() | ||
rb = connect() | ||
publish(rb, "topic_1") | ||
sleep(1) | ||
close(rb) | ||
end | ||
|
||
function run() | ||
ctx = Ctx(0) | ||
|
||
#rb = connect() | ||
#publish(rb, "topic_1") | ||
#sleep(1) | ||
#close(rb) | ||
|
||
myc = connect("myc") | ||
shared(myc, ctx) | ||
subscribe(myc, topic_1, msg_from=Second(1)) | ||
reactive(myc, msg_from=Now()) | ||
sleep(1) | ||
close(myc) | ||
|
||
# # Only named component may receive message from past ... | ||
# @component "myc" | ||
# @shared ctx | ||
# @subscribe topic_1 from = Second(1) | ||
# @reactive msg_from = Now() | ||
# sleep(1) | ||
# @terminate | ||
|
||
@test ctx.topic_1 == 0 | ||
end | ||
|
||
execute(() -> publish_msg(), "test_reactive::1") | ||
execute(() -> run(), "test_reactive::2", reset=false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters