-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqpushy.lua
executable file
·99 lines (90 loc) · 2.91 KB
/
cqpushy.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/dbus-run-session lua5.3
-- package.path = "/path/to/lua-cqueues-pushy/?.lua;"..package.path
package.path = "/opt/rame/rameplayer-backend/?.lua;"..package.path
local posix = require 'posix'
local socket = require 'socket'
local plutils = require 'pl.utils'
local cqueues = require 'cqueues'
local httpd = require 'cqp.httpd'
local process = require 'cqp.process'
local posixfd = require 'cqp.posixfd'
local RAME = require 'rame.rame'
local Item = require 'rame.item'
local function start_player()
RAME:load_plugins(
"/opt/rame/rameplayer-backend/plugins/",
RAME.config.settings_path .. "/plugins/",
"./plugins/")
RAME:hook("early_init")
httpd.new{
local_addr = "0.0.0.0",
port = 8000,
uri = function(ctx, reply)
reply.headers["Access-Control-Allow-Origin"] = "*"
if ctx.method == "OPTIONS" then
local methods = "GET,OPTIONS,POST,PUT,DELETE"
reply.headers["Allow"] = methods
reply.headers["Access-Control-Allow-Methods"] = methods
reply.headers["Access-Control-Allow-Headers"] = "Content-Type"
reply.headers["Cache-Control"] = "public,max-age=600"
return 200
end
reply.headers["Cache-Control"] = "no-cache"
reply.headers["Pragma"] = "no-cache"
return ctx:route(reply, RAME.rest, true)
end
}
RAME:hook("init")
--RAME.root:add(Item.new{id="movies", title="Movies", uri="file:///pub/movies"})
for _, p in pairs(RAME.plugins) do
if p.main then cqueues.running():wrap(p.main) end
end
cqueues.running():wrap(RAME.main)
end
local function exit_handler()
local signal = require 'cqueues.signal'
signal.block(signal.SIGKILL, signal.SIGTERM, signal.SIGHUP)
local s = signal.listen(signal.SIGKILL, signal.SIGTERM, signal.SIGHUP)
cqueues.poll(2)
plutils.writefile('/var/run/cqpushy.pid', tostring(posix.getpid('pid')))
s:wait()
RAME.running = false
error("exit")
end
local function update_ip()
local nlfd, err = posix.socket(posix.AF_NETLINK, posix.SOCK_RAW, posix.NETLINK_ROUTE)
posix.fcntl(nlfd, posix.F_SETFD, posix.FD_CLOEXEC)
posix.fcntl(nlfd, posix.F_SETFL, posix.O_NONBLOCK)
posix.bind(nlfd, {family=posix.AF_NETLINK, pid=posix.getpid("pid"), groups=0x440}) --groups=RTMGRP_IPV4_ROUTE|RTMGRP_IPV6_ROUTE
local nlsock = posixfd.openfd(nlfd, 'r')
local timeout = 0.05
while true do
if cqueues.poll(nlsock, timeout) == nlsock then
-- Just read the netlink data. No real processing,
-- instead just refresh the IP.
nlsock:read(16*1024)
timeout = 0.05
else
local s = socket.udp()
s:setpeername("8.8.8.8", 80)
local ip, port = s:getsockname()
s:close()
local ip_str = tostring(ip)
if ip_str ~= RAME.system.ip() then
RAME.log.info("IP "..ip_str)
end
RAME.system.ip(ip_str)
timeout = nil
end
end
end
-- Map local directory to be visible
local loop = cqueues.new()
loop:wrap(exit_handler)
loop:wrap(start_player)
--loop:wrap(update_ip)
for e in loop:errors() do
if not RAME.running then break end
RAME.log.error(e)
end
process.killall(9)