Skip to content

Commit

Permalink
Made changes as recommended in PR review by gavv
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrick87 committed Nov 13, 2023
1 parent f7bb157 commit 3197896
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
26 changes: 10 additions & 16 deletions src/internal_modules/roc_address/print_supported.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Roc Streaming authors
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -16,13 +16,11 @@ namespace address {

namespace {

enum { ArraySize = 100, LineSize = 70 };
enum { LineSize = 70 };

void print_string_list(core::Printer& prn,
Interface interface,
const core::StringList& list,
const char* prefix,
const char* suffix) {
void print_interface_protos(core::Printer& prn,
Interface interface,
const core::StringList& list) {
const char* str = list.front();

while (str != NULL) {
Expand All @@ -33,7 +31,7 @@ void print_string_list(core::Printer& prn,
prn.writef(" %s:", interface_to_str(interface));

while (size < LineSize) {
size += prn.writef(" %s%s%s", prefix, str, suffix);
size += prn.writef(" %s%s%s", "", str, "://");

str = list.nextof(str);
if (!str) {
Expand All @@ -52,26 +50,22 @@ bool print_supported(ProtocolMap& protocol_map, core::IArena& arena) {
core::Array<Interface> interface_array(arena);
core::StringList list(arena);

if (!interface_array.resize(Iface_Max)) {
return false;
}

if (!protocol_map.get_supported_interfaces(interface_array)) {
roc_log(LogError, "can't retrieve interface array");
return false;
}

for (size_t x = 0; x < interface_array.size(); x++) {
if (!protocol_map.get_supported_protocols(interface_array[x], list)) {
for (size_t n_interface = 0; n_interface < interface_array.size(); n_interface++) {
if (!protocol_map.get_supported_protocols(interface_array[n_interface], list)) {
roc_log(LogError, "can't retrieve protocols list");
return false;
}

if (x == 0) {
if (n_interface == 0) {
prn.writef("\nsupported network protocols:\n");
}

print_string_list(prn, interface_array[x], list, "", "://");
print_interface_protos(prn, interface_array[n_interface], list);
}
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal_modules/roc_address/print_supported.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 2019 Roc Streaming authors
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

//! @file roc_address/print_supported.h
//! @brief Print supported schemes and formats.
//! @brief Print supported interfaces and protocols.

#ifndef ROC_ADDRESS_PRINT_SUPPORTED_H_
#define ROC_ADDRESS_PRINT_SUPPORTED_H_
Expand All @@ -19,7 +19,7 @@
namespace roc {
namespace address {

//! Print supported schemes and formats.
//! Print supported interfaces and protocols.
ROC_ATTR_NODISCARD bool print_supported(ProtocolMap&, core::IArena&);

} // namespace address
Expand Down
49 changes: 30 additions & 19 deletions src/internal_modules/roc_address/protocol_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,22 @@ const ProtocolAttrs* ProtocolMap::find_by_scheme(const char* scheme) const {
return NULL;
}

void ProtocolMap::add_proto_(const ProtocolAttrs& proto) {
roc_panic_if((int)proto.protocol < 0);
roc_panic_if((int)proto.protocol >= MaxProtos);
roc_panic_if(protos_[proto.protocol].protocol != 0);

protos_[proto.protocol] = proto;
}

bool ProtocolMap::get_supported_interfaces(core::Array<Interface>& interface_array) {
interface_array.clear();
bool interfaces_exist = false;

for (unsigned x = (unsigned)Iface_Consolidated + 1; x != (unsigned)Iface_Max; x++) {
for (size_t y = 0; y < MaxProtos; y++) {
if (x == (unsigned)protos_[y].iface) {
interface_array.push_back(protos_[y].iface);
for (unsigned n_iface = (unsigned)Iface_Consolidated; n_iface != (unsigned)Iface_Max;
n_iface++) {
for (size_t n_proto = 0; n_proto < MaxProtos; n_proto++) {
if (protos_[n_proto].protocol == Proto_None) {
continue;
}

if (n_iface == (unsigned)protos_[n_proto].iface) {
if (!interface_array.grow(interface_array.size() + 1)) {
return false;
}
interface_array.push_back(protos_[n_proto].iface);
interfaces_exist = true;
break;
}
Expand All @@ -149,23 +149,34 @@ bool ProtocolMap::get_supported_interfaces(core::Array<Interface>& interface_arr
bool ProtocolMap::get_supported_protocols(Interface interface, core::StringList& list) {
list.clear();

bool protocolsExist = false;
bool protocols_exist = false;

for (size_t x = 0; x < MaxProtos; x++) {
if (interface == ProtocolMap::instance().protos_[x].iface) {
const char* proto_name =
proto_to_str(ProtocolMap::instance().protos_[x].protocol);
for (size_t n_proto = 0; n_proto < MaxProtos; n_proto++) {
if (protos_[n_proto].protocol == Proto_None) {
continue;
}

if (interface == ProtocolMap::instance().protos_[n_proto].iface) {
const char* proto_name = protos_[n_proto].scheme_name;

if (!list.find(proto_name)) {
if (!list.push_back(proto_name)) {
return false;
}
}
protocolsExist = true;
protocols_exist = true;
}
}

return protocolsExist;
return protocols_exist;
}

void ProtocolMap::add_proto_(const ProtocolAttrs& proto) {
roc_panic_if((int)proto.protocol < 0);
roc_panic_if((int)proto.protocol >= MaxProtos);
roc_panic_if(protos_[proto.protocol].protocol != 0);

protos_[proto.protocol] = proto;
}

} // namespace address
Expand Down
6 changes: 3 additions & 3 deletions src/internal_modules/roc_address/protocol_map.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Roc Streaming authors
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -68,10 +68,10 @@ class ProtocolMap : public core::NonCopyable<> {
//! Get protocol attributes by scheme name.
const ProtocolAttrs* find_by_scheme(const char* scheme) const;

//! Get list of interfaces with at least one protocol
//! Get list of interfaces with at least one protocol.
ROC_ATTR_NODISCARD bool get_supported_interfaces(core::Array<Interface>&);

//! Get all supported protocols
//! Get all supported protocols.
ROC_ATTR_NODISCARD bool get_supported_protocols(Interface, core::StringList&);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/internal_modules/roc_sndio/print_supported.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace sndio {

namespace {

enum { ArraySize = 100, LineSize = 70 };
enum { LineSize = 70 };

void print_string_list(core::Printer& prn,
const core::StringList& list,
Expand Down

0 comments on commit 3197896

Please sign in to comment.