Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dbModem = peripheral.wrap("back") or error("No data modem attached", 0)
- -- PostMonitor.lua - Basic debugging / logging put on a monitor
- local displayModem = dbModem
- function checkMonitor(monitorName)
- if not displayModem.isPresentRemote(monitorName) then
- print("Missing post monitor")
- print("Change 'displayMonitorName' to any of the following values if applicable")
- for _, name in displayModem.getNamesRemote() do
- print(" >'"..name.."'")
- end
- return
- end
- end
- function wrapPostMonitor(monitorName)
- local display = peripheral.wrap(monitorName)
- display.setBackgroundColour(0x8000)
- display.clear()
- display.setCursorPos(1,1)
- display.setCursorBlink(true)
- display.setTextScale(0.5)
- nextLine = function (monitor, screenHeight)
- local newYPos = ({monitor.getCursorPos()})[1] +1;
- monitor.setCursorPos(1, newYPos)
- if (newYPos > screenHeight) then
- monitor.scroll(1)
- end
- end
- writeWrapped = function(postMonitor, log)
- local screenHeight, maxLineLength = postMonitor.getSize()
- local lineLength = 0
- nextLine(postMonitor.display, screenHeight)
- local words = {}
- for word in log:gmatch("%w+") do
- table.insert(words, word)
- end
- for index, word in words do
- lineLength = lineLength + word:len()
- postMonitor.display.write(word.." ")
- if (index ~= #words and lineLength >= maxLineLength) then
- nextLine(postMonitor.display, screenHeight)
- lineLength = 0
- end
- end
- end
- return {
- display = display,
- postLog = function(postMonitor, log)
- postMonitor:resetColor()
- writeWrapped(postMonitor, log)
- end,
- postError = function(postMonitor, log)
- postMonitor:textColor(0x4000)
- writeWrapped(postMonitor, log)
- end,
- resetColor = function(postMonitor)
- postMonitor.display.setBackgroundColour(0x8000)
- postMonitor.display.setTextColour(0x1)
- end,
- textColor = function(postMonitor, color)
- postMonitor.resetColor()
- postMonitor.display.setTextColour(color)
- end,
- paintColor = function(postMonitor, color)
- postMonitor.display.setBackgroundColour(color)
- postMonitor.display.setTextColour(color)
- end,
- }
- end
- function safeWrapPostMonitor(monitorName)
- checkMonitor(monitorName)
- return wrapPostMonitor(monitorName)
- end
- --End of PostMonitor.lua
- local driveMonitor = safeWrapPostMonitor("monitor_6")
- local networkMonitor = safeWrapPostMonitor("monitor_8")
- --Load a list of drives from manifest.txt in a drive below
- local driveManifest = peripheral.wrap("bottom")
- if (not driveManifest.hasData()) then
- driveMonitor:postError("ERR: Missing drive in manifest!")
- return
- end
- driveMonitor:postLog("Found manifest drive with path " .. driveManifest.getMountPath())
- driveMonitor:postLog("Loading manifest...")
- local manifestFile = fs.open(driveManifest.getMountPath() .. "/manifest.txt", "r")
- local manifest = {}
- local i = 0
- for driveName in string.gmatch(manifestFile.readAll(), "(.-),") do
- manifest[driveName] = { listIndex = i, fileExists = false, filePath = "" }
- i=i+1
- end
- manifestFile.close()
- driveMonitor:postLog("Read manifest, found " .. tostring(#manifest) .. " entries")
- --Post initaliser
- driveMonitor.display.setBackgroundColour(0x8000)
- driveMonitor.display.clear()
- driveMonitor.display.setCursorBlink(false)
- driveMonitor.display.setTextScale(0.5)
- local maxNameLength = 20
- driveMonitor.display.setCursorPos(1, 1)
- driveMonitor.display.write("DRIVE |CONNECTED |CONTENTS ")
- --Look for drives that have disks inside
- local discoveredDrives = {}
- for _, peripheralName in pairs(dbModem.getNamesRemote()) do
- local peripheral = peripheral.wrap(peripheralName)
- if (dbModem.hasTypeRemote(peripheralName, "drive") and
- peripheral.isDiskPresent()) then
- discoveredDrives[peripheral.getDiskLabel()] = peripheral
- end
- end
- function checkDrive(driveName)
- if (manifest[driveName] == nil) then
- return false
- end
- local isAvaliable = false
- local yPos = manifest[driveName].listIndex +2
- driveMonitor.display.setCursorPos(1, yPos)
- driveMonitor:resetColor()
- driveMonitor.display.write(driveName)
- driveMonitor.display.setCursorPos(16, yPos)
- driveMonitor.display.write("|")
- if (discoveredDrives[driveName] ~= nil) then
- driveMonitor:paintColor(0x2000)
- else
- driveMonitor:paintColor(0x4000)
- end
- driveMonitor.display.write((" "):rep(10))
- driveMonitor:resetColor()
- driveMonitor.display.setCursorPos(27, yPos)
- driveMonitor.display.write("|")
- if (discoveredDrives[driveName] ~= nil
- and fs.exists(discoveredDrives[driveName].getMountPath().."/"..driveName..".lua")) then
- driveMonitor:paintColor(0x2000)
- isAvaliable = true
- else
- driveMonitor:paintColor(0x4000)
- end
- driveMonitor.display.write((" "):rep(10))
- return isAvaliable
- end
- --Display the discoveredDrives, by checking through the items from the manifest
- for driveName, data in pairs(manifest) do
- --Fetching the drive will also display its state in the event it fails
- checkDrive(driveName)
- end
- --Shipnet.lua - Networking module for ship components,
- local shipnetModem = peripheral.wrap("top") or error("No shipnet modem attached", 0)
- local openRequests = {}
- local shipnetChannel = 1
- shipnetModem.open(shipnetChannel)
- -- print("Awaiting channel "..shipnetChannel.." to be opened...")
- -- local isOpen
- -- repeat
- -- isOpen = shipnetModem.isOpen(shipnetChannel)
- -- sleep()
- -- until isOpen
- -- print("Channel "..shipnetChannel.." is open!")
- function sendPacketRequest(channel, requestInfo, stringData)
- math.randomseed(os.time())
- local requestId = math.floor(math.random()*1000)
- openRequests[requestId] = requestInfo
- shipnetModem.transmit(channel, channel, "request:"..requestId..":"..stringData)
- return requestId
- end
- function sendPacketResponse(channel, requestId, stringData)
- math.randomseed(os.time())
- shipnetModem.transmit(channel, channel, "response:"..requestId..":"..stringData)
- return requestId
- end
- function parseSection(sections, remainingMessage)
- local colonPos = remainingMessage:find(":")
- if (colonPos == nil) then
- return false
- end
- table.insert(sections, remainingMessage:sub(1, colonPos -1))
- return true, remainingMessage:sub(colonPos +1)
- end
- function parsePacket(message)
- local remainingMessage = message
- local parsed = true
- local sections = {}
- for _ = 0, 1 do
- parsed, remainingMessage = parseSection(sections, remainingMessage)
- if not parsed then return false, {} end
- end
- if sections[1] == "response" then --Read the response type error or success
- parsed, remainingMessage = parseSection(sections, remainingMessage)
- if not parsed then return false, {} end
- end
- return true, {
- packetType = sections[1],
- id = sections[2],
- responseCode = sections[3],
- data = remainingMessage
- }
- end
- function pullPacket(isAwaitingResponsePackets)
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if side ~= peripheral.getName(shipnetModem) and channel ~= shipnetChannel then
- return false, 0, {}
- end
- local parsed, parsedMessage = parsePacket(message)
- print(parsed)
- print(parsedMessage.packetType)
- print((isAwaitingResponsePackets and "response" or "request"))
- if parsed and parsedMessage.packetType == (isAwaitingResponsePackets and "response" or "request")
- and (not isAwaitingResponsePackets or openRequests[parsedMessage.id] ~= nil) then
- return true, parsedMessage.id, parsedMessage
- end
- end
- --Packet handlers
- --Request info is used to track what the packet will be, i.e. the file requested
- local onResponse = function (packetId, requestInfo, parsedMessage) end
- local onRequest = function (packetId, parsedMessage)
- local driveName = parsedMessage.data
- if not checkDrive(parsedMessage.data) then
- sendPacketResponse(shipnetChannel, packetId, "error:notfound")
- networkMonitor:postError("File not found for request "..parsedMessage.id.." for "..driveName..".lua")
- return
- end
- local requestedFile = fs.open(discoveredDrives[driveName].getMountPath().."/"..driveName..".lua", "r")
- fileData = requestedFile.readAll()
- requestedFile.close()
- networkMonitor:postLog("Answered request "..parsedMessage.id.." for "..driveName..".lua")
- sendPacketResponse(shipnetChannel, packetId, "success:"..fileData)
- end
- function awaitPackets(isAwaitingResponsePackets)
- while true do
- local foundPacket, packetId, parsedMessage = pullPacket(isAwaitingResponsePackets)
- print("recived")
- if (foundPacket) then
- print("a packet")
- if (isAwaitingResponsePackets) then
- onResponse(packetId, openRequests[parsedMessage.id], parsedMessage)
- else
- print("a request")
- onRequest(packetId, parsedMessage)
- end
- end
- end
- end
- awaitPackets(false)
- --End of Shipnet.lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement