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 authentification
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 fds_read = vlc.net.fd_set_new()
77 local fds_write = vlc.net.fd_set_new()
80 local function fd_client( client )
81 if client.status == status.read then
88 local function send( client, data, len )
90 return vlc.net.send( client.wfd, data, len )
92 return vlc.net.send( client.wfd, data or client.buffer )
96 local function recv( client, len )
98 return vlc.net.recv( client.rfd, len )
100 return vlc.net.recv( client.rfd )
104 local function write( client, data )
105 return vlc.net.write( client.wfd, data or client.buffer )
108 local function read( client, len )
110 return vlc.net.read( client.rfd, len )
112 return vlc.net.read( client.rfd )
116 local function del_client( client )
117 if client.type == client_type.stdio then
118 client:send( "Cannot delete stdin/stdout client.\n" )
121 for i, c in pairs(clients) do
123 if client.type == client_type.net then
124 if client.wfd ~= client.rfd then
125 vlc.net.close( client.rfd )
127 vlc.net.close( client.wfd )
133 vlc.msg.err("couldn't find client to remove.")
136 local function switch_status( client, s )
137 if client.status == s then return end
139 if status_callbacks[s] then
140 status_callbacks[s]( client )
144 -- append a line to a client's (output) buffer
145 local function append( client, string )
146 client.buffer = client.buffer .. string .. "\r\n"
149 local function new_client( h, fd, wfd, t )
150 if fd < 0 then return end
152 if t == client_type.net then
155 else if t == client_type.stdio or t == client_type.fifo then
159 error("Unknown client type", t )
161 local client = { -- data
164 status = status.init,
172 switch_status = switch_status,
175 client:send( "VLC media player "..vlc.misc.version().."\n" )
176 table.insert(clients, client)
177 client:switch_status(status.password)
180 function filter_client( fd, status, status2 )
183 for _, client in pairs(clients) do
184 if client.status == status or client.status == status2 then
185 fd:set( client:fd() )
186 l = math.max( l, client:fd() )
193 local function _listen_tcp( h, host, port )
194 if listeners.tcp and listeners.tcp[host]
195 and listeners.tcp[host][port] then
196 error("Already listening on tcp host `"..host..":"..tostring(port).."'")
198 if not listeners.tcp then
201 if not listeners.tcp[host] then
202 listeners.tcp[host] = {}
204 local listener = vlc.net.listen_tcp( host, port )
205 listeners.tcp[host][port] = listener
206 if not listeners.tcp.list then
207 -- FIXME: if host == "list" we'll have a problem
208 listeners.tcp.list = {}
209 local m = { __mode = "v" } -- week values
210 setmetatable( listeners.tcp.list, m )
212 table.insert( listeners.tcp.list, listener )
215 local function _listen_stdio( h )
217 if listeners.stdio then
218 error("Already listening on stdio")
220 new_client( h, 0, 1, client_type.stdio )
221 listeners.stdio = true
224 local function _listen( h, url )
225 if type(url)==type({}) then
226 for _,u in pairs(url) do
230 vlc.msg.info( "Listening on host \""..url.."\"." )
231 if url == "*console" then
234 u = vlc.net.url_parse( url )
235 h:listen_tcp( u.host, u.port )
240 local function _accept_and_select( h, timeout )
241 local nfds = math.max( filter_client( fds_read, status.read, status.password ),
242 filter_client( fds_write, status.write ) ) + 1
243 if listeners.tcp then
244 for _, listener in pairs(listeners.tcp.list) do
245 for _, fd in pairs({listener:fds()}) do
254 local ret = vlc.net.select( nfds, fds_read, fds_write,
259 for _, client in pairs(clients) do
260 if fds_write:isset( client:fd() ) then
261 table.insert(wclients,client)
263 if fds_read:isset( client:fd() ) then
264 table.insert(rclients,client)
267 if listeners.tcp then
268 for _, listener in pairs(listeners.tcp.list) do
269 for _, fd in pairs({listener:fds()}) do
270 if fds_read:isset(fd) then
271 local afd = listener:accept(0)
272 new_client( h, afd, afd, client_type.net )
279 return wclients, rclients
282 local function destructor( h )
284 for _,client in pairs(clients) do
285 client:send("Shutting down.")
286 if client.type == client_type.tcp then
287 if client.wfd ~= client.rfd then
288 vlc.net.close(client.rfd)
290 vlc.net.close(client.wfd)
295 local function _broadcast( h, msg )
296 for _,client in pairs(clients) do
303 status_callbacks = status_callbacks,
306 listen_tcp = _listen_tcp,
307 listen_stdio = _listen_stdio,
308 accept_and_select = _accept_and_select,
309 broadcast = _broadcast,
314 __metatable = "Nothing to see here. Move along.",