-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncd.lua
More file actions
74 lines (67 loc) · 2.3 KB
/
Copy pathsyncd.lua
File metadata and controls
74 lines (67 loc) · 2.3 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
package.loaded.messagehandler = nil
package.loaded.messagetype = nil
package.loaded.message = nil
package.loaded.socket = nil
package.loaded.syncdconfig = nil
package.loaded.utils = nil
local component = require("component")
local internet = component.isAvailable("internet") and component.internet
local event = require("event")
local utils = require("utils")
local messageHandler = require("messagehandler")
local MessageType = require("messagetype")
local Message = require("message")
local Socket = require("socket")
local config = require("syncdconfig")
local socket
local enableLogging = true
if enableLogging then
function log(str, ...)
local logfile = io.open("/home/socklog.txt", "a")
logfile:write(string.format(str.."\n", ...))
logfile:close()
end
else
function log() end
end
local conversationState
local function conversationReset()
conversationState = {
requestedSubs = {}
}
end
conversationReset()
local function internetReadyHandler(ev, inetAddress, socketId)
if socket:id() == socketId then
local ok, messages = socket:readLenPrep()
if ok then
for _, data in ipairs(messages) do
log("Calling message handler with data: %s", data)
local response = messageHandler(data, conversationState)
if response then
for i = 1, #response do
socket:writeLenPrep(response[i])
end
end
end
else
if not socket:connect(config.addr, config.port) then
event.ignore("internet_ready", internetReadyHandler)
else
-- reset conversation state
conversationReset()
socket:writeLenPrep(Message(MessageType.HELLO, config.protocolVersion, config.clientName):toBytes())
end
end
end
end
function disconnect()
event.ignore("internet_ready", internetReadyHandler)
socket:close()
end
socket = Socket(config.headerFormat, config.maxRetries)
if not socket:connect(config.addr, config.port) then
error("Couldn't connect to the server, check logs for more info")
end
event.listen("internet_ready", internetReadyHandler)
socket:writeLenPrep(Message(MessageType.HELLO, config.protocolVersion, config.clientName):toBytes())