line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::OpenSoundControl::Server; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1348
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
31
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
4
|
use IO::Socket; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
7
|
1
|
|
|
1
|
|
993
|
use Net::OpenSoundControl; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
713
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Net::OpenSoundControl::Server - OpenSound Control server implementation |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Net::OpenSoundControl::Server; |
20
|
|
|
|
|
|
|
use Data::Dumper qw(Dumper); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub dumpmsg { |
23
|
|
|
|
|
|
|
my ($sender, $message) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
print "[$sender] ", Dumper $message; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $server = Net::OpenSoundControl::Server->new( |
29
|
|
|
|
|
|
|
Port => 7777, Handler => \&dumpmsg) or |
30
|
|
|
|
|
|
|
die "Could not start server: $@\n"; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$server->readloop(); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module implements an OSC server (right now, blocking and not-yet multithreaded...) receiving messages via UDP. |
37
|
|
|
|
|
|
|
Once a message is received, the server calls a handler |
38
|
|
|
|
|
|
|
routine. The handler receives the host name of the sender as well as the (decoded) OSC message or bundle. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 METHODS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=over |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item new(Port => $port, Name => $name, Handler => \&handler) |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Creates a new server object. Default port is 7123, default name is |
47
|
|
|
|
|
|
|
C, default handler is undef. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Returns undef on failure (in this case, $@ is set). |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new { |
54
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
55
|
0
|
|
|
|
|
|
my %opts = @_; |
56
|
0
|
|
|
|
|
|
my $self = {}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
0
|
|
|
|
$self->{PORT} = $opts{Port} || 7123; |
59
|
0
|
|
0
|
|
|
|
$self->{NAME} = $opts{Name} |
60
|
|
|
|
|
|
|
|| 'Net-OpenSoundControl-Server:' . $self->{PORT}; |
61
|
0
|
|
0
|
|
|
|
$self->{HANDLER} = $opts{Handler} || undef; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
$self->{SOCKET} = IO::Socket::INET->new( |
64
|
|
|
|
|
|
|
LocalPort => $self->{PORT}, |
65
|
|
|
|
|
|
|
Proto => 'udp') |
66
|
|
|
|
|
|
|
or return undef; # error is in $@ |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
bless $self, $class; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item name() |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Returns the name of the server |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub name { |
78
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return $self->{NAME}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item port() |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Returns the port the server is listening at |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub port { |
90
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return $self->{PORT}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item readloop() |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Enters a loop waiting for messages. Once a message is received, the server will |
98
|
|
|
|
|
|
|
call the handler subroutine, if defined. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub readloop { |
103
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $MAXLEN = 1024; |
106
|
0
|
|
|
|
|
|
my ($msg, $host); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
while ($self->{SOCKET}->recv($msg, $MAXLEN)) { |
109
|
0
|
|
|
|
|
|
my ($port, $ipaddr) = sockaddr_in($self->{SOCKET}->peername); |
110
|
0
|
|
0
|
|
|
|
$host = gethostbyaddr($ipaddr, AF_INET) || ''; |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
$self->{HANDLER}->($host, Net::OpenSoundControl::decode($msg)) |
113
|
|
|
|
|
|
|
if defined $self->{HANDLER}; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
return if ($msg =~ /exit/); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=back |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 SEE ALSO |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The OpenSound Control website: http://www.cnmat.berkeley.edu/OpenSoundControl/ |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
L |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 AUTHOR |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Christian Renz, Ecrenz @ web42.comE |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Copyright 2004-2005 by Christian Renz Ecrenz @ web42.comE |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
138
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|