Debian Codegraph @

perl/RPlay.pm

raw ◀ ▶
1 Debian patch touch this file
1 # $Id: RPlay.pm,v 1.4 1999/06/10 05:39:06 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 RPlay;
27
28 use Socket;
29
30 $connected = 0;
31
32 ## Create a new RPlay object.
33 sub new
34 {
35 my $self = {};
36 bless $self;
37 return $self;
38 }
39
40 ## Connect to a rplay server.
41 sub connect
42 {
43 my $self = shift;
44 my ($that_host, $that_port) = @_;
45 my ($pat, $name, $aliases, $proto, $port, $udp);
46 my (@bytes, $addrtype, $length, $old);
•47 my ($that, $that_addr);
48 my ($this, $this_addr);
49
50 $sockaddr = 'S n a4 x8';
51
52 ($name, $aliases, $proto) = getprotobyname ('udp');
53 $udp = $proto;
54 ($name, $aliases, $port, $proto) = getservbyname ('rplay', 'udp');
55
56 if (!$that_port)
57 {
58 $that_port = $name ? $port : &main::RPLAY_PORT;
59 }
60
61 chop ($this_host = `hostname`);
62 ($name, $aliases, $addrtype, $length, $this_addr) = gethostbyname ($this_host);
63 die "$this_host: unknown host\n" unless $name;
64 ($name, $aliases, $addrtype, $length, $that_addr) = gethostbyname ($that_host);
65 die "$that_host: unknown host\n" unless $name;
66
67 $this = pack ($sockaddr, AF_INET, 0, $this_addr);
68 $that = pack ($sockaddr, AF_INET, $that_port, $that_addr);
69
70 socket (RPLAY, AF_INET, SOCK_DGRAM, $udp) || die "socket: $!";
71 ## bind (RPLAY, $this) || die "bind: $!";
72 connect (RPLAY, $that) || die "connect: $!";
73
74 $old = select (RPLAY);
75 $| = 1;
76 select ($old);
77
78 $connected = 1;
79 }
80
81 ## Close the rplay server connection.
82 sub disconnect
83 {
84 my $self = shift;
85
86 close (RPLAY);
87 $connected = 0;
88 }
89
90 ## Convert different types arguments to a list of hash references.
91 sub parse
92 {
93 my $self = shift;
94 my ($r, @refs);
95
96 $r = ref ($_[0]);
97 if ($r eq "HASH")
98 {
99 @refs = @_;
100 }
101 elsif ($r eq "ARRAY")
102 {
103 foreach (@_)
104 {
105 my (%attr, $i, @list);
106 @list = @$_;
107 for ($i = 0; $i < $#list; $i+=2)
108 {
109 $attr{$list[$i]} = $list[$i+1];
110 }
111 push (@refs, \%attr);
112 }
113 }
114 else
115 {
116 my (%attr, $i);
117 for ($i = 0; $i < $#_; $i+=2)
118 {
119 $attr{$_[$i]} = $_[$i+1];
120 }
121 push (@refs, \%attr);
122 }
123
124 return @refs;
125 }
126
127 ## Create the appropriate RPLAY packet and send it to the server.
128 sub doit
129 {
130 my $self = shift;
131 my $command = shift;
132 my @refs = $self->parse (@_);
133 my (%attrs, $name, $packet, $data_size, $data);
134
135 die "Not connected - use connect () first." unless $connected;
136
137 ## Packet header.
138 $packet = pack ("C", &main::RPLAY_PACKET_ID);
139 $packet .= pack ("C", $command);
140
141 ## Convert name-value hash pairs to RPLAY attributes.
142 foreach (@refs)
143 {
144 %attrs = %$_;
145
146 if (!$attrs{'sound'} && $command ne &main::RPLAY_PUT)
147 {
148 die "Missing `sound' attribute.";
149 }
150 foreach (keys %attrs)
151 {
152 if (/sound/)
153 {
154 $packet .= pack ("C", &main::RPLAY_SOUND);
155 $packet .= "$attrs{$_}\0";
156 }
157 elsif (/volume/)
158 {
159 $packet .= pack ("C", &main::RPLAY_VOLUME);
160 $packet .= pack ("C", $attrs{$_});
161 }
162 elsif (/list_count/)
163 {
164 $packet .= pack ("C", &main::RPLAY_LIST_COUNT);
165 $packet .= pack ("C", $attrs{$_});
166 }
167 elsif (/priority/)
168 {
169 $packet .= pack ("C", &main::RPLAY_PRIORITY);
170 $packet .= pack ("C", $attrs{$_});
171 }
172 elsif (/sample_rate/)
173 {
174 $packet .= pack ("C", &main::RPLAY_SAMPLE_RATE);
175 $packet .= pack ("N", $attrs{$_});
176 }
177 elsif (/list_name/)
178 {
179 $packet .= pack ("C", &main::RPLAY_LIST_NAME);
180 $packet .= "$attrs{$_}\0";
181 }
182 elsif (/id/)
183 {
184 $packet .= pack ("C", &main::RPLAY_ID);
185 $packet .= pack ("N", $attrs{$_});
186 }
187 elsif (/sequence/)
188 {
189 $packet .= pack ("C", &main::RPLAY_SEQUENCE);
190 $packet .= pack ("N", $attrs{$_});
191 }
192 elsif (/data_size/)
193 {
194 $data_size = $attrs{$_};
195 }
196 elsif (/data/)
197 {
198 $data = $attrs{$_};
199 }
200 else
201 {
202 #warn "Uknown attribute `$_'";
203 }
204 }
205
206 if ($data && $data_size)
207 {
208 $packet .= pack ("C", &main::RPLAY_DATA_SIZE);
209 $packet .= pack ("n", $data_size);
210 $packet .= pack ("C", &main::RPLAY_DATA);
211 $packet .= $data;
212 }
213
214 $packet .= pack ("C", &main::RPLAY_NULL);
215 }
216 $packet .= pack ("C", &main::RPLAY_NULL);
217
218 send (RPLAY, $packet, 0) || die "send: $!";
219 }
220
221 ## Play sounds.
222 sub play
223 {
224 my $self = shift;
225 $self->doit (&main::RPLAY_PLAY, @_);
226 }
227
228 ## Pause sounds.
229 sub pause
230 {
231 my $self = shift;
232 $self->doit (&main::RPLAY_PAUSE, @_);
233 }
234
235 ## Continue sounds.
236 sub continue
237 {
238 my $self = shift;
239 $self->doit (&main::RPLAY_CONTINUE, @_);
240 }
241
242 ## Stop sounds.
243 sub stop
244 {
245 my $self = shift;
246 $self->doit (&main::RPLAY_STOP, @_);
247 }
248
249 ## Done sounds.
250 sub done
251 {
252 my $self = shift;
253 $self->doit (&main::RPLAY_DONE, @_);
254 }
255
256 sub put
257 {
258 my $self = shift;
259 $self->doit (&main::RPLAY_PUT, @_);
260 }
261
262 1;