-- -- port_scan.lua -- -- One-shot diagnostic for the model-test data center. Run on the CHAMBER RTU -- computer with a SELF-AWARE model sitting in a chamber (Cow/Enderman). -- -- port_scan -- -- It (1) dumps every barrel + its contents and every IO port, (2) lists chambers -- that hold a model, then (3) takes ONE model out to a buffer barrel and tries -- pushing it into EACH data_center_io_port, reporting which accepts - since CC -- can't read a port's mode, this is how we find the real model-input port. It -- puts the model back when done. -- local CHAMBER = "hostilenetworks:sim_chamber" local PORT = "hostilenetworks:data_center_io_port" local BARREL = "minecraft:barrel" local MODEL = "data_model" local CAT = "prediction_matrix" local out = fs.open("port_scan.txt", "w") local function w(s) out.write(tostring(s).."\n"); print(s) end local function typeOf(n) return peripheral.getType(n) end local function names(t) local o = {} for _, n in ipairs(peripheral.getNames()) do if typeOf(n) == t then o[#o+1] = n end end table.sort(o); return o end local ports = names(PORT) local barrels = names(BARREL) local chambers = names(CHAMBER) w("=== port_scan " .. os.date("%H:%M:%S") .. " ===") w("io ports : " .. #ports) for _, n in ipairs(ports) do w(" " .. n) end w("barrels : " .. #barrels) for _, n in ipairs(barrels) do local p = peripheral.wrap(n) local first, total = "(empty)", 0 local ok, list = pcall(p.list) if ok and list then for s, it in pairs(list) do total = total + it.count if first == "(empty)" then local okd, d = pcall(p.getItemDetail, s) first = (okd and d and d.displayName) or it.name end end end w(string.format(" %-24s %s x%d", n, first, total)) end -- find a chamber holding a model local function modelSlot(p) local ok, list = pcall(p.list) if ok and list then for s, it in pairs(list) do if it.name and it.name:find(MODEL) then return s, it end end end end local src, srcName, srcSlot, srcDisp for _, n in ipairs(chambers) do local p = peripheral.wrap(n) local slot = modelSlot(p) if slot then local okd, d = pcall(p.getItemDetail, slot) src, srcName, srcSlot = p, n, slot srcDisp = (okd and d and d.displayName) or "?" break end end w("") if not src then w("no chamber holds a model - load one first."); out.close(); return end w("test model: " .. srcDisp .. " in " .. srcName .. " slot " .. srcSlot) w("(make sure THIS is a Self-Aware model, else the result is meaningless)") -- pick an empty barrel as a buffer local buffer for _, n in ipairs(barrels) do local p = peripheral.wrap(n) local ok, list = pcall(p.list) if ok and (not list or next(list) == nil) then buffer = n; break end end if not buffer then w("no empty barrel to use as a buffer - free one up."); out.close(); return end w("buffer barrel: " .. buffer) -- stage the model into the buffer local ok1, m1 = pcall(src.pushItems, buffer, srcSlot, 1) if not (ok1 and m1 and m1 > 0) then w("FAILED to move model into buffer: " .. tostring(m1) .. " (chamber & buffer on same network?)") out.close(); return end local bp = peripheral.wrap(buffer) local function bufSlot() local ok, list = pcall(bp.list) if ok and list then for s, it in pairs(list) do if it.name:find(MODEL) then return s end end end end w("") w("--- pushing the model at each IO port ---") local winner for _, port in ipairs(ports) do local bs = bufSlot() if bs then local okp, moved = pcall(bp.pushItems, port, bs, 1) if not okp then w(string.format(" %-36s ERROR %s", port, tostring(moved))) elseif moved and moved > 0 then w(string.format(" %-36s ACCEPTED (moved %d) <== MODEL PORT", port, moved)) winner = port -- pull it back out of the port to the buffer local pp = peripheral.wrap(port) local ok, list = pcall(pp.list) if ok and list then for s, it in pairs(list) do if it.name:find(MODEL) then pcall(pp.pushItems, buffer, s) end end end else w(string.format(" %-36s refused (moved 0)", port)) end end end -- return the model to its chamber local bs = bufSlot() if bs then pcall(bp.pushItems, srcName, bs) end w("") if winner then w("=> the model port is: " .. winner) w(" set chamber_test_port to that in configure.") else w("=> NO port accepted the model.") w(" if this was a Self-Aware model, the DC is refusing input:") w(" check it's fully formed + POWERED, and its model bank isn't full.") end out.close() print("") print("wrote port_scan.txt")