mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 21:48:21 +00:00
Support unserializing broker data into type 'any'
The receiver side will wrap the data as a Broker::Data value, which can then be type-checked/cast via 'is' or 'as' operators to a specific Bro type. For example: Sender: Broker::publish("topic", my_event, "hello") Receiver: event my_event(arg: any) { if ( arg is string ) print arg as string; }
This commit is contained in:
parent
bd3c16c6d7
commit
d873acc9e3
4 changed files with 133 additions and 0 deletions
|
@ -772,6 +772,9 @@ struct type_checker {
|
|||
|
||||
Val* bro_broker::data_to_val(broker::data d, BroType* type)
|
||||
{
|
||||
if ( type->Tag() == TYPE_ANY )
|
||||
return bro_broker::make_data_val(move(d));
|
||||
|
||||
return broker::visit(val_converter{type}, std::move(d));
|
||||
}
|
||||
|
||||
|
|
12
testing/btest/Baseline/broker.remote_event_any/recv.recv.out
Normal file
12
testing/btest/Baseline/broker.remote_event_any/recv.recv.out
Normal file
|
@ -0,0 +1,12 @@
|
|||
receiver added peer: endpoint=127.0.0.1 msg=handshake successful
|
||||
is_remote should be T, and is, T
|
||||
receiver got ping: my-message, 1
|
||||
is_remote should be T, and is, T
|
||||
receiver got ping: my-message, 2
|
||||
is_remote should be T, and is, T
|
||||
receiver got ping: my-message, 3
|
||||
is_remote should be T, and is, T
|
||||
receiver got ping: my-message, 4
|
||||
is_remote should be T, and is, T
|
||||
receiver got ping: my-message, 5
|
||||
[num_peers=1, num_stores=0, num_pending_queries=0, num_events_incoming=5, num_events_outgoing=4, num_logs_incoming=0, num_logs_outgoing=1, num_ids_incoming=0, num_ids_outgoing=0]
|
11
testing/btest/Baseline/broker.remote_event_any/send.send.out
Normal file
11
testing/btest/Baseline/broker.remote_event_any/send.send.out
Normal file
|
@ -0,0 +1,11 @@
|
|||
is_remote should be F, and is, F
|
||||
sender added peer: endpoint=127.0.0.1 msg=received handshake from remote core
|
||||
is_remote should be T, and is, T
|
||||
sender got pong: my-message, 1
|
||||
is_remote should be T, and is, T
|
||||
sender got pong: my-message, 2
|
||||
is_remote should be T, and is, T
|
||||
sender got pong: my-message, 3
|
||||
is_remote should be T, and is, T
|
||||
sender got pong: my-message, 4
|
||||
sender lost peer: endpoint=127.0.0.1 msg=lost remote peer
|
107
testing/btest/broker/remote_event_any.bro
Normal file
107
testing/btest/broker/remote_event_any.bro
Normal file
|
@ -0,0 +1,107 @@
|
|||
# @TEST-SERIALIZE: comm
|
||||
#
|
||||
# @TEST-EXEC: btest-bg-run recv "bro -B broker -b ../recv.bro >recv.out"
|
||||
# @TEST-EXEC: btest-bg-run send "bro -B broker -b ../send.bro >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.bro
|
||||
|
||||
redef Broker::default_connect_retry=1secs;
|
||||
redef Broker::default_listen_retry=1secs;
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
global event_count = 0;
|
||||
|
||||
global ping: event(msg: string, c: any);
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
Broker::subscribe("bro/event/my_topic");
|
||||
Broker::peer("127.0.0.1");
|
||||
print "is_remote should be F, and is", is_remote_event();
|
||||
}
|
||||
|
||||
function send_event()
|
||||
{
|
||||
++event_count;
|
||||
local e = Broker::make_event(ping, "my-message", event_count);
|
||||
Broker::publish("bro/event/my_topic", e);
|
||||
}
|
||||
|
||||
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
print fmt("sender added peer: endpoint=%s msg=%s",
|
||||
endpoint$network$address, msg);
|
||||
send_event();
|
||||
}
|
||||
|
||||
event Broker::peer_lost(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
print fmt("sender lost peer: endpoint=%s msg=%s",
|
||||
endpoint$network$address, msg);
|
||||
terminate();
|
||||
}
|
||||
|
||||
event pong(msg: string, n: any)
|
||||
{
|
||||
print "is_remote should be T, and is", is_remote_event();
|
||||
|
||||
if ( n is count )
|
||||
print fmt("sender got pong: %s, %s", msg, n as count);
|
||||
|
||||
send_event();
|
||||
}
|
||||
|
||||
@TEST-END-FILE
|
||||
|
||||
|
||||
@TEST-START-FILE recv.bro
|
||||
|
||||
redef Broker::default_connect_retry=1secs;
|
||||
redef Broker::default_listen_retry=1secs;
|
||||
redef exit_only_after_terminate = T;
|
||||
|
||||
const events_to_recv = 5;
|
||||
|
||||
global handler: event(msg: string, c: count);
|
||||
global auto_handler: event(msg: string, c: count);
|
||||
|
||||
global pong: event(msg: string, c: count);
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
Broker::subscribe("bro/event/my_topic");
|
||||
Broker::listen("127.0.0.1");
|
||||
}
|
||||
|
||||
event Broker::peer_added(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
print fmt("receiver added peer: endpoint=%s msg=%s", endpoint$network$address, msg);
|
||||
}
|
||||
|
||||
event Broker::peer_lost(endpoint: Broker::EndpointInfo, msg: string)
|
||||
{
|
||||
print fmt("receiver lost peer: endpoint=%s msg=%s", endpoint$network$address, msg);
|
||||
}
|
||||
|
||||
event ping(msg: string, n: any)
|
||||
{
|
||||
print "is_remote should be T, and is", is_remote_event();
|
||||
|
||||
if ( n is count )
|
||||
print fmt("receiver got ping: %s, %s", msg, n as count);
|
||||
|
||||
if ( (n as count) == events_to_recv )
|
||||
{
|
||||
print get_broker_stats();
|
||||
terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
Broker::publish("bro/event/my_topic", pong, msg, n as count);
|
||||
}
|
||||
|
||||
@TEST-END-FILE
|
Loading…
Add table
Add a link
Reference in a new issue