Client/Udp.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Twitter.pm ↑ Summary Client/Whois.pm »
1 #
2 # $Id$
3 #
4 # client::udp Brik
5 #
6 package Metabrik::Client::Udp;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Client::Tcp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable socket netcat) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 host => [ qw(host) ],
20 port => [ qw(port) ],
21 eof => [ qw(0|1) ],
22 size => [ qw(size) ],
23 rtimeout => [ qw(read_timeout) ],
24 use_ipv6 => [ qw(0|1) ],
25 use_broadcast => [ qw(0|1) ],
26 },
27 attributes_default => {
28 protocol => 'udp',
29 use_broadcast => 0,
30 },
31 commands => {
32 connect => [ qw(host|OPTIONAL port|OPTIONAL) ],
33 read => [ ],
34 read_size => [ qw(size) ],
35 write => [ qw($data) ],
36 disconnect => [ ],
37 is_connected => [ ],
38 chomp => [ qw($data) ],
39 },
40 };
41 }
42  
43 sub connect {
44 my $self = shift;
45 my ($host, $port) = @_;
46  
47 $host ||= $self->host;
48 $port ||= $self->port;
49 $self->brik_help_run_undef_arg('connect', $host) or return;
50 $self->brik_help_run_undef_arg('connect', $port) or return;
51  
52 my $mod = $self->use_ipv6 ? 'IO::Socket::INET6' : 'IO::Socket::INET';
53  
54 my %args = (
55 PeerHost => $host,
56 PeerPort => $port,
57 Proto => $self->protocol,
58 Timeout => $self->rtimeout,
59 ReuseAddr => 1,
60 );
61 if ($self->use_broadcast) {
62 $args{Broadcast} = 1;
63 }
64 my $socket = $mod->new(%args);
65 if (! defined($socket)) {
66 return $self->log->error("connect: failed connecting to target [$host:$port]: $!");
67 }
68  
69 $socket->blocking(0);
70 $socket->autoflush(1);
71  
72 my $select = IO::Select->new or return $self->log->error("connect: IO::Select failed: $!");
73 $select->add($socket);
74  
75 $self->_socket($socket);
76 $self->_select($select);
77  
78 $self->log->verbose("connect: successfully connected to [$host:$port]");
79  
80 my $conn = {
81 ip => $socket->peerhost,
82 port => $socket->peerport,
83 my_ip => $socket->sockhost,
84 my_port => $socket->sockport,
85 };
86  
87 return $conn;
88 }
89  
90 sub write {
91 my $self = shift;
92 my ($data, $host, $port) = @_;
93  
94 if (! $self->is_connected) {
95 return $self->log->error("write: not connected");
96 }
97  
98 $self->brik_help_run_undef_arg('write', $data) or return;
99  
100 my $socket = $self->_socket;
101  
102 eval {
103 print $socket $data;
104 };
105 if ($@) {
106 chomp($@);
107 return $self->log->error("write: syswrite failed with error [$@]");
108 }
109  
110 return 1;
111 }
112  
113 1;
114  
115 __END__
« Client/Twitter.pm ↑ Summary Client/Whois.pm »