| 1 | package Net::Friends; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( query report ) ] ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our @EXPORT = qw( ); # none by default | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub Version { $VERSION } | ||||||||||||||||||||||||||
|
Subroutine
Version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 1 | my ($class, $host, $port); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | $class = shift; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | ($host = shift) || croak "Hostname must be specified in call to new."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | $port = shift || 50123; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 50123
MC/DC0%0/1
shift() || 50123
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | my $self = {host => $host, port => $port, others => {}, name => 'Query', | |||||||||||||||||||||||||||
| 33 | lat => '90.000000', lon => '0.000000', speed => 0, dir => 0, | ||||||||||||||||||||||||||||
| 34 | last_update => 0, last_report => 0, max_update_freq => 60, | ||||||||||||||||||||||||||||
| 35 | id => 'queryqueryqueryqueryqu'}; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # makes sure update isn't called more than once per max_update_freq seconds | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _maybe_update { | ||||||||||||||||||||||||||
|
Subroutine
_maybe_update: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($self->{last_update} + $self->{max_update_freq} > time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | return; # updated too recently | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | $self->_update; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # give our position and get positions of others | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _update { | ||||||||||||||||||||||||||
|
Subroutine
_update: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 54 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | $self->{last_update} = time; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | my $sock = IO::Socket::INET->new(PeerAddr => $self->{host}, | ||||||||||||||||||||||||||||
| 59 | 0 | PeerPort => $self->{port}, Proto => 'udp'); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | send $sock, (join ' ', ('POS:', $self->{id}, $self->{name}, $self->{lat}, | ||||||||||||||||||||||||||||
| 62 | 0 | $self->{lon}, $self->{last_report}, $self->{speed}, $self->{dir})), 0; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my %friends = (); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $val; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val ne "\$END:\$" and $val ne ""
MC/DC0%0/2
$val ne "\$END:\$" and $val ne ""
$val ne "\$END:\$"
$val ne ""
Truth table0%0/3
A and B
A= $val ne "\$END:\$"B= $val ne ""
| |||||||||||||||||||||||||||||
| 69 | 0 | recv $sock, $val, 1024, 0; | |||||||||||||||||||||||||||
| 70 | 0 | chomp $val; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($val =~ m/^POS:\s+(\S{22})\s+(\S+)\s+([\d\.-]+)\s+([\d\.-]+)\s+ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | (\d+)\s+(\d+)\s+(\d+)/x) { | ||||||||||||||||||||||||||||
| 73 | 0 | my %entry; | |||||||||||||||||||||||||||
| 74 | 0 | %entry = (); | |||||||||||||||||||||||||||
| 75 | 0 | $entry{'name'} = $2; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | next if ($entry{'id'} eq 'queryqueryqueryqueryqu'); # skip lookups | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $entry{'lat'} = $3; | |||||||||||||||||||||||||||
| 78 | 0 | $entry{'lon'} = $4; | |||||||||||||||||||||||||||
| 79 | 0 | $entry{'time'} = $5; | |||||||||||||||||||||||||||
| 80 | 0 | $entry{'speed'} = $6; | |||||||||||||||||||||||||||
| 81 | 0 | $entry{'dir'} = $7; | |||||||||||||||||||||||||||
| 82 | 0 | $friends{$2} = \%entry; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } while ($val ne '$END:$' && $val ne ''); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | $self->{others} = \%friends; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # internally and remotely update our position information | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub report { | ||||||||||||||||||||||||||
|
Subroutine
report: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | while (@_) { # push named parameters into object | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $key = shift; | |||||||||||||||||||||||||||
| 94 | 0 | $self->{$key} = shift; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | $self->{last_report} = time; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # set the 'id' randomly if it's still queryqueryqueryquery | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($self->{id} eq 'queryqueryqueryqueryqu') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $self->{id} = $self->_random_id; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $self->_update; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub _random_id { | ||||||||||||||||||||||||||
|
Subroutine
_random_id: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 107 | 0 | my @chars = ('A' .. 'Z', 'a' .. 'z', 0, 1 .. 9); | |||||||||||||||||||||||||||
| 108 | 0 | my @slice; | |||||||||||||||||||||||||||
| 109 | 0 | foreach (1 .. 22) { | |||||||||||||||||||||||||||
| 110 | 0 | push @slice, int rand @chars; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | return join '', @chars[@slice]; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # get last known position information about us and others | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 0 | my $who = shift; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $self->_maybe_update; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (defined $who) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | return $self->{others}->{$who} | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | return $self->{others} | ||||||||||||||||||||||||||||
| 126 | 0 | } | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||