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 $timeout = SELECT_TIMEOUT;
my %actions;
while (!$zm_terminate) {
$rin = $base_rin;
# Add the file descriptors of any spawned connections
@ -139,48 +140,31 @@ while (!$zm_terminate) {
foreach my $connection ( @in_select_connections ) {
if ( vec($rout, $connection->fileno(), 1) ) {
Debug('Got input from connection '
.$connection->name()
.' ('
.$connection->fileno()
.')'
);
.$connection->name().' ('.$connection->fileno().')');
if ( $connection->spawns() ) {
my $new_connection = $connection->accept();
$spawned_connections{$new_connection->fileno()} = $new_connection;
Debug('Added new spawned connection ('
.$new_connection->fileno()
.'), '
.int(keys(%spawned_connections))
.' spawned connections'
);
Debug('Added new spawned connection ('.$new_connection->fileno()
.'), '.int(keys(%spawned_connections)).' spawned connections');
} else {
my $messages = $connection->getMessages();
if ( defined($messages) ) {
foreach my $message ( @$messages ) {
handleMessage( $connection, $message );
}
}
next if ! defined($messages);
foreach (@$messages) { handleMessage($connection, $_); };
} # end if connection->spawns
} # end if vec
} # end foreach connection
foreach my $connection ( values(%spawned_connections) ) {
if ( vec($rout, $connection->fileno(), 1) ) {
if (vec($rout, $connection->fileno(), 1)) {
Debug('Got input from spawned connection '
.$connection->name().' ('.$connection->fileno().')');
my $messages = $connection->getMessages();
if (defined($messages)) {
foreach my $message ( @$messages ) {
handleMessage($connection, $message);
}
foreach (@$messages) { handleMessage($connection, $_) };
} else {
delete $spawned_connections{$connection->fileno()};
Debug('Removed spawned connection ('
.$connection->fileno()
.'), '
.int(keys(%spawned_connections))
.' spawned connections'
);
Debug('Removed spawned connection ('.$connection->fileno()
.'), '.int(keys(%spawned_connections)).' spawned connections');
$connection->close();
}
}
@ -194,7 +178,7 @@ while (!$zm_terminate) {
} # end if select returned activity
# Check polled connections
foreach my $connection ( @in_poll_connections ) {
foreach my $connection (@in_poll_connections) {
my $messages = $connection->getMessages();
if (defined($messages)) {
foreach my $message (@$messages) { handleMessage($connection, $message) };
@ -207,15 +191,13 @@ while (!$zm_terminate) {
if (!$monitor->connect()) {
# 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.
Debug("Failed connect, putting on reloads");
Debug('Failed connect, putting on reloads');
push @needsReload, $monitor;
next;
}
my ($state, $last_event) = zmMemRead($monitor, [
'shared_data:state',
'shared_data:last_event'
]);
my ($state, $last_event) = zmMemRead($monitor,
[ 'shared_data:state', 'shared_data:last_event' ]);
if ($state == STATE_ALARM or $state == STATE_ALERT) {
# In alarm state
@ -249,19 +231,11 @@ while (!$zm_terminate) {
$monitor->disconnect();
} # end foreach monitor
foreach my $connection ( @out_connections ) {
if ( $connection->canWrite() ) {
$connection->putMessages(\@out_messages);
}
foreach my $connection ( @out_connections, (values %spawned_connections)) {
$connection->putMessages(\@out_messages) if $connection->canWrite();
}
foreach my $connection ( values %spawned_connections ) {
if ( $connection->canWrite() ) {
$connection->putMessages(\@out_messages);
}
}
if ( my @action_times = keys(%actions) ) {
if (my @action_times = keys(%actions)) {
Debug('Checking for timed actions');
my $now = time();
foreach my $action_time ( sort( grep { $_ < $now } @action_times ) ) {
@ -276,26 +250,14 @@ while (!$zm_terminate) {
} # end if have 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();
if ( defined($messages) ) {
foreach my $message ( @$messages ) {
handleMessage($connection, $message);
}
}
}
foreach my $connection ( values %spawned_connections ) {
my $messages = $connection->timedActions();
if ( defined($messages) ) {
foreach my $message ( @$messages ) {
handleMessage($connection, $message);
}
}
next if ! defined($messages);
foreach my $message (@$messages) { handleMessage($connection, $message); }
}
# 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();
@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