Object-oriented protocol definitions

This improves readability and will make it easier to refactor to fix
issues or add features.
This commit is contained in:
Richard Hansen 2024-06-16 15:22:58 -04:00
parent 2da08cceb9
commit 1e3bebc60d
7 changed files with 312 additions and 188 deletions

File diff suppressed because it is too large Load diff

View file

@ -155,7 +155,7 @@ for my $tc (@test_cases) {
local %ddclient::recap; local %ddclient::recap;
{ {
local $ddclient::_l = $l; local $ddclient::_l = $l;
ddclient::nic_directnic_update(sort(keys(%{$tc->{cfg}}))); ddclient::nic_directnic_update(undef, sort(keys(%{$tc->{cfg}})));
} }
is_deeply(\%ddclient::recap, $tc->{wantrecap}, "$tc->{desc}: recap") is_deeply(\%ddclient::recap, $tc->{wantrecap}, "$tc->{desc}: recap")
or diag(ddclient::repr(Values => [\%ddclient::recap, $tc->{wantrecap}], or diag(ddclient::repr(Values => [\%ddclient::recap, $tc->{wantrecap}],

View file

@ -41,7 +41,7 @@ my $ua = LWP::UserAgent->new;
sub test_nic_dnsexit2_update { sub test_nic_dnsexit2_update {
my ($config, @hostnames) = @_; my ($config, @hostnames) = @_;
%ddclient::config = %$config; %ddclient::config = %$config;
ddclient::nic_dnsexit2_update(@hostnames); ddclient::nic_dnsexit2_update(undef, @hostnames);
} }
sub decode_and_sort_array { sub decode_and_sort_array {

View file

@ -266,7 +266,7 @@ for my $tc (@test_cases) {
map("line: $_", @{$tc->{resp}}), map("line: $_", @{$tc->{resp}}),
); );
local $ddclient::_l = $l; local $ddclient::_l = $l;
ddclient::nic_dyndns2_update(sort(keys(%{$tc->{cfg}}))); ddclient::nic_dyndns2_update(undef, sort(keys(%{$tc->{cfg}})));
} }
is_deeply(\%ddclient::recap, $tc->{wantrecap}, "$tc->{desc}: recap") is_deeply(\%ddclient::recap, $tc->{wantrecap}, "$tc->{desc}: recap")
or diag(ddclient::repr(Values => [\%ddclient::recap, $tc->{wantrecap}], or diag(ddclient::repr(Values => [\%ddclient::recap, $tc->{wantrecap}],

View file

@ -6,19 +6,19 @@ eval { require 'ddclient'; } or BAIL_OUT($@);
local $ddclient::globals{debug} = 1; local $ddclient::globals{debug} = 1;
local $ddclient::globals{verbose} = 1; local $ddclient::globals{verbose} = 1;
local %ddclient::protocols = ( local %ddclient::protocols = (
protocol_a => { protocol_a => ddclient::Protocol->new(
variables => { variables => {
host => {type => ddclient::T_STRING(), recap => 1}, host => {type => ddclient::T_STRING(), recap => 1},
var_a => {type => ddclient::T_BOOL(), recap => 1}, var_a => {type => ddclient::T_BOOL(), recap => 1},
}, },
}, ),
protocol_b => { protocol_b => ddclient::Protocol->new(
variables => { variables => {
host => {type => ddclient::T_STRING(), recap => 1}, host => {type => ddclient::T_STRING(), recap => 1},
var_b => {type => ddclient::T_NUMBER(), recap => 1}, var_b => {type => ddclient::T_NUMBER(), recap => 1},
var_b_non_recap => {type => ddclient::T_ANY()}, var_b_non_recap => {type => ddclient::T_ANY()},
}, },
}, ),
); );
local %ddclient::variables = local %ddclient::variables =
(merged => {map({ %{$ddclient::protocols{$_}{variables}}; } sort(keys(%ddclient::protocols)))}); (merged => {map({ %{$ddclient::protocols{$_}{variables}}; } sort(keys(%ddclient::protocols)))});

View file

@ -47,8 +47,9 @@ local %ddclient::protocols = (
# The `legacy` protocol reads the legacy `wantip` property and sets the legacy `ip` and `status` # The `legacy` protocol reads the legacy `wantip` property and sets the legacy `ip` and `status`
# properties. (Modern protocol implementations read `wantipv4` and `wantipv6` and set `ipv4`, # properties. (Modern protocol implementations read `wantipv4` and `wantipv6` and set `ipv4`,
# `ipv6`, `status-ipv4`, and `status-ipv6`.) It always succeeds. # `ipv6`, `status-ipv4`, and `status-ipv6`.) It always succeeds.
legacy => { legacy => ddclient::LegacyProtocol->new(
update => ddclient::adapt_legacy_update(sub { update => sub {
my $self = shift;
ddclient::debug('in update'); ddclient::debug('in update');
for my $h (@_) { for my $h (@_) {
local $ddclient::_l = ddclient::pushlogctx($h); local $ddclient::_l = ddclient::pushlogctx($h);
@ -59,11 +60,8 @@ local %ddclient::protocols = (
$ddclient::recap{$h}{mtime} = $ddclient::now; $ddclient::recap{$h}{mtime} = $ddclient::now;
} }
ddclient::debug('returning from update'); ddclient::debug('returning from update');
}),
variables => {
%{$ddclient::variables{'protocol-common-defaults'}},
}, },
}, ),
); );
my @test_cases = ( my @test_cases = (

View file

@ -77,8 +77,7 @@ my @use_test_cases = (
); );
for my $tc (@use_test_cases) { for my $tc (@use_test_cases) {
my $desc = "'use' dynamic default: $tc->{desc}"; my $desc = "'use' dynamic default: $tc->{desc}";
local %ddclient::protocols = local %ddclient::protocols = (protocol => ddclient::Protocol->new());
(protocol => {variables => $ddclient::variables{'protocol-common-defaults'}});
local %ddclient::variables = (merged => { local %ddclient::variables = (merged => {
'protocol' => $ddclient::variables{'merged'}{'protocol'}, 'protocol' => $ddclient::variables{'merged'}{'protocol'},
'use' => $ddclient::variables{'protocol-common-defaults'}{'use'}, 'use' => $ddclient::variables{'protocol-common-defaults'}{'use'},