Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Coded By HPWebcamAble
- ==== Description ====
- This is my protocol API
- It allows you to assign functions to messages
- When a message is received, the function will be run (With any arguments provided)
- See the CC Forum Post in 'Documentation' for a quick start guide
- ==== Documentation ====
- Computer Craft Forum Post:
- http://www.computercraft.info/forums2/index.php?/topic/22510-protocol-manager/
- Below 'Update History' is a description of all the functions and how to use them
- ==== Installation and Use ====
- Pastebin Code:Not on pb yet
- This won't do anything on its own. You use this to help write your programs
- You have two options:
- 1. Copy and paste the functions into your program and call them like you would normally call a function
- 2. In your program, call os.loadAPI("filename"), then call functions with filename.functionName()
- ==== Update History ====
- The pastebin will always have the latest version
- |1.2| <--This program
- -Fixed small bug in nAssert function
- |1.1|
- -waitForMessage stops when a function is called
- |1.0|
- -Release
- ==== Functions and How to Use Them ====
- WARNING! This API doesn't interact with channels
- You need to open the channels you want to use yourself!
- new
- Creates a new protocol set
- name: A string to help distinguish the messages from other programs. protocols are stored by the name
- protocolVersion: When you change how the program deals with messages, increment this number to tell the clients/servers that it is a different version
- setDefault: Pass true to make this the defualt protocol, most functions use the defualt if you don't specify a protocol
- addMessage
- Adds a message and function to the protocol set
- msg: The message to look for
- func: The function to run if the message is received
- protocol: The name of the protocol to add to (Will use the default if not specified)
- waitForMessage
- WARNING! OPEN CHANNELS FIRST! This function doesn't open channels
- Waits for an event, if it is modem_message, it checks to see if it was a message from the protocol, and runs the function
- with the arguments (sender ID, distance, arguments from message). Stops when a message is received
- protocol: Which protocol's messages to look for (Will use the default if not specified)
- timeout: How long to wait, in seconds. Leave nil to wait forever
- useRaw: If true, uses os.pullEventRaw
- sendMessage
- Sends a message
- modem: Pass the value the is returned by 'peripheral.wrap(side)'
- channel: The channel to send the message on. Only computers with this channel open can receive it
- protocol: The name of the protocol that will be receiving the message (Will use the default if not specified)
- msg: The message to send
- ...: Any number of arguments, which will be sent as well
- ==== Examples ====
- An example function that you might add for a message
- function addUser(id,distance,name)
- users[name] = id
- end
- (Assume users is a table already)
- ==== Program ====
- ]]--
- --Variables--
- local protocolSets = {}
- local default
- --Functions--
- --Like assert, but takes a third arguemnt, used by the API
- local function nAssert(toTest,err,lvl)
- lvl = lvl or 1
- if not toTest then
- error(err,lvl+1)
- end
- end
- function new(name,protocolVersion,setDefault)
- nAssert(type(protocolVersion)=="number" and name,"expected string,number got "..type(name)..","..type(protocolVersion),2)
- nAssert(not protocolSets[name],"The protocol "..name.." already exists",2)
- protocolSets[name] = {
- functions = {},
- version = protocolVersion
- }
- if setDefault then default = name end
- end
- function addMessage(msg,func,protocol)
- protocol = protocol or default
- nAssert(protocolSets[protocol],"protocol doesn't exist",2)
- nAssert(type(msg)=="string" and type(func)=="function","expected string,function, got "..type(msg)..","..type(func),2)
- protocolSets[protocol][msg] = func
- end
- function sendMessage(modem,channel,protocol,msg,...)
- protocol = protocol or default
- nAssert(protocolSets[protocol],"protocol doesn't exist",2)
- channel = tonumber(channel)
- nAssert(channel and msg,"expected modem(table),number,string, got "..type(modem)..","..type(channel)..","..type(msg),2)
- modem.transmit(channel,os.getComputerID(),{protocol,protocolSets[protocol].version,msg,{...}})
- end
- function waitForMessage(protocol,timeout,useRaw)
- protocol = protocol or default
- timeout = tonumber(timeout)
- nAssert(protocolSets[protocol],"protocol doesn't exist",2)
- local pull = useRaw and os.pullEventRaw or os.pullEvent
- if timeout then local timer = os.startTimer(timeout) end
- while true do
- local eArgs = {pull()}
- if eArgs[1] == "modem_message" then
- if type(eArgs[5]) == "table" and eArgs[5][1] == protocol and eArgs[5][2] == protocolSets[protocol].version then
- if protocolSets[protocol][eArgs[5][3]] then
- protocolSets[protocol][eArgs[5][3]](eArgs[4],eArgs[6],unpack(eArgs[5][4]))
- return true
- end
- end
- elseif eArgs[1] == "timer" and eArgs[2] == timer then
- return false
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement