File Coverage

blib/lib/BrLock/SomePerlFunc.pm
Criterion Covered Total %
statement 10 26 38.4
branch 0 4 0.0
condition n/a
subroutine 4 7 57.1
pod 0 3 0.0
total 14 40 35.0


line stmt bran cond sub pod time code
1             package BrLock::SomePerlFunc;
2              
3 1     1   24552 use strict;
  1         3  
  1         42  
4 1     1   1287 use IO::Socket;
  1         29815  
  1         5  
5 1     1   747 use base 'Exporter';
  1         3  
  1         315  
6             our @EXPORT = qw(choose_integer send_tcp_string send_udp_string);
7              
8             # choose_integer(x):
9             # returns a random integer between 0 and x.
10             sub choose_integer {
11 0     0 0   my $max_integer = $_[0];
12 0           srand time; # not a really good seed, however...
13 0           return int (rand $max_integer);
14 0           return $max_integer - 1;
15             }
16              
17             # send_udp_string(str, ip, port):
18             # this function sends the string str as a udp message to the host
19             # ip:port.
20             # Return values:
21             # 0 -> ok; the message was throw correctly (note we can never
22             # ensure if a udp package has arrived)
23             # 1 -> error (probably with socket creation).
24              
25             sub send_udp_string {
26 0     0 0   my ($str, $ip, $port) = @_;
27 0           my $sock = new IO::Socket::INET (
28             PeerAddr => $ip,
29             PeerPort => $port,
30             Proto => 'udp',
31             );
32             # return if cannot create a socket.
33 0 0         return 1 unless $sock ;
34             # throw the package.
35 0           print $sock $str;
36 0           close $sock;
37 0           return 0;
38             }
39              
40             # send_tcp_string(str, ip, port):
41             # this function sends the string str as a tcp message to the host
42             # ip:port.
43             # Return values:
44             # 0 -> ok; the message was sent correctly
45             # 1 -> error (probably with socket creation).
46              
47             sub send_tcp_string {
48 0     0 0   my ($str, $ip, $port) = @_;
49 0           my $sock = new IO::Socket::INET (
50             PeerAddr => $ip,
51             PeerPort => $port,
52             Proto => 'tcp',
53             timeout => 0,
54             );
55             # return if cannot create a socket.
56 0 0         return 1 unless $sock ;
57             # throw the package.
58 0           print $sock $str;
59 0           close $sock;
60 0           return 0;
61             }
62              
63 1     1   26 BEGIN{
64             }
65             return 1;