Repository navigation
Expand file tree
/
Copy pathmaxi-server.lua
More file actions
151 lines (136 loc) · 4.5 KB
/
Copy pathmaxi-server.lua
File metadata and controls
151 lines (136 loc) · 4.5 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
--
-- maxi-server: comprehensive lsdbus server showing all major features
--
-- Demonstrates: multiple typed methods (in/out args, arrays, dicts, variants,
-- error propagation), read/write/write-only properties, signals, multiple
-- object paths, emitAllPropertiesChanged with filter, periodic signal emit,
-- SIGINT for clean exit, SIGHUP for reload.
--
-- Use maxi-client.lua or maxi-client-async.lua to exercise it, or:
--
-- lsdb-info show lsdbus.maxi,/1
-- lsdb-call lsdbus.maxi,/1,lsdbus.maxi,pow "{x=7}"
-- lsdb-call lsdbus.maxi,/1,lsdbus.maxi,Shutdown
local lsdb = require("lsdbus")
local u = require("utils")
local interface = {
name="lsdbus.maxi",
methods={
Raise={
handler=function(vt)
print("emitting HighWater")
vt:emit('HighWater', 99, "alarm")
end
},
thunk={
handler=function(vt) u.pp(vt._bus:context()) end
},
pow={
{direction="in", name="x", type="i"},
{direction="out", name="result", type="i"},
handler=function(_,x) print("pow of ", x); return x^2 end
},
twoin={
{direction="in", name="x", type="i"},
{direction="in", name="y", type="a{ss}"},
handler=function(_,x,y) end
},
twoout={
{direction="out", name="x", type="i"},
{direction="out", name="y", type="a{ss}"},
handler=function(_) return 333, { a=1, b=2, c=3 } end
},
concat={
{direction="in", name="a", type="s"},
{direction="in", name="b", type="s"},
{direction="out", name="result", type="s"},
handler=function(_,a,b) return a..b end
},
getarray={
{direction="in", name="size", type="i"},
{direction="out", name="result", type="a{is}"},
handler=function(_,size)
local x = {}
for i=1,size do x[#x+1]=i end
return x
end
},
getdict={
{direction="in", name="size", type="i"},
{direction="out", name="result", type="a{ss}"},
handler=function(_,size)
local x = {}
for i=1,size do
x['key'..tostring(i)]='value'..tostring(i)
end
return x
end
},
Shutdown = {
handler=function(vt) print("shutting down"); vt._bus:exit_loop() end
},
Fail={
handler=function() error("messed up!") end -- [expected] generic error → org.freedesktop.DBus.Error.Failed
},
FailWithDBusError={
handler=function() error("lsdbus.maxi.BananaPeelSlip|argh!") end -- [expected] "name|msg" format → custom D-Bus error name
},
},
properties={
Bar={
access="readwrite",
type="y",
get=function(vt) return vt.bar or 255 end,
set=function(vt,val)
print("setting Bar to ", val)
vt.bar = val
vt:emitPropertiesChanged("Bar", "Date")
end
},
Date={
access="read",
type="s",
get=function() return os.date() end,
},
Wronly={
access="write",
type="s",
set=function(_, x) print(x) end,
},
Fail={ -- [expected] get/set both error; skipped by filter_props below
access="readwrite",
type="b",
get=function() error("lsdbus.maxi.BOOM|it exploded") end,
set=function(_,_) error("lsdbus.maxi.CRASH|it crashed") end
},
},
signals = {
HighWater={
{ name="level", type="u" },
{ name="state", type="s" }
},
Dunk={},
}
}
-- Fail property getter intentionally errors; exclude it from emitAllPropertiesChanged
local function filter_props(p, _) return p ~= "Fail" end
local b = lsdb.open('user')
b:request_name("lsdbus.maxi")
local vt1 = lsdb.server.new(b, "/1", interface)
local vt2 = lsdb.server.new(b, "/2", interface)
vt1:emitAllPropertiesChanged(filter_props)
vt2:emitAllPropertiesChanged(filter_props)
local function reload()
print("SIGHUP: reloading")
vt1:unref(); vt2:unref()
vt1 = lsdb.server.new(b, "/1", interface)
vt2 = lsdb.server.new(b, "/2", interface)
vt1:emitAllPropertiesChanged(filter_props)
vt2:emitAllPropertiesChanged(filter_props)
end
local sig_int = b:add_signal(lsdb.SIGINT, function() print("SIGINT: exiting"); b:exit_loop() end)
local sig_hup = b:add_signal(lsdb.SIGHUP, reload)
local periodic = b:add_periodic(5*1000^2, 0, function()
vt1:emit('HighWater', 42, "nominal")
end)
b:loop()