-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener.lua
More file actions
52 lines (45 loc) · 1.64 KB
/
listener.lua
File metadata and controls
52 lines (45 loc) · 1.64 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
-- listener.lua
-- Receives encrypted modem messages and sends back a confirmation
-- Intended to be run on another ComputerCraft computer
local statusNet, Net = pcall(require, "net_module")
if not statusNet or not Net then
error("Could not load net_module.lua")
end
local statusLogger, Logger = pcall(require, "logger")
if not statusLogger or not Logger then
-- Logging is optional, fall back to stub if not available
Logger = {logEvent = function() end}
end
local FREQ = 1 -- same frequency as main.lua
-- detect modem
local modem
for _, side in ipairs(peripheral.getNames()) do
if peripheral.getType(side) == "modem" then
modem = peripheral.wrap(side)
break
end
end
if not modem then
error("No modem found. Attach a modem peripheral.")
end
modem.open(FREQ)
Logger.logEvent("Listener started on frequency " .. FREQ)
print("Listening on frequency " .. FREQ)
while true do
local event, side, freq, replyChannel, message = os.pullEvent("modem_message")
if freq == FREQ then
-- decode incoming message
local msgHash = string.sub(message, -4)
local encMsg = string.sub(message, 1, -5)
local text = Net.decryptMessage(encMsg, msgHash)
Logger.logEvent("Received message: " .. text)
print("Received: " .. text)
-- send confirmation
local response = "4321"
local responseHash = Net.enhancedHash(response)
local encResponse = Net.encryptMessage(response, responseHash)
modem.transmit(FREQ, FREQ, encResponse .. responseHash)
Logger.logEvent("Sent confirmation: " .. response)
print("Sent confirmation")
end
end