Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local internet = require"internet"
- local serialization = require"serialization"
- local thread = require"thread"
- local on = require"opennet"
- local unicode = require"unicode"
- local term = require"term"
- local component = require"component"
- -- Check for all required libs
- assert(internet, "Interned card expected")
- assert(thread, "Thread library expected")
- assert(on, "Opennet library expected")
- local unpack = table.unpack
- local gpu = component.gpu
- -- Init
- local ip = on.getIP()
- thread.init()
- -- Additional function actions
- local proc = {
- request = function(...)
- local s = {}
- for chunk in internet.request(...) do
- table.insert(s,chunk)
- end
- io.write("\n| Chunks received: "..#s, " \"".. ((s[1] and s[1]:sub(1,10)) or "") .."\"...")
- return s
- end
- }
- local function processReceived(r)
- -- Check if this message from internet
- if r and r[2] == "internet" and type(r[3])=="string" then
- local obj = serialization.unserialize(r[3])
- local fncName = obj.__functionName
- obj.__functionName = nil
- io.write("\n| Calling function: " .. fncName .. "() ...")
- local returnedObj
- if proc[fncName] then
- returnedObj = {proc[fncName](unpack(obj))}
- else
- local fnc = internet[fncName]
- if type(fnc)=="function" then
- returnedObj = {fnc(unpack(obj))}
- io.write("\n| Returned: ", unpack(returnedObj))
- end
- end
- io.write("\n| Sending back...\n")
- on.send(r[1], serialization.serialize(returnedObj))
- end
- end
- -- Interface
- gpu.setResolution(gpu.maxResolution())
- local W,H = gpu.getResolution()
- local loaderSymbols = "◓◑◒◐"
- local loadStep = unicode.len(loaderSymbols)
- -- Shows small loading circle on screen
- local function workIndicator(isWorking)
- local col = isWorking and 0xFF0000 or 0x00FF00
- local oldCol = gpu.getForeground()
- gpu.setForeground(col)
- gpu.fill(W, 1, 1, 1, unicode.sub(loaderSymbols, loadStep, loadStep))
- gpu.setForeground(oldCol)
- loadStep = (loadStep % unicode.len(loaderSymbols)) + 1
- end
- -- Receiving loop
- while true do
- local r = {on.receive(0.5)}
- -- Write log
- if #r > 0 then
- workIndicator(true)
- io.write("\n| Received: ")
- for _,v in pairs(r) do io.write(tostring(v):sub(1,20),(#tostring(v)>20) and "..." or ""," ") end
- end
- -- Process it
- local ok, err = pcall(processReceived,r)
- if not ok then
- io.stderr:write("\nError during processing:"..tostring(err).."\n")
- end
- -- Show that we are working
- workIndicator()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement