Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local initd = false;
- local api = {
- meta = {
- connect = {}
- },
- commands = {},
- }
- function api.init (channel, protocol, side)
- api.channel = channel
- api.hostname = os.getComputerLabel() or ("computer_" .. math.random(1, 2^31-1));
- api.protocol = protocol;
- api.modem = peripheral.wrap(side);
- api.modem.open(channel);
- initd = true;
- end
- function api.start_listener ()
- assert(initd, "init needs to be called before starting listener")
- coroutine.resume(coroutine.create(function()
- while 1 do
- local event, modemSide, senderChannel,
- replyChannel, message, senderDistance = os.pullEvent("modem_message")
- api.proccess_message(modemSide, senderChannel,
- replyChannel, message, senderDistance);
- end
- end))
- end
- function api.send_message (recipient, channel, command, content)
- api.modem.transmit(channel, 1, {protocol=api.protocol, recipient=recipient,
- from=api.hostname, command=command, content=content})
- end
- function api.connect (remotehost, channel)
- return setmetatable({
- protocol = api.protocol,
- remotehost = recipient,
- }, {__index == api.meta.connect});
- --send_message(remotehost, channel, "API_CONNECT",
- end
- function api.msg_address (message, replyChannel)
- return message.from .. "@" .. api.protocol .. "#" .. replyChannel;
- end
- ----------------------------
- -- connect object methods --
- function api.meta.connect:message(command, content)
- send_message(self.remotehost, self.channel, command, content)
- end
- function api.meta.connect:print (msg)
- self:message("PRINT", msg);
- end
- function api.meta.connect:loadstring (script)
- self:message("LOADSTRING", script)
- end
- -----------------------
- -- message processor --
- function api.proccess_message (modemSide, senderChannel,
- replyChannel, message, senderDistance)
- if type(message) == "table" and message.protocol == api.protocol then
- if message.recipient == api.hostname then
- if message.from ~= nil then
- local com = api.commands[message.command]
- if com then com(modemSide, senderChannel, replyChannel, message, senderDistance) end
- else
- print("ERR_MALFORMED_MSG: Missing 'from' in message from unknown host on channel #" .. replyChannel .. ".");
- end
- else
- print("INFO_NOT_RECIPIENT: message recieved, different recipient specified, ignoring message");
- end
- else
- print("ERR_MALFORMED_MSG: Unrecognized message format");
- end
- end
- -----------------------------
- -- message commands action --
- function api.commands.LOADSTRING (modemSide, senderChannel, replyChannel, message, senderDistance)
- if type(message.content) == "string" then
- local success, result = pcall(loadstring, message.content);
- if success then
- setfenv(result, getfenv())
- local call_success, call_result = pcall(result)
- assert(call_success, "ERR_LOADSTRING_CALL: error occured when calling content from " .. api.msg_address(message, replyChannel) .. " :: " .. call_result);
- else
- print("ERR_LOADSTRING: failed to perform loadstring on content from " .. api.msg_address(message, replyChannel) .. " :: " .. result);
- end
- else
- print("ERR_MALFORMED_CMD: expected content to be of type string, " .. api.msg_address(message, replyChannel) .. " sent content of type " .. type(message.content));
- end
- end
- function api.commands.PRINT (modemSide, senderChannel, replyChannel, message, senderDistance)
- local suc, res = pcall(tostring, message.content)
- if not suc then
- print("ERR_PRINT: could not convert content to string, from " .. api.msg_address(message, replyChannel))
- else
- print(api.msg_address(message, replyChannel) .. ": " .. res)
- end
- end
- return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement