Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local programName = "sortingTurtle"
- local args = {...}
- local dtype = args[1]
- function findDeviceIndex (t, target)
- for i = 1, #t, 1 do
- if t[i].id == target then
- return i
- end
- end
- return 0
- end
- function client ()
- local protocol = args[2] or "sortingTurtle"
- local hostname = args[3] or "sortingTurtleServer"
- local host = rednet.lookup(protocol, hostname)
- if not rednet.send(host, "sortingTurtleConnect", protocol) then
- error("Could not send message.")
- else
- print("Connected!")
- end
- local message
- local sorting = false
- while true do
- if sorting then
- -- sort stuff
- if false then -- if all items have been sorted
- -- might want to add remaining items in chest as part of the message
- if not rednet.send(hosts[i], 'sortingTurtleDone', protocol) then
- error("Could not send message.")
- end
- sorting = false
- end
- else
- _, message, _ = rednet.receive(protocol)
- -- Get Items + Amount from message and sort them
- sorting = true
- end
- end
- end
- function server ()
- local protocol = args[2] or "sortingTurtle"
- local name = args[3] or "sortingTurtleServer"
- local devices = {}
- rednet.host(protocol, name)
- print ("Server running!")
- while true do
- local deviceID
- local message
- -- waiting for sortingTurtle to connect
- deviceID, message, _ = rednet.receive(protocol)
- if message == "sortingTurtleConnect" then
- print(deviceID .. " connected!")
- end
- -- wait for input, then send them to be sorted
- -- Send commands to sortingTurtle
- if not rednet.send(deviceID, message, protocol) then
- error("Could not send message.")
- end
- -- wait for response (sortingTurtleDone)
- -- then wait for input again
- end
- rednet.unhost(protocol)
- end
- function main ()
- if not rednet.isOpen("back") then
- error("Could not open modem 'back'")
- return
- end
- if dtype == "client" then
- client()
- elseif dtype == "server" then
- server()
- elseif dtype == "update" then
- shell.run("pastebin", "run", "FuQ3WvPs HmhXJtBm " .. programName)
- elseif dtype == "help" then
- prettyPrint ("", "Arguments:\n - " .. programName .. " server <protocol> <hostname>\n - " .. programName .. " client <protocol> <hostname> <username>\n - " .. programName .. " update\n - " .. programName .. " help\n", nil, "8")
- else
- error("Please specify the device type as 'server' or 'client' by passing it as the first argument.")
- end
- end
- rednet.open("back")
- main()
- rednet.close("back")
Add Comment
Please, Sign In to add comment