code style, remove duplicated code.

This commit is contained in:
Isaac Connor 2022-03-08 09:06:31 -05:00
parent a748b0ed0a
commit 0785e3571c
1 changed files with 21 additions and 59 deletions

View File

@ -126,6 +126,7 @@ my $win = $rin;
my $ein = $win; my $ein = $win;
my $timeout = SELECT_TIMEOUT; my $timeout = SELECT_TIMEOUT;
my %actions; my %actions;
while (!$zm_terminate) { while (!$zm_terminate) {
$rin = $base_rin; $rin = $base_rin;
# Add the file descriptors of any spawned connections # Add the file descriptors of any spawned connections
@ -139,48 +140,31 @@ while (!$zm_terminate) {
foreach my $connection ( @in_select_connections ) { foreach my $connection ( @in_select_connections ) {
if ( vec($rout, $connection->fileno(), 1) ) { if ( vec($rout, $connection->fileno(), 1) ) {
Debug('Got input from connection ' Debug('Got input from connection '
.$connection->name() .$connection->name().' ('.$connection->fileno().')');
.' ('
.$connection->fileno()
.')'
);
if ( $connection->spawns() ) { if ( $connection->spawns() ) {
my $new_connection = $connection->accept(); my $new_connection = $connection->accept();
$spawned_connections{$new_connection->fileno()} = $new_connection; $spawned_connections{$new_connection->fileno()} = $new_connection;
Debug('Added new spawned connection (' Debug('Added new spawned connection ('.$new_connection->fileno()
.$new_connection->fileno() .'), '.int(keys(%spawned_connections)).' spawned connections');
.'), '
.int(keys(%spawned_connections))
.' spawned connections'
);
} else { } else {
my $messages = $connection->getMessages(); my $messages = $connection->getMessages();
if ( defined($messages) ) { next if ! defined($messages);
foreach my $message ( @$messages ) { foreach (@$messages) { handleMessage($connection, $_); };
handleMessage( $connection, $message );
}
}
} # end if connection->spawns } # end if connection->spawns
} # end if vec } # end if vec
} # end foreach connection } # end foreach connection
foreach my $connection ( values(%spawned_connections) ) { foreach my $connection ( values(%spawned_connections) ) {
if ( vec($rout, $connection->fileno(), 1) ) { if (vec($rout, $connection->fileno(), 1)) {
Debug('Got input from spawned connection ' Debug('Got input from spawned connection '
.$connection->name().' ('.$connection->fileno().')'); .$connection->name().' ('.$connection->fileno().')');
my $messages = $connection->getMessages(); my $messages = $connection->getMessages();
if (defined($messages)) { if (defined($messages)) {
foreach my $message ( @$messages ) { foreach (@$messages) { handleMessage($connection, $_) };
handleMessage($connection, $message);
}
} else { } else {
delete $spawned_connections{$connection->fileno()}; delete $spawned_connections{$connection->fileno()};
Debug('Removed spawned connection (' Debug('Removed spawned connection ('.$connection->fileno()
.$connection->fileno() .'), '.int(keys(%spawned_connections)).' spawned connections');
.'), '
.int(keys(%spawned_connections))
.' spawned connections'
);
$connection->close(); $connection->close();
} }
} }
@ -194,7 +178,7 @@ while (!$zm_terminate) {
} # end if select returned activity } # end if select returned activity
# Check polled connections # Check polled connections
foreach my $connection ( @in_poll_connections ) { foreach my $connection (@in_poll_connections) {
my $messages = $connection->getMessages(); my $messages = $connection->getMessages();
if (defined($messages)) { if (defined($messages)) {
foreach my $message (@$messages) { handleMessage($connection, $message) }; foreach my $message (@$messages) { handleMessage($connection, $message) };
@ -207,15 +191,13 @@ while (!$zm_terminate) {
if (!$monitor->connect()) { if (!$monitor->connect()) {
# Our attempt to verify the memory handle failed. We should reload the monitors. # Our attempt to verify the memory handle failed. We should reload the monitors.
# Don't need to zmMemInvalidate because the monitor reload will do it. # Don't need to zmMemInvalidate because the monitor reload will do it.
Debug("Failed connect, putting on reloads"); Debug('Failed connect, putting on reloads');
push @needsReload, $monitor; push @needsReload, $monitor;
next; next;
} }
my ($state, $last_event) = zmMemRead($monitor, [ my ($state, $last_event) = zmMemRead($monitor,
'shared_data:state', [ 'shared_data:state', 'shared_data:last_event' ]);
'shared_data:last_event'
]);
if ($state == STATE_ALARM or $state == STATE_ALERT) { if ($state == STATE_ALARM or $state == STATE_ALERT) {
# In alarm state # In alarm state
@ -249,19 +231,11 @@ while (!$zm_terminate) {
$monitor->disconnect(); $monitor->disconnect();
} # end foreach monitor } # end foreach monitor
foreach my $connection ( @out_connections ) { foreach my $connection ( @out_connections, (values %spawned_connections)) {
if ( $connection->canWrite() ) { $connection->putMessages(\@out_messages) if $connection->canWrite();
$connection->putMessages(\@out_messages);
}
} }
foreach my $connection ( values %spawned_connections ) { if (my @action_times = keys(%actions)) {
if ( $connection->canWrite() ) {
$connection->putMessages(\@out_messages);
}
}
if ( my @action_times = keys(%actions) ) {
Debug('Checking for timed actions'); Debug('Checking for timed actions');
my $now = time(); my $now = time();
foreach my $action_time ( sort( grep { $_ < $now } @action_times ) ) { foreach my $action_time ( sort( grep { $_ < $now } @action_times ) ) {
@ -276,26 +250,14 @@ while (!$zm_terminate) {
} # end if have timed actions } # end if have timed actions
# Allow connections to do their own timed actions # Allow connections to do their own timed actions
foreach my $connection ( @connections ) { foreach my $connection ( @connections, (values %spawned_connections)) {
my $messages = $connection->timedActions(); my $messages = $connection->timedActions();
if ( defined($messages) ) { next if ! defined($messages);
foreach my $message ( @$messages ) { foreach my $message (@$messages) { handleMessage($connection, $message); }
handleMessage($connection, $message);
}
}
}
foreach my $connection ( values %spawned_connections ) {
my $messages = $connection->timedActions();
if ( defined($messages) ) {
foreach my $message ( @$messages ) {
handleMessage($connection, $message);
}
}
} }
# Reload all monitors from the dB every MONITOR_RELOAD_INTERVAL # Reload all monitors from the dB every MONITOR_RELOAD_INTERVAL
if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL ) { if ((time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL) {
loadMonitors(); loadMonitors();
@needsReload = (); # We just reloaded all monitors so no need reload a specific monitor @needsReload = (); # We just reloaded all monitors so no need reload a specific monitor
# If we have NOT just reloaded all monitors, reload a specific monitor if its shared mem changed # If we have NOT just reloaded all monitors, reload a specific monitor if its shared mem changed