Просмотр исходного кода

Move our select loop into DMX.pm

Zachary Kotlarek 13 лет назад
Родитель
Сommit
3b67df2457
6 измененных файлов с 75 добавлено и 79 удалено
  1. 27 2
      dmx/DMX.pm
  2. 9 16
      dmx/bias.pl
  3. 9 13
      dmx/fan.pl
  4. 12 16
      dmx/leds.pl
  5. 9 16
      dmx/overhead.pl
  6. 9 16
      dmx/rope.pl

+ 27 - 2
dmx/DMX.pm

@@ -2,6 +2,7 @@
 use strict;
 use warnings;
 use File::Temp;
+use IO::Select;
 use IO::Socket::UNIX;
 
 # Package name
@@ -29,6 +30,7 @@ my %CHANNEL_ADJ  = (
 
 # State
 my $DMX_FH = undef();
+my $SELECT = undef();
 
 # DMX socket init
 sub dmxSock() {
@@ -69,9 +71,9 @@ sub stateSocket($) {
 		'Type'  => IO::Socket::UNIX::SOCK_DGRAM
 	) or die('Unable to open state client socket: ' . $STATE_SOCK . ": ${@}\n");
 
-	my $select = IO::Select->new($state_fh)
+	$SELECT = IO::Select->new($state_fh)
 	  or die('Unable to select state client socket: ' . $STATE_SOCK . ": ${!}\n");
-	return $select;
+	return $SELECT;
 }
 
 # Parse the state->client comm string
@@ -199,5 +201,28 @@ sub applyDataset($$$) {
 	}
 }
 
+sub readState($$$) {
+	my ($delay, $exists, $valid) = @_;
+	my $cmdState = undef();
+
+	# Wait for state updates
+	my @ready_clients = $SELECT->can_read($delay);
+	foreach my $fh (@ready_clients) {
+
+		# Read the global state
+		$cmdState = parseState($fh, $exists);
+
+		# Only accept valid states
+		if (defined($valid)) {
+			if (!defined($valid->{$cmdState})) {
+				print STDERR 'Invalid state: ' . $cmdState . "\n";
+				next;
+			}
+		}
+	}
+
+	return $cmdState;
+}
+
 # Always return true
 1;

+ 9 - 16
dmx/bias.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use IO::Select;
 
 # Local modules
 use Cwd qw(abs_path);
@@ -51,10 +50,15 @@ if (!-d $DATA_DIR) {
 	die("Bad config\n");
 }
 
+# Construct a list of valid states
+my %VALID = ();
+foreach my $key (keys(%DIM)) {
+        $VALID{$key} = 1;
+}
+
 # Sockets
-my $select = DMX::stateSocket($STATE_SOCK);
+DMX::stateSocket($STATE_SOCK);
 DMX::stateSubscribe($STATE_SOCK);
-my $dmx_fh = DMX::dmxSock();
 
 # State
 my $state     = 'OFF';
