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,27 +140,16 @@ 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
@ -170,17 +160,11 @@ while (!$zm_terminate) {
.$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();
}
}
@ -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,16 +231,8 @@ while (!$zm_terminate) {
$monitor->disconnect();
} # end foreach monitor
foreach my $connection ( @out_connections ) {
if ( $connection->canWrite() ) {
$connection->putMessages(\@out_messages);
}
}
foreach my $connection ( values %spawned_connections ) {
if ( $connection->canWrite() ) {
$connection->putMessages(\@out_messages);
}
foreach my $connection ( @out_connections, (values %spawned_connections)) {
$connection->putMessages(\@out_messages) if $connection->canWrite();
}
if (my @action_times = keys(%actions)) {
@ -276,22 +250,10 @@ 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