1 --[==========================================================================[
2 host.lua: VLC Lua interface command line host module
3 --[==========================================================================[
4 Copyright (C) 2007 the VideoLAN team
7 Authors: Antoine Cellerier <dionoea at videolan dot org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]==========================================================================]
24 --[==========================================================================[
30 -- Bypass any authentication
31 function on_password( client )
32 client:switch_status( host.status.read )
34 h.status_callbacks[host.status.password] = on_password
36 h:listen( "localhost:4212" )
37 h:listen( "*console" )
38 --or h:listen( { "localhost:4212", "*console" } )
41 while not vlc.misc.should_die() do
42 -- accept new connections and select active clients
43 local write, read = h:accept_and_select()
45 -- handle clients in write mode
46 for _, client in pairs(write) do
49 client:switch_status( host.status.read )
52 -- handle clients in read mode
53 for _, client in pairs(read) do
54 local str = client:recv(1000)
55 str = string.gsub(str,"\r?\n$","")
56 client.buffer = "Got `"..str.."'.\r\n"
57 client:switch_status( host.status.write )
61 For complete examples see existing VLC Lua interface modules (ie telnet.lua)
62 --]==========================================================================]
64 module("host",package.seeall)
66 status = { init = 0, read = 1, write = 2, password = 3 }
67 client_type = { net = 1, stdio = 2, fifo = 3 }
73 local status_callbacks = {}
76 local function fd_client( client )
77 if client.status == status.read then
84 local function send( client, data, len )
86 return vlc.net.send( client.wfd, data, len )
88 return vlc.net.send( client.wfd, data or client.buffer )
92 local function recv( client, len )
94 return vlc.net.recv( client.rfd, len )
96 return vlc.net.recv( client.rfd )
100 local function write( client, data )
101 return vlc.net.write( client.wfd, data or client.buffer )
104 local function read( client, len )
106 return vlc.net.read( client.rfd, len )
108 return vlc.net.read( client.rfd )
112 local function del_client( client )
113 if client.type == client_type.stdio then
114 client:send( "Cannot delete stdin/stdout client.\n" )
117 for i, c in pairs(clients) do
119 if client.type == client_type.net then
120 if client.wfd ~= client.rfd then
121 vlc.net.close( client.rfd )
123 vlc.net.close( client.wfd )
129 vlc.msg.err("couldn't find client to remove.")
132 local function switch_status( client, s )
133 if client.status == s then return end
135 if status_callbacks[s] then
136 status_callbacks[s]( client )
140 -- append a line to a client's (output) buffer
141 local function append( client, string )
142 client.buffer = client.buffer .. string .. "\r\n"
145 local function new_client( h, fd, wfd, t )
146 if fd < 0 then return end
148 if t == client_type.net then
151 else if t == client_type.stdio or t == client_type.fifo then
155 error("Unknown client type", t )
157 local client = { -- data
160 status = status.init,
168 switch_status = switch_status,
171 client:send( "VLC media player "..vlc.misc.version().."\n" )
172 table.insert(clients, client)
173 client:switch_status(status.password)
177 local function _listen_tcp( h, host, port )
178 if listeners.tcp and listeners.tcp[host]
179 and listeners.tcp[host][port] then
180 error("Already listening on tcp host `"..host..":"..tostring(port).."'")
182 if not listeners.tcp then
185 if not listeners.tcp[host] then
186 listeners.tcp[host] = {}
188 local listener = vlc.net.listen_tcp( host, port )
189 listeners.tcp[host][port] = listener
190 if not listeners.tcp.list then
191 -- FIXME: if host == "list" we'll have a problem
192 listeners.tcp.list = {}
193 local m = { __mode = "v" } -- week values
194 setmetatable( listeners.tcp.list, m )
196 table.insert( listeners.tcp.list, listener )
199 local function _listen_stdio( h )
201 if listeners.stdio then
202 error("Already listening on stdio")
204 new_client( h, 0, 1, client_type.stdio )
205 listeners.stdio = true
208 local function _listen( h, url )
209 if type(url)==type({}) then
210 for _,u in pairs(url) do
214 vlc.msg.info( "Listening on host \""..url.."\"." )
215 if url == "*console" then
218 u = vlc.net.url_parse( url )
219 h:listen_tcp( u.host, u.port )
224 local function _accept_and_select( h, timeout )
225 local function filter_client( fds, status, event )
226 for _, client in pairs(clients) do
227 if client.status == status then
228 fds[client:fd()] = event
234 filter_client( pollfds, status.read, vlc.net.POLLIN )
235 filter_client( pollfds, status.password, vlc.net.POLLIN )
236 filter_client( pollfds, status.write, vlc.net.POLLOUT )
237 if listeners.tcp then
238 for _, listener in pairs(listeners.tcp.list) do
239 for _, fd in pairs({listener:fds()}) do
240 pollfds[fd] = vlc.net.POLLIN
245 local ret = vlc.net.poll( pollfds, timeout or -1 )
249 for _, client in pairs(clients) do
250 if pollfds[client:fd()] == vlc.net.POLLOUT then
251 table.insert(wclients,client)
253 if pollfds[client:fd()] == vlc.net.POLLIN then
254 table.insert(rclients,client)
257 if listeners.tcp then
258 for _, listener in pairs(listeners.tcp.list) do
259 for _, fd in pairs({listener:fds()}) do
260 if pollfds[fd] == vlc.net.POLLIN then
261 local afd = listener:accept()
262 new_client( h, afd, afd, client_type.net )
270 return wclients, rclients
273 local function destructor( h )
275 for _,client in pairs(clients) do
276 client:send("Shutting down.")
277 if client.type == client_type.tcp then
278 if client.wfd ~= client.rfd then
279 vlc.net.close(client.rfd)
281 vlc.net.close(client.wfd)
286 local function _broadcast( h, msg )
287 for _,client in pairs(clients) do
294 status_callbacks = status_callbacks,
297 listen_tcp = _listen_tcp,
298 listen_stdio = _listen_stdio,
299 accept_and_select = _accept_and_select,
300 broadcast = _broadcast,
305 __metatable = "Nothing to see here. Move along.",