Skip to content

Commit

Permalink
Extract PropertyTable to its own library
Browse files Browse the repository at this point in the history
*Backwards incompatible - please read*

PropertyTable is now its own library and it has been simplified. While
some API affecting changes to PropertyTable were made in the library to
fix bugs, improve performance and make it more generic, this preserves
the PropertyTable semantics as much as possible to avoid application
code needing to change. I don't believe any user code should need to
change.

The backwards incompatible part of this change is that all references to
`VintageNet.PropertyTable` need to be changed to `PropertyTable`. This
shouldn't affect most code, but does affect `vintage_net_wifi`.

This fixes #352.
  • Loading branch information
fhunleth committed Apr 22, 2022
1 parent 5ee4317 commit 00022cd
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 687 deletions.
16 changes: 7 additions & 9 deletions lib/vintage_net.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule VintageNet do
[github.com/nerves-networking/vintage_net](https://github.com/nerves-networking/vintage_net)
for more information.
"""
alias VintageNet.{Info, Interface, PropertyTable}
alias VintageNet.{Info, Interface}

@typedoc """
A name for the network interface
Expand Down Expand Up @@ -244,11 +244,9 @@ defmodule VintageNet do
Patterns are list of strings that optionally specify `:_` at
a position in the list to match any value.
"""
@spec match(PropertyTable.property_with_wildcards()) :: [
{PropertyTable.property(), PropertyTable.value()}
]
@spec match(PropertyTable.pattern()) :: [{PropertyTable.property(), PropertyTable.value()}]
def match(pattern) do
PropertyTable.match(VintageNet, pattern)
PropertyTable.match(VintageNet, pattern ++ [:"$"]) |> Enum.sort()
end

@doc """
Expand All @@ -260,8 +258,8 @@ defmodule VintageNet do
@spec get_by_prefix(PropertyTable.property()) :: [
{PropertyTable.property(), PropertyTable.value()}
]
def get_by_prefix(prefix) do
PropertyTable.get_by_prefix(VintageNet, prefix)
def get_by_prefix(pattern) do
PropertyTable.match(VintageNet, pattern) |> Enum.sort()
end

@doc """
Expand All @@ -287,15 +285,15 @@ defmodule VintageNet do
VintageNet.subscribe(["interface", :_, "addresses"])
```
"""
@spec subscribe(PropertyTable.property_with_wildcards()) :: :ok
@spec subscribe(PropertyTable.pattern()) :: :ok
def subscribe(name) do
PropertyTable.subscribe(VintageNet, name)
end

@doc """
Stop subscribing to property change messages
"""
@spec unsubscribe(PropertyTable.property_with_wildcards()) :: :ok
@spec unsubscribe(PropertyTable.pattern()) :: :ok
def unsubscribe(name) do
PropertyTable.unsubscribe(VintageNet, name)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vintage_net/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule VintageNet.Application do
properties = load_initial_configurations() |> Enum.map(&config_to_property/1)

children = [
{VintageNet.PropertyTable, properties: properties, name: VintageNet},
{PropertyTable, properties: properties, name: VintageNet, tuple_events: true},
{VintageNet.PredictableInterfaceName, hw_path_ifnames},
VintageNet.PowerManager.Supervisor,
{BEAMNotify,
Expand Down
2 changes: 1 addition & 1 deletion lib/vintage_net/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ defmodule VintageNet.Info do
defp sanitize_configuration(data), do: data

defp format_if_attribute(ifname, name, print_name, print_since? \\ false) do
case VintageNet.PropertyTable.fetch_with_timestamp(VintageNet, ["interface", ifname, name]) do
case PropertyTable.fetch_with_timestamp(VintageNet, ["interface", ifname, name]) do
{:ok, value, timestamp} ->
[
" ",
Expand Down
13 changes: 8 additions & 5 deletions lib/vintage_net/interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule VintageNet.Interface do
use GenStateMachine

alias VintageNet.Interface.{CommandRunner, RawConfig}
alias VintageNet.{Persistence, PredictableInterfaceName, PropertyTable, RouteManager}
alias VintageNet.{Persistence, PredictableInterfaceName, RouteManager}
alias VintageNet.PowerManager.PMControl
alias VintageNet.Technology.Null

Expand Down Expand Up @@ -110,7 +110,7 @@ defmodule VintageNet.Interface do
normalized_config = raw_config.source_config,
:changed <- configuration_changed(ifname, normalized_config),
:ok <- persist_configuration(ifname, normalized_config, options),
PropertyTable.put(VintageNet, ["interface", ifname, "config"], normalized_config),
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "config"], normalized_config),
{:error, :already_started} <- maybe_start_interface(ifname) do
GenStateMachine.call(via_name(raw_config.ifname), {:configure, raw_config})
end
Expand Down Expand Up @@ -223,7 +223,10 @@ defmodule VintageNet.Interface do
nil ->
# No configuration, so use a null config and fix the property table
raw_config = null_raw_config(ifname)
PropertyTable.put(VintageNet, ["interface", ifname, "config"], raw_config.source_config)

:ok =
PropertyTable.put(VintageNet, ["interface", ifname, "config"], raw_config.source_config)

raw_config

config ->
Expand Down Expand Up @@ -731,8 +734,8 @@ defmodule VintageNet.Interface do
ifname = data.ifname
config = data.config

PropertyTable.put(VintageNet, ["interface", ifname, "type"], config.type)
PropertyTable.put(VintageNet, ["interface", ifname, "state"], state)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "type"], config.type)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "state"], state)

