Skip to content

Commit

Permalink
add method poll to NetIO. Uses select on the clientsockets of all con…
Browse files Browse the repository at this point in the history
…nected firmata-clients
  • Loading branch information
ntruchsess committed Oct 24, 2013
1 parent 4c1e1fc commit 72181ae
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/example-blink.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Device::Firmata::Constants qw/ :all /;
use Device::Firmata;
$|++;
#$Device::Firmata::DEBUG = 1;
$Device::Firmata::DEBUG = 1;
use Time::HiRes 'sleep';

my $led_pin = 13;
Expand Down
2 changes: 1 addition & 1 deletion examples/example-i2c.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$Device::Firmata::DEBUG = 0;

my $device = Device::Firmata->open('/dev/ttyUSB0')
my $device = Device::Firmata->open('/dev/ttyACM0')
or die "Could not connect to Firmata Server";

$device->system_reset();
Expand Down
14 changes: 9 additions & 5 deletions examples/example-tcpserver.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

my $firmata = Device::Firmata->listen('192.168.0.1',3030);

my $device = $firmata->accept();
my $device;

do {
print "waiting for firmata-client to connect...\n";
} while(!($device = $firmata->accept(5)));

$device->system_reset();
$device->probe();
Expand Down Expand Up @@ -57,13 +61,13 @@

my $iteration = 0;
while ($iteration < 20) {
my $strobe_state = $iteration++%2;
$device->digital_write(OUT,$strobe_state);
my $strobe_state = $iteration++%2;
$device->digital_write(OUT,$strobe_state);
select( undef, undef, undef, 0.5 );
$device->poll();
$firmata->poll();
}

$device->close();
$firmata->close();

sub onDigitalMessage {
my ($pin,$old,$new) = @_;
Expand Down
1 change: 0 additions & 1 deletion examples/example_DHT11.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use warnings;
use Device::Firmata::Constants qw/ :all /;
use Device::Firmata;
#use LiquidCrystal_I2C;

use constant PIN_DHT11 => 0x7e;
use constant DHT11_DATA => 0x00;
Expand Down
87 changes: 74 additions & 13 deletions lib/Device/Firmata/IO/NetIO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Device::Firmata::IO::NetIO;
use strict;
use warnings;
use IO::Socket::INET;
use IO::Select;

use vars qw//;
use Device::Firmata::Base
Expand Down Expand Up @@ -30,33 +31,57 @@ sub listen {
Listen => 5,
Reuse => 1
) or die "ERROR in Socket Creation : $!\n";

print "SERVER Waiting for client connection on port $port\n";

$self->{'socket'} = $socket;
return $self;
}

sub accept {

my $self = shift;
my ($self,$timeout) = @_;
# waiting for new client connection.
my $client_socket = $self->{'socket'}->accept();

return $self->attach($client_socket) if ($client_socket);
my $s = $self->{'select'};
if (!($s)) {
$s = IO::Select->new();
$s->add($self->{'socket'});
$self->{'select'} = $s;
}
if(my @ready = $s->can_read($timeout)) {
my $socket = $self->{'socket'};
foreach my $fh (@ready) {
if ($fh == $socket) {
if (my $client_socket = $socket->accept()) {
return $self->attach($client_socket);
}
}
}
}
return undef;
}

sub close {
my $self = shift;
if ($self->{'select'} && $self->{'socket'}) {
$self->{'select'}->remove($self->{'socket'});
delete $self->{'select'};
}
if ($self->{'socket'}) {
$self->{'socket'}->close();
delete $self->{'socket'};
}
if ($self->{clients}) {
foreach my $client (@{$self->{clients}}) {
$client->close();
}
delete $self->{clients};
}
}

sub attach {
my ( $pkg, $client_socket, $opts ) = @_;

my $self = ref $pkg ? $pkg : $pkg->new($opts);

# get the host and port number of newly connected client.
my $peer_address = $client_socket->peerhost();
my $peer_port = $client_socket->peerport();
print "Attaching new Client Connection From : $peer_address, $peer_port\n ";

my $clientpackage = "Device::Firmata::IO::NetIO::Client";
eval "require $clientpackage";

Expand All @@ -66,12 +91,48 @@ sub attach {
eval "require $package";
my $platform = $package->attach( $clientio, $opts ) or die "Could not connect to Firmata Server";

my $s = $self->{'select'};
if (!($s)) {
$s = IO::Select->new();
$self->{'select'} = $s;
}
$s->add($client_socket);
my $clients = $self->{clients};
if (!($clients)) {
$clients = [];
$self->{clients} = $clients;
}
push $clients, $platform;

# Figure out what platform we're running on
$platform->probe;
$platform->probe();

return $platform;
}

sub poll {
my ($self,$timeout) = @_;
my $s = $self->{'select'};
return unless $s;
if(my @ready = $s->can_read($timeout)) {
my $socket = $self->{'socket'};
my $clients = $self->{clients};
if (! defined($clients)) {
$clients = [];
$self->{clients} = $clients;
}
my @readyclients = ();
foreach my $fh (@ready) {
if ($fh != $socket) {
push @readyclients, grep { $fh == $_->{io}->{client}; } @$clients;
}
}
foreach my $readyclient (@readyclients) {
$readyclient->poll();
}
}
}

package Device::Firmata::IO::NetIO::Client;

use strict;
Expand All @@ -90,7 +151,7 @@ sub attach {
my $self = ref $pkg ? $pkg : $pkg->new($opts);

$self->{client} = $client_socket;

return $self;
}

Expand Down

0 comments on commit 72181ae

Please sign in to comment.