58 lines
1.6 KiB
Lua
58 lines
1.6 KiB
Lua
local inferiumPlans = require('config/inferium-plans') -- temporary default plan
|
|
|
|
local VERSION = "0.3.0"
|
|
|
|
local INFERIUM_QUERY_PORT = 111
|
|
local INFERIUM_REPLY_PORT = 112
|
|
|
|
local defaultPlan = inferiumPlans.default or error('no default plan provided in config')
|
|
|
|
local modem = peripheral.find("modem") or error("No modem attached", 0)
|
|
|
|
modem.open(INFERIUM_QUERY_PORT)
|
|
|
|
local function getPlanForComputer(computerId)
|
|
return inferiumPlans[tostring(computerId)]
|
|
end
|
|
|
|
local function messageReceived(message)
|
|
if message.type == 'getplan' then
|
|
local payload = message.payload or {}
|
|
local computerId = payload.computerId
|
|
|
|
if not computerId == nil then
|
|
print('getplan error: no computerId found in received payload')
|
|
return nil
|
|
end
|
|
|
|
local plan = getPlanForComputer(computerId)
|
|
|
|
if not plan then
|
|
print('getplan warning: no plan found for computerID ' .. tostring(computerId))
|
|
plan = defaultPlan
|
|
end
|
|
|
|
return {
|
|
type = "getplan/response",
|
|
payload = plan
|
|
}
|
|
end
|
|
end
|
|
|
|
print('> inferium server v' .. VERSION .. ' listening on port ' .. INFERIUM_QUERY_PORT)
|
|
|
|
while true do
|
|
local _, _, channel, replyChannel, rawMessage, _ = os.pullEvent("modem_message")
|
|
|
|
if channel == INFERIUM_QUERY_PORT and replyChannel == INFERIUM_REPLY_PORT then
|
|
local message = textutils.unserialize(rawMessage)
|
|
local replyMessage = messageReceived(message)
|
|
|
|
if replyMessage then
|
|
local rawReplyMessage = textutils.serialize(replyMessage)
|
|
modem.transmit(INFERIUM_REPLY_PORT, INFERIUM_QUERY_PORT, rawReplyMessage)
|
|
else
|
|
print('warning: unknown message received')
|
|
end
|
|
end
|
|
end |