Port broker::data variant usages to use CAF API directly

Old code still all worked, but made use of Broker functions which
now just redirect to CAF ones.
This commit is contained in:
Jon Siwek 2018-07-17 14:20:19 -05:00
parent 4c072409f0
commit 9caad8a042
8 changed files with 125 additions and 118 deletions

View file

@ -1,4 +1,9 @@
2.5-743 | 2018-07-17 14:20:19 -0500
* Port broker::data variant usages to use CAF API directly
(Jon Siwek, Corelight)
2.5-741 | 2018-07-16 16:06:02 -0500
* Improve Specific_RE_Matcher::CompileSet() error condition cleanup

View file

@ -1 +1 @@
2.5-741
2.5-743

@ -1 +1 @@
Subproject commit 1d90b931cc888e31b0d4774dbae54f5758841ab3
Subproject commit 467024ab09e3443798126e69054fb8862c69562f

View file

@ -215,7 +215,7 @@ struct val_converter {
{
auto expected_index_types = tt->Indices()->Types();
broker::vector composite_key;
auto indices = broker::get_if<broker::vector>(item);
auto indices = caf::get_if<broker::vector>(&item);
if ( indices )
{
@ -283,7 +283,7 @@ struct val_converter {
{
auto expected_index_types = tt->Indices()->Types();
broker::vector composite_key;
auto indices = broker::get_if<broker::vector>(item.first);
auto indices = caf::get_if<broker::vector>(&item.first);
if ( indices )
{
@ -384,7 +384,7 @@ struct val_converter {
return nullptr;
}
if ( broker::get_if<broker::none>(a[idx]) != nullptr )
if ( caf::get_if<broker::none>(&a[idx]) != nullptr )
{
rval->Assign(i, nullptr);
++idx;
@ -411,8 +411,8 @@ struct val_converter {
if ( a.size() != 2 )
return nullptr;
auto exact_text = broker::get_if<std::string>(a[0]);
auto anywhere_text = broker::get_if<std::string>(a[1]);
auto exact_text = caf::get_if<std::string>(&a[0]);
auto anywhere_text = caf::get_if<std::string>(&a[1]);
if ( ! exact_text || ! anywhere_text )
return nullptr;
@ -582,7 +582,7 @@ struct type_checker {
for ( const auto& item : a )
{
auto expected_index_types = tt->Indices()->Types();
auto indices = broker::get_if<broker::vector>(item);
auto indices = caf::get_if<broker::vector>(&item);
vector<const broker::data*> indices_to_check;
if ( indices )
@ -624,7 +624,7 @@ struct type_checker {
auto expect = (*expected_index_types)[i];
auto& index_to_check = *(indices_to_check)[i];
if ( ! broker::visit(type_checker{expect}, index_to_check) )
if ( ! caf::visit(type_checker{expect}, index_to_check) )
return false;
}
}
@ -642,7 +642,7 @@ struct type_checker {
for ( auto& item : a )
{
auto expected_index_types = tt->Indices()->Types();
auto indices = broker::get_if<broker::vector>(item.first);
auto indices = caf::get_if<broker::vector>(&item.first);
vector<const broker::data*> indices_to_check;
if ( indices )
@ -689,11 +689,11 @@ struct type_checker {
auto expect = (*expected_index_types)[i];
auto& index_to_check = *(indices_to_check)[i];
if ( ! broker::visit(type_checker{expect}, index_to_check) )
if ( ! caf::visit(type_checker{expect}, index_to_check) )
return false;
}
if ( ! broker::visit(type_checker{tt->YieldType()},
if ( ! caf::visit(type_checker{tt->YieldType()},
item.second) )
return false;
}
@ -709,7 +709,7 @@ struct type_checker {
for ( auto& item : a )
{
if ( ! broker::visit(type_checker{vt->YieldType()}, item) )
if ( ! caf::visit(type_checker{vt->YieldType()}, item) )
return false;
}
@ -725,13 +725,13 @@ struct type_checker {
if ( idx >= a.size() )
return false;
if ( broker::get_if<broker::none>(a[idx]) != nullptr )
if ( caf::get_if<broker::none>(&a[idx]) != nullptr )
{
++idx;
continue;
}
if ( ! broker::visit(type_checker{rt->FieldType(i)},
if ( ! caf::visit(type_checker{rt->FieldType(i)},
a[idx]) )
return false;
@ -745,8 +745,8 @@ struct type_checker {
if ( a.size() != 2 )
return false;
auto exact_text = broker::get_if<std::string>(a[0]);
auto anywhere_text = broker::get_if<std::string>(a[1]);
auto exact_text = caf::get_if<std::string>(&a[0]);
auto anywhere_text = caf::get_if<std::string>(&a[1]);
if ( ! exact_text || ! anywhere_text )
return false;
@ -775,7 +775,7 @@ 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));
return caf::visit(val_converter{type}, std::move(d));
}
broker::expected<broker::data> bro_broker::val_to_data(Val* v)
@ -900,7 +900,7 @@ broker::expected<broker::data> bro_broker::val_to_data(Val* v)
key = move(composite_key);
if ( is_set )
broker::get<broker::set>(rval).emplace(move(key));
caf::get<broker::set>(rval).emplace(move(key));
else
{
auto val = val_to_data(entry->Value());
@ -908,7 +908,7 @@ broker::expected<broker::data> bro_broker::val_to_data(Val* v)
if ( ! val )
return broker::ec::invalid_data;
broker::get<broker::table>(rval).emplace(move(key), move(*val));
caf::get<broker::table>(rval).emplace(move(key), move(*val));
}
}
@ -1115,7 +1115,7 @@ struct data_type_getter {
EnumVal* bro_broker::get_data_type(RecordVal* v, Frame* frame)
{
return broker::visit(data_type_getter{}, opaque_field_to_data(v, frame));
return caf::visit(data_type_getter{}, opaque_field_to_data(v, frame));
}
broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f)
@ -1131,7 +1131,7 @@ broker::data& bro_broker::opaque_field_to_data(RecordVal* v, Frame* f)
bool bro_broker::DataVal::canCastTo(BroType* t) const
{
return broker::visit(type_checker{t}, data);
return caf::visit(type_checker{t}, data);
}
Val* bro_broker::DataVal::castTo(BroType* t)
@ -1192,24 +1192,24 @@ broker::data bro_broker::threading_field_to_data(const threading::Field* f)
threading::Field* bro_broker::data_to_threading_field(broker::data d)
{
if ( ! broker::is<broker::vector>(d) )
if ( ! caf::holds_alternative<broker::vector>(d) )
return nullptr;
auto& v = broker::get<broker::vector>(d);
auto name = broker::get_if<std::string>(v[0]);
auto& v = caf::get<broker::vector>(d);
auto name = caf::get_if<std::string>(&v[0]);
auto secondary = v[1];
auto type = broker::get_if<broker::count>(v[2]);
auto subtype = broker::get_if<broker::count>(v[3]);
auto optional = broker::get_if<broker::boolean>(v[4]);
auto type = caf::get_if<broker::count>(&v[2]);
auto subtype = caf::get_if<broker::count>(&v[3]);
auto optional = caf::get_if<broker::boolean>(&v[4]);
if ( ! (name && type && subtype && optional) )
return nullptr;
if ( secondary != broker::nil && ! broker::is<std::string>(secondary) )
if ( secondary != broker::nil && ! caf::holds_alternative<std::string>(secondary) )
return nullptr;
return new threading::Field(name->c_str(),
secondary != broker::nil ? broker::get<std::string>(secondary).c_str() : nullptr,
secondary != broker::nil ? caf::get<std::string>(secondary).c_str() : nullptr,
static_cast<TypeTag>(*type),
static_cast<TypeTag>(*subtype),
*optional);

View file

@ -210,11 +210,11 @@ broker::data& opaque_field_to_data(RecordVal* v, Frame* f);
template <typename T>
T& require_data_type(broker::data& d, TypeTag tag, Frame* f)
{
auto ptr = broker::get_if<T>(d);
auto ptr = caf::get_if<T>(&d);
if ( ! ptr )
reporter->RuntimeError(f->GetCall()->GetLocationInfo(),
"data is of type '%s' not of type '%s'",
broker::visit(type_name_getter{tag}, d),
caf::visit(type_name_getter{tag}, d),
type_name(tag));
return *ptr;

View file

@ -942,13 +942,13 @@ void Manager::Process()
{
had_input = true;
if ( auto stat = broker::get_if<broker::status>(status_msg) )
if ( auto stat = caf::get_if<broker::status>(&status_msg) )
{
ProcessStatus(std::move(*stat));
continue;
}
if ( auto err = broker::get_if<broker::error>(status_msg) )
if ( auto err = caf::get_if<broker::error>(&status_msg) )
{
ProcessError(std::move(*err));
continue;
@ -1048,7 +1048,7 @@ void Manager::ProcessRelayEvent(broker::bro::RelayEvent ev)
++statistics.num_events_incoming;
for ( auto& t : ev.topics() )
PublishEvent(std::move(broker::get<std::string>(t)),
PublishEvent(std::move(caf::get<std::string>(t)),
std::move(ev.name()),
std::move(ev.args()));
}
@ -1060,7 +1060,7 @@ void Manager::ProcessHandleAndRelayEvent(broker::bro::HandleAndRelayEvent ev)
ProcessEvent(ev.name(), ev.args());
for ( auto& t : ev.topics() )
PublishEvent(std::move(broker::get<std::string>(t)),
PublishEvent(std::move(caf::get<std::string>(t)),
std::move(ev.name()),
std::move(ev.args()));
}
@ -1095,20 +1095,25 @@ bool bro_broker::Manager::ProcessLogCreate(broker::bro::LogCreate lc)
}
// Get log fields.
auto fields_data = caf::get_if<broker::vector>(&lc.fields_data());
try
if ( ! fields_data )
{
auto fields_data = std::move(broker::get<broker::vector>(lc.fields_data()));
auto num_fields = fields_data.size();
reporter->Warning("failed to unpack remote log fields");
return false;
}
auto num_fields = fields_data->size();
auto fields = new threading::Field* [num_fields];
for ( auto i = 0u; i < num_fields; ++i )
{
if ( auto field = data_to_threading_field(std::move(fields_data[i])) )
if ( auto field = data_to_threading_field(std::move((*fields_data)[i])) )
fields[i] = field;
else
{
reporter->Warning("failed to convert remote log field # %d", i);
delete [] fields;
return false;
}
}
@ -1124,13 +1129,6 @@ bool bro_broker::Manager::ProcessLogCreate(broker::bro::LogCreate lc)
return true;
}
catch (const broker::bad_variant_access& e)
{
reporter->Warning("failed to unpack remote log fields");
return false;
}
}
bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw)
{
DBG_LOG(DBG_BROKER, "Received log-write: %s", RenderMessage(lw).c_str());
@ -1159,14 +1157,24 @@ bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw)
}
unref_guard writer_id_unreffer{writer_id};
auto path = caf::get_if<std::string>(&lw.path());
try
if ( ! path )
{
auto& path = broker::get<std::string>(lw.path());
auto& serial_data = broker::get<std::string>(lw.serial_data());
reporter->Warning("failed to unpack remote log values (bad path variant) for stream: %s", stream_id_name.data());
return false;
}
auto serial_data = caf::get_if<std::string>(&lw.serial_data());
if ( ! serial_data )
{
reporter->Warning("failed to unpack remote log values (bad serial_data variant) for stream: %s", stream_id_name.data());
return false;
}
BinarySerializationFormat fmt;
fmt.StartRead(serial_data.data(), serial_data.size());
fmt.StartRead(serial_data->data(), serial_data->size());
int num_fields;
bool success = fmt.Read(&num_fields, "num_fields");
@ -1195,18 +1203,12 @@ bool bro_broker::Manager::ProcessLogWrite(broker::bro::LogWrite lw)
}
}
log_mgr->WriteFromRemote(stream_id->AsEnumVal(), writer_id->AsEnumVal(), std::move(path), num_fields, vals);
log_mgr->WriteFromRemote(stream_id->AsEnumVal(), writer_id->AsEnumVal(),
std::move(*path), num_fields, vals);
fmt.EndRead();
return true;
}
catch ( const broker::bad_variant_access& e)
{
reporter->Warning("failed to unpack remote log values (bad variant) for stream: %s", stream_id_name.data());
return false;
}
}
bool Manager::ProcessIdentifierUpdate(broker::bro::IdentifierUpdate iu)
{
DBG_LOG(DBG_BROKER, "Received id-update: %s", RenderMessage(iu).c_str());

View file

@ -457,7 +457,7 @@ function Broker::__record_lookup%(r: Broker::Data, idx: count%): Broker::Data
auto& v = bro_broker::require_data_type<broker::vector>(r->AsRecordVal(),
TYPE_RECORD, frame);
if ( idx >= v.size() || broker::get_if<broker::none>(v[idx]) )
if ( idx >= v.size() || caf::get_if<broker::none>(&v[idx]) )
return new RecordVal(BifType::Record::Broker::Data);
return bro_broker::make_data_val(v[idx]);
@ -496,7 +496,7 @@ function Broker::__record_iterator_value%(it: opaque of Broker::RecordIterator%)
return rval;
}
if ( broker::get_if<broker::none>(*ri->it) )
if ( caf::get_if<broker::none>(&(*ri->it)) )
return rval; // field isn't set
rval->Assign(0, new bro_broker::DataVal(*ri->it));

View file

@ -137,15 +137,15 @@ broker::data WriterBackend::WriterInfo::ToBroker() const
bool WriterBackend::WriterInfo::FromBroker(broker::data d)
{
if ( ! broker::is<broker::vector>(d) )
if ( ! caf::holds_alternative<broker::vector>(d) )
return false;
auto v = broker::get<broker::vector>(d);
auto bpath = broker::get_if<std::string>(v[0]);
auto brotation_base = broker::get_if<double>(v[1]);
auto brotation_interval = broker::get_if<double>(v[2]);
auto bnetwork_time = broker::get_if<double>(v[3]);
auto bconfig = broker::get_if<broker::table>(v[4]);
auto v = caf::get<broker::vector>(d);
auto bpath = caf::get_if<std::string>(&v[0]);
auto brotation_base = caf::get_if<double>(&v[1]);
auto brotation_interval = caf::get_if<double>(&v[2]);
auto bnetwork_time = caf::get_if<double>(&v[3]);
auto bconfig = caf::get_if<broker::table>(&v[4]);
if ( ! (bpath && brotation_base && brotation_interval && bnetwork_time && bconfig) )
return false;
@ -157,8 +157,8 @@ bool WriterBackend::WriterInfo::FromBroker(broker::data d)
for ( auto i : *bconfig )
{
auto k = broker::get_if<std::string>(i.first);
auto v = broker::get_if<std::string>(i.second);
auto k = caf::get_if<std::string>(&i.first);
auto v = caf::get_if<std::string>(&i.second);
if ( ! (k && v) )
return false;