Advertisement
HPWebcamAble

[CC API][1.1] Protocol Manager

Mar 23rd, 2015
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. --[[
  2. Coded By HPWebcamAble
  3.  
  4. ==== Description ====
  5. This is my protocol API
  6.  
  7. It allows you to assign functions to messages
  8. When a message is received, the function  will be run (With any arguments provided)
  9. See the CC Forum Post in 'Documentation' for a quick start guide
  10.  
  11.  
  12. ==== Documentation ====
  13. Computer Craft Forum Post:
  14. http://www.computercraft.info/forums2/index.php?/topic/22510-protocol-manager/
  15.  
  16. Below 'Update History' is a description of all the functions and how to use them
  17.  
  18.  
  19. ==== Installation and Use ====
  20. Pastebin Code:Not on pb yet
  21.  
  22. This won't do anything on its own. You use this to help write your programs
  23.  
  24. You have two options:
  25. 1. Copy and paste the functions into your program and call them like you would normally call a function
  26. 2. In your program, call os.loadAPI("filename"), then call functions with filename.functionName()
  27.  
  28.  
  29. ==== Update History ====
  30. The pastebin will always have the latest version
  31.  
  32. |1.2|  <--This program
  33.   -Fixed small bug in nAssert function
  34.  
  35. |1.1|
  36.   -waitForMessage stops when a function is called
  37.  
  38. |1.0|
  39.   -Release
  40.  
  41.  
  42. ==== Functions and How to Use Them ====
  43.  
  44. WARNING! This API doesn't interact with channels
  45. You need to open the channels you want to use yourself!
  46.  
  47. new
  48.   Creates a new protocol set
  49.   name: A string to help distinguish the messages from other programs. protocols are stored by the name
  50.   protocolVersion: When you change how the program deals with messages, increment this number to tell the clients/servers that it is a different version
  51.   setDefault: Pass true to make this the defualt protocol, most functions use the defualt if you don't specify a protocol
  52.  
  53. addMessage
  54.   Adds a message and function to the protocol set
  55.   msg: The message to look for
  56.   func: The function to run if the message is received
  57.   protocol: The name of the protocol to add to (Will use the default if not specified)
  58.  
  59. waitForMessage
  60.   WARNING! OPEN CHANNELS FIRST! This function doesn't open channels
  61.   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
  62.   with the arguments (sender ID, distance, arguments from message). Stops when a message is received
  63.   protocol: Which protocol's messages to look for (Will use the default if not specified)
  64.   timeout: How long to wait, in seconds. Leave nil to wait forever
  65.   useRaw: If true, uses os.pullEventRaw
  66.  
  67. sendMessage
  68.   Sends a message
  69.   modem: Pass the value the is returned by 'peripheral.wrap(side)'
  70.   channel: The channel to send the message on. Only computers with this channel open can receive it
  71.   protocol: The name of the protocol that will be receiving the message (Will use the default if not specified)
  72.   msg: The message to send
  73.   ...: Any number of arguments, which will be sent as well
  74.  
  75. ==== Examples ====
  76.  
  77. An example function that you might add for a message
  78.  
  79. function addUser(id,distance,name)
  80.   users[name] = id
  81. end
  82. (Assume users is a table already)
  83.  
  84. ==== Program ====
  85. ]]--
  86.  
  87. --Variables--
  88. local protocolSets = {}
  89. local default
  90.  
  91. --Functions--
  92. --Like assert, but takes a third arguemnt, used by the API
  93. local function nAssert(toTest,err,lvl)
  94.   lvl = lvl or 1
  95.   if not toTest then
  96.     error(err,lvl+1)
  97.   end
  98. end
  99.  
  100. function new(name,protocolVersion,setDefault)
  101.   nAssert(type(protocolVersion)=="number" and name,"expected string,number got "..type(name)..","..type(protocolVersion),2)
  102.   nAssert(not protocolSets[name],"The protocol "..name.." already exists",2)
  103.   protocolSets[name] = {
  104.     functions = {},
  105.     version = protocolVersion
  106.   }
  107.   if setDefault then default = name end
  108. end
  109.  
  110. function addMessage(msg,func,protocol)
  111.   protocol = protocol or default
  112.   nAssert(protocolSets[protocol],"protocol doesn't exist",2)
  113.   nAssert(type(msg)=="string" and type(func)=="function","expected string,function, got "..type(msg)..","..type(func),2)
  114.   protocolSets[protocol][msg] = func
  115. end
  116.  
  117. function sendMessage(modem,channel,protocol,msg,...)
  118.   protocol = protocol or default
  119.   nAssert(protocolSets[protocol],"protocol doesn't exist",2)
  120.   channel = tonumber(channel)
  121.   nAssert(channel and msg,"expected modem(table),number,string, got "..type(modem)..","..type(channel)..","..type(msg),2)
  122.   modem.transmit(channel,os.getComputerID(),{protocol,protocolSets[protocol].version,msg,{...}})
  123. end
  124.  
  125. function waitForMessage(protocol,timeout,useRaw)
  126.   protocol = protocol or default
  127.   timeout = tonumber(timeout)
  128.   nAssert(protocolSets[protocol],"protocol doesn't exist",2)
  129.   local pull = useRaw and os.pullEventRaw or os.pullEvent
  130.   if timeout then local timer = os.startTimer(timeout) end
  131.   while true do
  132.     local eArgs = {pull()}
  133.     if eArgs[1] == "modem_message" then
  134.       if type(eArgs[5]) == "table" and eArgs[5][1] == protocol and eArgs[5][2] == protocolSets[protocol].version then
  135.         if protocolSets[protocol][eArgs[5][3]] then
  136.           protocolSets[protocol][eArgs[5][3]](eArgs[4],eArgs[6],unpack(eArgs[5][4]))
  137.           return true
  138.         end
  139.       end
  140.     elseif eArgs[1] == "timer" and eArgs[2] == timer then
  141.       return false
  142.     end
  143.   end
  144.   return false
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement