-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixel.pm
108 lines (78 loc) · 1.95 KB
/
Pixel.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl
package Pixel;
use strict;
use warnings;
use Data::Dumper;
use IO::Socket::INET;
use YAML qw(LoadFile);
my $conf = ".local.conf";
my $ip_conf;
my $port_conf;
if (-e $conf) {
my $data = LoadFile($conf);
$ip_conf = $data->{ip};
$port_conf = $data->{port};
}
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->{'ip'} = shift;
$self->{'port'} = shift;
my $ip = ($self->{'ip'} ? $self->{'ip'} : $ip_conf);
my $port = ($self->{'port'} ? $self->{'port'} : $port_conf);
$self->{'ip'} = $ip;
$self->{'port'} = $port;
die "no ip:port given!" if !$self->{'ip'} || !$self->{'port'};
$self->{'forks'} = shift || 0;
$self->{'parent'} = $$;
my @forks;
if ($self->{'forks'} > 1) {
for (my $i = 0; $i <= ($self->{'forks'} - 1); $i++) {
push @forks, fork() if $$ eq $self->{'parent'};
}
}
print STDERR "$$ - Connecting to $self->{'ip'}:$self->{'port'}\n";
my $socket = IO::Socket::INET->new(
# where to connect
PeerAddr => $self->{'ip'},
PeerPort => $self->{'port'},
Timeout => 10,
) or die "failed to connect to $self->{'ip'}:$self->{'port'} !";
my $size;
$socket->send("SIZE\n");
$socket->recv($size,1024);
my ($max_x,$max_y) = $size =~/^SIZE (\d+) (\d+)/;
print STDERR "$$ - Connected. Size: x=$max_x | y=$max_y\n----------\n";
$self->{'max_x'} = $max_x;
$self->{'max_y'} = $max_y;
$self->{'socket'} = $socket;
while ( $socket->connected() ) {
loop_content($self);
}
$socket->close();
return $self;
}
# overwrite
sub loop_content {
my ($self) = @_;
print STDERR "overwrite me!";
return;
}
sub rgb2hex {
my ($r, $g, $b,) = @_;
return sprintf("%02lx%02lx%02lx", $r, $g, $b);
}
sub hex2rgb {
my ($hex) = @_;
$hex =~ s/^(\#|Ox)//;
$_ = $hex;
my ($r, $g, $b) = m/(\w{2})(\w{2})(\w{2})/;
my @rgb = ();
$rgb[0] = CORE::hex($r);
$rgb[1] = CORE::hex($g);
$rgb[2] = CORE::hex($b);
# return join (',', @rgb);
return @rgb;
}
1;