Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("Apnet")
- Apnet.Initialize("Terminal", "false")
- local function getTokens(str)
- local result = {}
- local i = 0
- for token in string.gmatch(str, "[a-zA-Z0-9*]+") do
- i = i + 1
- result[i] = token
- end
- result.Count = i
- return result
- end
- local function handleDetails(tokens)
- local details = Apnet.GetComputerDetails()
- print("ID: " .. details.ComputerId)
- print("Label: " .. details.ComputerLabel)
- print("Type: " .. details.ComputerType)
- end
- local function handleNext(tokens)
- local msg = Apnet.GetNextInboundMessage()
- if (msg == nil) then
- print("nil")
- elseif (tokens.Count == 1) then
- print(msg.Content)
- elseif (tokens[2] == "details") then
- print(textutils.serialize(msg))
- end
- end
- local function handleSend(tokens)
- if (tokens.Count < 6) then
- print("Not enough arguments")
- else
- local msg = Apnet.NewMessage()
- if (tokens[2] ~= "*") then
- msg.Destination.ComputerId = tonumber(tokens[2])
- else
- msg.Destination.ComputerId = tokens[2]
- end
- msg.Destination.ComputerLabel = tokens[3]
- msg.Destination.ComputerType = tokens[4]
- msg.MessageType = tokens[5]
- msg.MessageSubType = tokens[6]
- if (tokens.Count > 6) then
- msg.Content = tokens[7]
- if (tokens.Count > 7) then
- for i = 8, tokens.Count do
- msg.Content = msg.Content .. " " .. tokens[i]
- end
- end
- end
- Apnet.Send(msg)
- end
- end
- local function processInput(tokens)
- if (tokens[1] == "details") then
- handleDetails(tokens)
- elseif (tokens[1] == "next") then
- handleNext(tokens)
- elseif (tokens[1] == "send") then
- handleSend(tokens)
- elseif (tokens[1] ~= "exit") then
- print("Unrecognized command")
- end
- end
- local function inputLoop()
- local string input = "default"
- while (input ~= "exit") do
- print("Enter a command")
- input = read()
- local tokens = getTokens(input)
- processInput(tokens)
- end
- end
- parallel.waitForAny(Apnet.Listen, inputLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement