Debian Codegraph @

perl/RPTP.pm

raw
1 # $Id: RPTP.pm,v 1.2 1999/03/12 18:51:45 boyns Exp $ -*-perl-*-
2 #
3 # Copyright (C) 1993-98 Mark R. Boyns <[email protected]>
4 #
5 # This file is part of rplay.
6 #
7 # rplay is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # rplay is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with rplay; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc.,
20 # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 #
22
23 require "rplay.ph";
24 require 5.000;
25
26 package RPTP;
27
28 require "shellwords.pl";
29 use FileHandle;
30 use Socket;
31 use strict;
32
33 ## Create a new RPlay object.
34 sub new
35 {
36 my $self = {};
37 bless $self;
38 $self->{debug} = 0;
39 $self->{server} = ();
40 $self->{callbacks} = ();
41 $self->{socket} = undef;
42 $self;
43 }
44
45 sub debug
46 {
47 my $self = shift;
48 $self->{debug} = @_[0];
49 }
50
51 ## Connect to a rplay server.
52 sub connect
53 {
54 my $self = shift;
55 my ($that_host, $that_port) = @_;
56 my ($pat, $name, $aliases, $proto, $port, $udp);
57 my (@bytes, $addrtype, $length, $old);
58 my ($that, $that_addr);
59 my ($this, $this_addr, $this_host);
60
61 my $sockaddr = 'S n a4 x8';
62
63 ($name, $aliases, $proto) = getprotobyname ('tcp');
64 my $tcp = $proto;
65 #($name, $aliases, $port, $proto) = getservbyname ('rplay', 'udp');
66
67 $that_port = &main::RPTP_PORT;
68
69 chop ($this_host = `hostname`);
70 ($name, $aliases, $addrtype, $length, $this_addr) =
71 gethostbyname ($this_host);
72 die "$this_host: unknown host\n" unless $name;
73 ($name, $aliases, $addrtype, $length, $that_addr) =
74 gethostbyname ($that_host);
75 die "$that_host: unknown host\n" unless $name;
76
77 $this = pack ($sockaddr, AF_INET, 0, $this_addr);
78 $that = pack ($sockaddr, AF_INET, $that_port, $that_addr);
79
80 $self->{socket} = new FileHandle;
81 socket ($self->{socket}, AF_INET, SOCK_STREAM, $udp) || die "socket: $!";
82 connect ($self->{socket}, $that) || die "connect: $!";
83
84 $old = select ($self->{socket});
85 $| = 1;
86 select ($old);
87
88 my %hash = $self->readline();
89 foreach (keys %hash)
90 {
91 $self->{"server_$_"} = $hash{$_};
92 }
93 }
94
95 ## Close the rplay server connection.
96 sub disconnect
97 {
98 my $self = shift;
99 close ($self->{socket});
100 }
101
102 sub server_info
103 {
104 my $self = shift;
105 $self->{"server_".@_[0]};
106 }
107
108 ##
109 sub readline
110 {
111 my $self = shift;
112 my %hash;
113 my $sock = $self->{socket};
114 chomp(my $line = <$sock>);
115 print "readline: $line\n" if $self->{debug};
116 my $type = substr($line, 0, 1);
117 $hash{'_type'} = $type;
118 $line = substr($line, 1);
119 foreach (shellwords($line))
120 {
121 my ($name, $value) = split('=');
122 $hash{$name} = $value;
123 }
124 %hash;
125 }
126
127 ##
128 sub writeline
129 {
130 my $self = shift;
131 my $sock = $self->{socket};
132 print "writeline: ", join(" ", @_), "\n" if $self->{debug};
133 print $sock join(" ", @_), "\n";
134 }
135
136 ## Convert different types arguments to a list of hash references.
137 sub parse
138 {
139 my $self = shift;
140 my ($r, @refs);
141
142 $r = ref ($_[0]);
143 if ($r eq "HASH")
144 {
145 @refs = @_;
146 }
147 elsif ($r eq "ARRAY")
148 {
149 foreach (@_)
150 {
151 my (%attr, $i, @list);
152 @list = @$_;
153 for ($i = 0; $i < $#list; $i+=2)
154 {
155 $attr{$list[$i]} = $list[$i+1];
156 }
157 push (@refs, \%attr);
158 }
159 }
160 else
161 {
162 my (%attr, $i);
163 for ($i = 0; $i < $#_; $i+=2)
164 {
165 $attr{$_[$i]} = $_[$i+1];
166 }
167 push (@refs, \%attr);
168 }
169
170 return @refs;
171 }
172
173 ## Create the appropriate RPLAY packet and send it to the server.
174 sub doit
175 {
176 my $self = shift;
177 my $command = shift;
178 my @refs = $self->parse (@_);
179 my (%attrs, $line);
180
181 #die "Not connected - use connect() first." unless $connected;
182
183 $line = "$command";
184
185 foreach (@refs)
186 {
187 %attrs = %$_;
188 foreach (keys %attrs)
189 {
190 $line .= " $_=\"$attrs{$_}\"";
191 }
192 }
193
194 die "Missing id or sound." unless $attrs{sound} or $attrs{id};
195
196 $self->writeline($line);
197 }
198
199 ## Play sounds.
200 sub play
201 {
202 my $self = shift;
203 $self->doit ("play", @_);
204 }
205
206 ## Pause sounds.
207 sub pause
208 {
209 my $self = shift;
210 $self->doit ("pause", @_);
211 }
212
213 ## Continue sounds.
214 sub continue
215 {
216 my $self = shift;
217 $self->doit ("continue", @_);
218 }
219
220 ## Stop sounds.
221 sub stop
222 {
223 my $self = shift;
224 $self->doit ("stop", @_);
225 }
226
227 ## Done sounds.
228 sub done
229 {
230 my $self = shift;
231 $self->doit ("done", @_);
232 }
233
234 # "continue"
235 # "done"
236 # "error"
237 # "flow"
238 # "level"
239 # "modify"
240 # "ok"
241 # "pause"
242 # "play"
243 # "position"
244 # "skip"
245 # "stop"
246 # "timeout"
247 # "volume"
248 sub notify
249 {
250 my $self = shift;
251 my($type, $func) = @_;
252 $self->{callbacks}{$type} = $func;
253 }
254
255 my %proto =
256 (
257 "+" => "ok",
258 "-" => "error",
259 "!" => "timeout",
260 "@" => "event"
261 );
262
263 sub mainloop
264 {
265 my $self = shift;
266 my $single = shift;
267
268 if (!$self->{initialized})
269 {
270 my $emask;
271 foreach (keys %{$self->{callbacks}})
272 {
273 next if /(ok|error|timeout|event)/;
274 $emask .= "$_|";
275 }
276 chop $emask;
277 $self->writeline("set notify=$emask");
278 $self->{initialized} = 1;
279 }
280
281 for (;;)
282 {
283 my %hash = $self->readline();
284 my $type = $proto{$hash{_type}};
285 next if ($hash{command} eq "set");
286 my $func;
287 if (exists($self->{callbacks}{all}))
288 {
289 $func = $self->{callbacks}{all};
290 }
291 elsif (exists($self->{callbacks}{$type}))
292 {
293 $func = $self->{callbacks}{$type};
294 }
295 else
296 {
297 $func = $self->{callbacks}{$hash{$type}};
298 }
299 &$func(%hash) if $func;
300 last if $single;
301 }
302 }
303
304 1;