summaryrefslogtreecommitdiffstats
path: root/ctl-lib/ctl-lua-utils.c
blob: 5c96226274aefea3f51c20fc268e72976abcefcb (plain)
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
152
153
154
155
156
157
158
/*
 * Copyright (C) 2016 \"IoT.bzh\"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
  Licensed under the Apache License, Version 2.0 (the \"License\");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an \"AS IS\" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ref:
 *  (manual) https://www.lua.org/manual/5.3/manual.html
 *  (lua->C) http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm#_Anatomy_of_a_Lua_Call
 *  (lua/C Var) http://acamara.es/blog/2012/08/passing-variables-from-lua-5-2-to-c-and-vice-versa/
 *  (Lua/C Lib)https://john.nachtimwald.com/2014/07/12/wrapping-a-c-library-in-lua/
 *  (Lua/C Table) https://gist.github.com/SONIC3D/10388137
 *  (Lua/C Nested table) https://stackoverflow.com/questions/45699144/lua-nested-table-from-lua-to-c
 *  (Lua/C Wrapper) https://stackoverflow.com/questions/45699950/lua-passing-handle-to-function-created-with-newlib
 *
 */

const char *lua_utils = "--===================================================\n\
--=  Niklas Frykholm\n\
-- basically if user tries to create global variable\n\
-- the system will not let them!!\n\
-- call GLOBAL_lock(_G)\n\
-- \n\
--===================================================\n\
function GLOBAL_lock(t)\n\
  local mt = getmetatable(t) or {}\n\
  mt.__newindex = lock_new_index\n\
  setmetatable(t, mt)\n\
end\n\
\n\
--===================================================\n\
-- call GLOBAL_unlock(_G)\n\
-- to change things back to normal.\n\
--===================================================\n\
function GLOBAL_unlock(t)\n\
  local mt = getmetatable(t) or {}\n\
  mt.__newindex = unlock_new_index\n\
  setmetatable(t, mt)\n\
end\n\
\n\
function lock_new_index(t, k, v)\n\
  if (string.sub(k,1,1) ~= \"_\") then\n\
    GLOBAL_unlock(_G)\n\
    error(\"GLOBALS are locked -- \" .. k ..\n\
          \" must be declared local or prefix with '_' for globals.\", 2)\n\
  else\n\
    rawset(t, k, v)\n\
  end\n\
end\n\
\n\
function unlock_new_index(t, k, v)\n\
  rawset(t, k, v)\n\
end\n\
\n\
-- return serialised version of printable table\n\
function Dump_Table(o)\n\
   if type(o) == 'table' then\n\
      local s = '{ '\n\
      for k,v in pairs(o) do\n\
         if type(k) ~= 'number' then k = '\"'..k..'\"' end\n\
         s = s .. '['..k..'] = ' .. Dump_Table(v) .. ','\
      end\n\
      return s .. '} '\n\
   else\n\
      return tostring(o)\n\
   end\n\
end\n\
\n\
-- simulate C prinf function\n\
printf = function(s,...)\n\
    io.write(s:format(...))\n\
    io.write(\"\")\n\
    return\n\
end\n\
\n\
function sleep(n)\n\
    os.execute(\"sleep \" .. tonumber(n))\n\
end\n\
\n\
function table_size(t)\n\
	local size = 0\n\
	for _ in pairs(t) do\n\
		size = size + 1\n\
	end\n\
	return size\n\
end\n\
function deep_copy(orig)\n\
    local orig_type = type(orig)\n\
    local copy\n\
    if orig_type == 'table' then\n\
        copy = {}\n\
        for orig_key, orig_value in next, orig, nil do\n\
            copy[deep_copy(orig_key)] = deep_copy(orig_value)\n\
        end\n\
        setmetatable(copy, deep_copy(getmetatable(orig)))\n\
    else -- number, string, boolean, etc\n\
        copy = orig\n\
    end\n\
    return copy\n\
end\n\
function table_eq(table1, table2)\n\
	local avoid_loops = {}\n\
	local function recurse(t1, t2)\n\
	-- compare value types\n\
	if type(t1) ~= type(t2) then return false end\n\
	-- Base case: compare simple values\n\
	if type(t1) ~= \"table\" then return t1 == t2 end\n\
	-- Now, on to tables.\n\
	-- First, let's avoid looping forever.\n\
	if avoid_loops[t1] then return avoid_loops[t1] == t2 end\n\
	avoid_loops[t1] = t2\n\
	-- Copy keys from t2\n\
	local t2keys = {}\n\
	local t2tablekeys = {}\n\
	for k, _ in pairs(t2) do\n\
	if type(k) == \"table\" then table.insert(t2tablekeys, k) end\n\
	t2keys[k] = true\n\
	end\n\
	-- Let's iterate keys from t1\n\
	for k1, v1 in pairs(t1) do\n\
	local v2 = t2[k1]\n\
	if type(k1) == \"table\" then\n\
		-- if key is a table, we need to find an equivalent one.\n\
		local ok = false\n\
		for i, tk in ipairs(t2tablekeys) do\n\
		if table_eq(k1, tk) and recurse(v1, t2[tk]) then\n\
		table.remove(t2tablekeys, i)\n\
		t2keys[tk] = nil\n\
		ok = true\n\
		break\n\
		end\n\
		end\n\
		if not ok then return false end\n\
	else\n\
		-- t1 has a key which t2 doesn't have, fail.\n\
		if v2 == nil then return false end\n\
		t2keys[k1] = nil\n\
		if not recurse(v1, v2) then return false end\n\
	end\n\
	end\n\
	-- if t2 has a key which t1 doesn't have, fail.\n\
	if next(t2keys) then return false end\n\
	return true\n\
	end\n\
	return recurse(table1, table2)\n\
end\n\
-- lock global variable\n\
GLOBAL_lock(_G)\n\
";
n class="p">{ cbKey, ok := e.cbArr[evName] if !ok { return fmt.Errorf("No event registered to such name") } // FIXME - NOT TESTED if id >= len(cbKey) { return fmt.Errorf("Invalid id") } else if id == len(cbKey) { e.cbArr[evName] = cbKey[:id-1] } else { e.cbArr[evName] = cbKey[id : id+1] } return nil } // GetEvents returns the Syncthing events func (e *Events) getEvents(since int) ([]STEvent, error) { var data []byte ev := []STEvent{} url := "events" if since != -1 { url += "?since=" + strconv.Itoa(since) } if err := e.st.client.HTTPGet(url, &data); err != nil { return ev, err } err := json.Unmarshal(data, &ev) return ev, err } // Loop to monitor Syncthing events func (e *Events) monitorLoop() { e.log.Infof("Event monitoring running...") since := 0 cntErrConn := 0 cntErrRetry := 1 for { select { case <-e.stop: e.log.Infof("Event monitoring exited") return case <-time.After(e.MonitorTime * time.Millisecond): if !e.st.Connected { cntErrConn++ time.Sleep(time.Second) if cntErrConn > cntErrRetry { e.log.Error("ST Event monitor: ST connection down") cntErrConn = 0 cntErrRetry *= 2 if _, err := e.getEvents(since); err == nil { e.st.Connected = true cntErrRetry = 1 // XXX - should we reset since value ? goto readEvent } } continue } readEvent: stEvArr, err := e.getEvents(since) if err != nil { e.log.Errorf("Syncthing Get Events: %v", err) e.st.Connected = false continue } // Process events for _, stEv := range stEvArr { since = stEv.SubscriptionID if e.Debug { e.log.Warnf("ST EVENT: %d %s\n %v", stEv.GlobalID, stEv.Type, stEv) } cbKey, ok := e.cbArr[stEv.Type] if !ok { continue } evData := Event{ Type: stEv.Type, Time: stEv.Time, } // Decode Events // FIXME: re-define data struct for each events // instead of map of string and use JSON marshing/unmarshing fID := "" evData.Data = make(map[string]string) switch stEv.Type { case EventFolderCompletion: fID = convString(stEv.Data["folder"]) evData.Data["completion"] = convFloat64(stEv.Data["completion"]) case EventFolderSummary: fID = convString(stEv.Data["folder"]) evData.Data["needBytes"] = convInt64(stEv.Data["needBytes"]) evData.Data["state"] = convString(stEv.Data["state"]) case EventFolderPaused, EventFolderResumed: fID = convString(stEv.Data["id"]) evData.Data["label"] = convString(stEv.Data["label"]) case EventFolderErrors: fID = convString(stEv.Data["folder"]) // TODO decode array evData.Data["errors"] = convString(stEv.Data["errors"]) case EventStateChanged: fID = convString(stEv.Data["folder"]) evData.Data["from"] = convString(stEv.Data["from"]) evData.Data["to"] = convString(stEv.Data["to"]) default: e.log.Warnf("Unsupported event type") } if fID != "" { evData.Data["id"] = fID } // Call all registered callbacks for _, c := range cbKey { if e.Debug { e.log.Warnf("EVENT CB fID=%s, filterID=%s", fID, c.filterID) } // Call when filterID is not set or when it matches if c.filterID == "" || (fID != "" && fID == c.filterID) { c.cb(evData, c.data) } } } } } } func convString(d interface{}) string { return d.(string) } func convFloat64(d interface{}) string { return strconv.FormatFloat(d.(float64), 'f', -1, 64) } func convInt64(d interface{}) string { return strconv.FormatInt(d.(int64), 10) }