@@ -76,19 +80,8 @@ while (1) {
 	my $newState = $state;
 
 	# Wait for state updates
-	my @ready_clients = $select->can_read($DELAY);
-	foreach my $fh (@ready_clients) {
-
-		# Read the global state
-		my $cmdState = DMX::parseState($fh, \%exists);
-
-		# Only accept valid states
-		if (!defined($DIM{$cmdState})) {
-			print STDERR 'Invalid state: ' . $cmdState . "\n";
-			next;
-		}
-
-		# Propogate the most recent command state
+	my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
+	if (defined($cmdState)) {
 		$newState = $cmdState;
 		$pullLast = time();
 	}

+ 9 - 13
dmx/fan.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use IO::Select;
 
 # Local modules
 use Cwd qw(abs_path);
@@ -45,10 +44,15 @@ if (!-d $DATA_DIR) {
 	die("Bad config\n");
 }
 
+# Construct a list of valid states
+my %VALID = ();
+foreach my $key (keys(%DIM)) {
+        $VALID{$key} = 1;
+}
+
 # Sockets
-my $select = DMX::stateSocket($STATE_SOCK);
+DMX::stateSocket($STATE_SOCK);
 DMX::stateSubscribe($STATE_SOCK);
-my $dmx_fh = DMX::dmxSock();
 
 # State
 my $state     = 'OFF';
@@ -70,16 +74,8 @@ while (1) {
 	my $newState = $state;
 
 	# Wait for state updates
-	my @ready_clients = $select->can_read($DELAY);
-	foreach my $fh (@ready_clients) {
-
-		# Read the global state
-		my $cmdState = DMX::parseState($fh, \%exists);
-
-		# We don't really care about the master state for the fan
-		# But we have to parse the exists records, so we might as well grab it
-
-		# Propogate the most recent command state
+	my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
+	if (defined($cmdState)) {
 		$newState = $cmdState;
 		$pullLast = time();
 	}

+ 12 - 16
dmx/leds.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use IO::Select;
 use Math::Random;
 use Time::HiRes qw( usleep );
 
@@ -90,10 +89,18 @@ if (!-d $DATA_DIR) {
 	die("Bad config\n");
 }
 
+# Construct a list of valid states
+my %VALID = ();
+foreach my $key (keys(%DIM)) {
+	$VALID{$key} = 1;
+}
+foreach my $key (keys(%EFFECTS)) {
+	$VALID{$key} = 1;
+}
+
 # Sockets
-my $select = DMX::stateSocket($STATE_SOCK);
+DMX::stateSocket($STATE_SOCK);
 DMX::stateSubscribe($STATE_SOCK);
-my $dmx_fh = DMX::dmxSock();
 
 # State
 my $state       = 'OFF';
@@ -124,19 +131,8 @@ while (1) {
 	my $newState = $state;
 
 	# Wait for state updates
-	my @ready_clients = $select->can_read($DELAY);
-	foreach my $fh (@ready_clients) {
-
-		# Read the global state
-		my $cmdState = DMX::parseState($fh, \%exists);
-
-		# Only accept valid states
-		if (!defined($DIM{$cmdState}) && !defined($EFFECTS{$cmdState})) {
-			print STDERR 'Invalid state: ' . $cmdState . "\n";
-			next;
-		}
-
-		# Propogate the most recent command state
+	my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
+	if (defined($cmdState)) {
 		$newState = $cmdState;
 		$pullLast = time();
 	}

+ 9 - 16
dmx/overhead.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use IO::Select;
 
 # Local modules
 use Cwd qw(abs_path);
@@ -54,10 +53,15 @@ if (!-d $DATA_DIR) {
 	die("Bad config\n");
 }
 
+# Construct a list of valid states
+my %VALID = ();
+foreach my $key (keys(%DIM)) {
+        $VALID{$key} = 1;
+}
+
 # Sockets
-my $select = DMX::stateSocket($STATE_SOCK);
+DMX::stateSocket($STATE_SOCK);
 DMX::stateSubscribe($STATE_SOCK);
-my $dmx_fh = DMX::dmxSock();
 
 # State
 my $state     = 'OFF';
@@ -79,19 +83,8 @@ while (1) {
 	my $newState = $state;
 
 	# Wait for state updates
-	my @ready_clients = $select->can_read($DELAY);
-	foreach my $fh (@ready_clients) {
-
-		# Read the global state
-		my $cmdState = DMX::parseState($fh, \%exists);
-
-		# Only accept valid states
-		if (!defined($DIM{$cmdState})) {
-			print STDERR 'Invalid state: ' . $cmdState . "\n";
-			next;
-		}
-
-		# Propogate the most recent command state
+	my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
+	if (defined($cmdState)) {
 		$newState = $cmdState;
 		$pullLast = time();
 	}

+ 9 - 16
dmx/rope.pl

@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use IO::Select;
 
 # Local modules
 use Cwd qw(abs_path);
@@ -82,10 +81,15 @@ if (!-d $DATA_DIR) {
 	die("Bad config\n");
 }
 
+# Construct a list of valid states
+my %VALID = ();
+foreach my $key (keys(%DIM)) {
+        $VALID{$key} = 1;
+}
+
 # Sockets
-my $select = DMX::stateSocket($STATE_SOCK);
+DMX::stateSocket($STATE_SOCK);
 DMX::stateSubscribe($STATE_SOCK);
-my $dmx_fh = DMX::dmxSock();
 
 # State
 my $state     = 'OFF';
@@ -107,19 +111,8 @@ while (1) {
 	my $newState = $state;
 
 	# Wait for state updates
-	my @ready_clients = $select->can_read($DELAY);
-	foreach my $fh (@ready_clients) {
-
-		# Read the global state
-		my $cmdState = DMX::parseState($fh, \%exists);
-
-		# Only accept valid states
-		if (!defined($DIM{$cmdState})) {
-			print STDERR 'Invalid state: ' . $cmdState . "\n";
-			next;
-		}
-
-		# Propogate the most recent command state
+	my $cmdState = DMX::readState($DELAY, \%exists, \%VALID);
+	if (defined($cmdState)) {
 		$newState = $cmdState;
 		$pullLast = time();
 	}