diff --git a/inferium-harvester.lua b/inferium-harvester.lua index 1a9c455..bff75f8 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -1,18 +1,21 @@ +local net = require('libs/net') local utils = require('libs/utils') local turtleUtils = require('libs/turtle-utils') local config = require('config/harvesting') -local VERSION = "1.0.1" +local VERSION = "2.0.0" +local INFERIUM_SERVER = 'inferium.com' local IDLE_TIME = 2 local WAIT_ITEM_IDLE_TIME = 5 +local NETWORK_TIMEOUT = 4 local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2 local MIN_FREE_SLOTS_BEFORE_COMPACT = 4 -local INFERIUM_QUERY_PORT = 111 -local INFERIUM_REPLY_PORT = 112 -- a table with the list of current crops local localPlan = nil +local previouslyFetchedRemotePlan = nil + -- UTILS local function removeFirst(t, x) @@ -73,7 +76,7 @@ local function getSeedNameFromCropName(cropName) return string.gsub(cropName, 'crop', 'seeds') end -function isSeed(item) +local function isSeed(item) local tags = item and item.tags or {} return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false end @@ -263,37 +266,21 @@ local function retrieveLocalPlan() goBackToHome() end --- TODO: fetch retry + timeout ? local function fetchRemotePlan() - local modem = peripheral.find("modem") - - if not modem then - error('no modem found') - end - - modem.open(INFERIUM_REPLY_PORT) - local message = { type = 'getplan', payload = { computerId = os.getComputerID() } } - modem.transmit(INFERIUM_QUERY_PORT, INFERIUM_REPLY_PORT, textutils.serialize(message)) - local channel, replyRawMessage - repeat - _, _, channel, _, replyRawMessage = os.pullEvent("modem_message") - until channel == INFERIUM_REPLY_PORT + while true do + local replyMessage, _ = net.sendQuery(INFERIUM_SERVER, message, NETWORK_TIMEOUT) - modem.close(INFERIUM_REPLY_PORT) + if replyMessage and replyMessage.payload then + previouslyFetchedRemotePlan = replyMessage.payload + return replyMessage.payload + elseif previouslyFetchedRemotePlan then + return previouslyFetchedRemotePlan + end - if not replyRawMessage then - error('empty response from the server') + os.sleep(IDLE_TIME) end - - local replyMessage = textutils.unserialize(replyRawMessage) - - if replyMessage and replyMessage.payload then - return replyMessage.payload - end - - error('message received but there is no payload: ' .. replyRawMessage) end local function removeSeeds(seeds) @@ -434,6 +421,8 @@ end -- Main procedure local function main() + net.openRednet() + print("Starting Trap's inferium harvester v" .. VERSION) turtleUtils.refuelAllFromInventory() @@ -454,6 +443,8 @@ local function main() harvestProcedure() goBackToHome() end + + net.closeRednet() end main() \ No newline at end of file diff --git a/inferium-server.lua b/inferium-server.lua index 5009167..e80285d 100644 --- a/inferium-server.lua +++ b/inferium-server.lua @@ -1,58 +1,72 @@ +local net = require('libs/net') local inferiumPlans = require('config/inferium-plans') -- temporary default plan -local VERSION = "0.3.0" +local INFERIUM_SERVER = 'inferium.com' +local UPGRADE_SCRIPT = '/upgrade.lua' +local VERSION = "0.5.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 defaultPlan = inferiumPlans.default or error('no default plan provided in config', 0) local function getPlanForComputer(computerId) return inferiumPlans[tostring(computerId)] end -local function messageReceived(message) +local function getPlan(computerId) + if not computerId == nil then + print('getplan error: no computerId found') + 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 + +local ROUTES = { + ['getplan'] = function(_, computerId) return getPlan(computerId) end, + ['exit-server'] = function(_, _, stopServer) stopServer() ; return true end, + ['upgrade-server'] = function() shell.execute(UPGRADE_SCRIPT) ; return true end, +} + +local function handleMessage(message, computerId) 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 - } + return getPlan(computerId) + elseif message.type == 'exit-server' then + stopServer() + return true + elseif message.type == 'upgrade-server' then + shell.execute('/upgrade.lua') + return true end end +net.openRednet() 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 +net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer) + if type(message) ~= table then + print('error: malformed message received', textutils.serialize(message)) + return {} end -end \ No newline at end of file + + local router = ROUTES[message.type] + + if not router then + print('warning: unknown type of message received', message.type) + return {} + end + + return router(message, computerId, stopServer) +end) +print('> server stopped') + +net.closeRednet() +print('> rednet closed') \ No newline at end of file