mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 16:18:19 +00:00

anonymous-functions, their closures, can now be sent over broker. In order to send an anonymous function the receiver must have parsed a definition of the functon, but it need not to have been evaluated. See testing/btest/language/closure-sending.zeek for an example of how this can be done. This also sends their closures as well as the closures of regular functions.
88 lines
2.1 KiB
Text
88 lines
2.1 KiB
Text
# @TEST-PORT: BROKER_PORT
|
|
#
|
|
# @TEST-EXEC: btest-bg-run recv "zeek -B broker -b ../recv.zeek >recv.out"
|
|
# @TEST-EXEC: btest-bg-run send "zeek -B broker -b ../send.zeek >send.out"
|
|
#
|
|
# @TEST-EXEC: btest-bg-wait 20
|
|
# @TEST-EXEC: btest-diff recv/recv.out
|
|
# @TEST-EXEC: btest-diff send/send.out
|
|
|
|
@TEST-START-FILE send.zeek
|
|
|
|
redef exit_only_after_terminate = T;
|
|
global event_count = 0;
|
|
type myfunctype: function(c: count);
|
|
function myfunc(c: count)
|
|
{
|
|
print fmt("bodiesdontsend(%s)", c);
|
|
}
|
|
global ping: event(msg: string, f: myfunctype);
|
|
event zeek_init()
|
|
{
|
|
Broker::subscribe("zeek/event/my_topic");
|
|
Broker::peer("127.0.0.1", 9999/tcp);
|
|
}
|
|
function send_event()
|
|
{
|
|
++event_count;
|
|
local e = Broker::make_event(ping, "my-message", myfunc);
|
|
Broker::publish("zeek/event/my_topic", e);
|
|
}
|
|
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
|
{
|
|
print "peer added";
|
|
send_event();
|
|
}
|
|
event Broker::peer_lost(endpoint: Broker::EndpointInfo, msg: string)
|
|
{
|
|
print "peer lost";
|
|
terminate();
|
|
}
|
|
event pong(msg: string, f: myfunctype)
|
|
{
|
|
print fmt("sender got pong: %s, %s", msg, f);
|
|
f(event_count);
|
|
send_event();
|
|
}
|
|
|
|
@TEST-END-FILE
|
|
|
|
@TEST-START-FILE recv.zeek
|
|
|
|
redef exit_only_after_terminate = T;
|
|
const events_to_recv = 5;
|
|
type myfunctype: function(c: count);
|
|
function myfunc(c: count)
|
|
{
|
|
print fmt("myfunc(%s)", c);
|
|
}
|
|
global pong: event(msg: string, f: myfunctype);
|
|
event zeek_init()
|
|
{
|
|
Broker::subscribe("zeek/event/my_topic");
|
|
Broker::listen("127.0.0.1", 9999/tcp);
|
|
}
|
|
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
|
{
|
|
print "peer added";
|
|
}
|
|
event Broker::peer_lost(endpoint: Broker::EndpointInfo, msg: string)
|
|
{
|
|
print "peer lost";
|
|
}
|
|
global n = 0;
|
|
event ping(msg: string, f: myfunctype)
|
|
{
|
|
print fmt("receiver got ping: %s, %s", msg, f);
|
|
++n;
|
|
f(n);
|
|
if ( n == events_to_recv )
|
|
terminate();
|
|
else
|
|
{
|
|
local e = Broker::make_event(pong, msg, f);
|
|
Broker::publish("zeek/event/my_topic", e);
|
|
}
|
|
}
|
|
|
|
@TEST-END-FILE
|