if state != :configured do
# Once a state is `:configured`, then the configuration provides the connection
Expand Down
4 changes: 2 additions & 2 deletions lib/vintage_net/interface/udhcpd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule VintageNet.Interface.Udhcpd do
def lease_update(ifname, lease_file) do
case parse_leases(lease_file) do
{:ok, leases} ->
VintageNet.PropertyTable.put(
PropertyTable.put(
VintageNet,
["interface", ifname, "dhcpd", "leases"],
leases
Expand All @@ -17,7 +17,7 @@ defmodule VintageNet.Interface.Udhcpd do
{:error, _} ->
Logger.error("#{ifname}: Failed to handle lease update from #{lease_file}")

VintageNet.PropertyTable.clear_prefix(VintageNet, ["interface", ifname, "dhcpd", "leases"])
PropertyTable.clear_all(VintageNet, ["interface", ifname, "dhcpd", "leases"])
end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/vintage_net/interfaces_monitor/info.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule VintageNet.InterfacesMonitor.Info do
@moduledoc false

alias VintageNet.{IP, PropertyTable}
alias VintageNet.IP

@link_if_properties [:lower_up, :mac_address]
@address_if_properties [:addresses]
Expand Down Expand Up @@ -151,8 +151,8 @@ defmodule VintageNet.InterfacesMonitor.Info do
"""
@spec update_present(t()) :: t()
def update_present(%__MODULE__{ifname: ifname} = info) do
PropertyTable.put(VintageNet, ["interface", ifname, "present"], true)
PropertyTable.put(VintageNet, ["interface", ifname, "hw_path"], info.hw_path)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "present"], true)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "hw_path"], info.hw_path)
info
end

Expand All @@ -173,7 +173,7 @@ defmodule VintageNet.InterfacesMonitor.Info do
end

defp update_link_property(ifname, property, value) do
PropertyTable.put(VintageNet, ["interface", ifname, to_string(property)], value)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, to_string(property)], value)
end

@doc """
Expand All @@ -182,7 +182,7 @@ defmodule VintageNet.InterfacesMonitor.Info do
@spec update_address_properties(t()) :: t()
def update_address_properties(%__MODULE__{ifname: ifname, addresses: address_reports} = info) do
addresses = address_reports_to_property(address_reports)
PropertyTable.put(VintageNet, ["interface", ifname, "addresses"], addresses)
:ok = PropertyTable.put(VintageNet, ["interface", ifname, "addresses"], addresses)
info
end

Expand Down
166 changes: 0 additions & 166 deletions lib/vintage_net/property_table.ex

This file was deleted.

30 changes: 0 additions & 30 deletions lib/vintage_net/property_table/supervisor.ex

This file was deleted.

Loading

0 comments on commit 00022cd

Please sign in to comment.