superanonymous

endernet

Jun 19th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. --To use in your program, the following lines must be added.
  2. --os.loadAPI("endernet")
  3. --os.pullEvent = endernet.middle
  4. --os.pullEventRaw = endernet.middleRaw
  5. --
  6. --This API exposes the following methods (NOTE: all topics MUST begin with "/", otherwise they will be dropped with 404 error code)
  7. --send(string topic, string message) --Sends a message to the specified topic --Returns maximum number of possibly connected clients (Not all that useful, but can be informative)
  8. --subscribe(string topic) --Subscribes to a topic in order to receive messages from it --Returns nil
  9. --assert(string topic) --Asserts a topic, renewing a request in case its connection is dropped
  10. --unsubscribe(string topic) --Unsubscribes from a topic --Returns nil
  11. --receive() --Receives message from any of the subscribed topics --Returns topic, message
  12. --middle(event) --Meant to replace os.pullEvent, and handles http events in order to keep them alive
  13. --middleRaw(event) --Same as above, for os.pullEventRaw
  14.  
  15. --Define global api modifications to help later
  16. function table.contains(table, element)
  17.     for _, value in pairs(table) do
  18.         if value == element then
  19.             return true
  20.         end
  21.     end
  22.     return false
  23. end
  24.  
  25. --Define local variables
  26. local subdTopics = {}
  27. local baseUrl = "https://ccnet.herokuapp.com"
  28. local sendUrl = "/send"
  29. local query = "/query"
  30. local dataToSend = "?message="
  31.  
  32. --Define functions
  33. function send(topic,message) --Sends a message to a topic
  34.     local hhandle = http.get(baseUrl..sendUrl..topic..dataToSend..textutils.urlEncode(message))
  35.     return tonumber(hhandle.readAll())
  36. end
  37.  
  38. function subscribe(topic) --Subscribes to a topic
  39.     if not table.contains(subdTopics,topic) then
  40.         table.insert(subdTopics,topic)
  41.         http.request(baseUrl..query..topic)
  42.     end
  43. end
  44.  
  45. function assert(topic) --Asserts a topic, renewing it's connection in case it dropped
  46.     http.request(baseUrl..query..topic)
  47. end
  48.  
  49. function unsubscribe(topic) --Unsubscribes from a topic
  50.     for k,v in pairs(subdTopics) do
  51.         if v==topic then
  52.             table.remove(subdTopics,k)
  53.         end
  54.     end
  55. end
  56.  
  57. function receive() --Returns topic, message
  58.     local event = {middle("endernet_receive")}
  59.     return event[2],event[3]
  60. end
  61.  
  62. function middle(eventName) --os.pullEvent = endernet.middle
  63.     --Define variables that I'd prefer not to leak out but are evaluated later
  64.     local event
  65.     local topic
  66.     local message
  67.  
  68.     --Yield should work, because if it's overwritten, the computer won't work
  69.     event = {coroutine.yield()}
  70.     if event[1]=="terminate" then
  71.         error("Terminated")
  72.     end
  73.  
  74.     --Parse the event
  75.     if event[1]=="http_success" then
  76.         topic = string.gsub(event[2],baseUrl..query,"")
  77.         if table.contains(subdTopics,topic) then
  78.             http.request(event[2])
  79.         end
  80.         if eventName==nil or eventName=="endernet_receive" then
  81.             if string.find(event[2],baseUrl..query)~=nil then
  82.                 message = event[3].readAll()
  83.                 return "endernet_receive",topic,message
  84.             else
  85.                 return table.unpack(event)
  86.             end
  87.         else
  88.             return middle(eventName)
  89.         end
  90.     elseif event[1]=="http_failure" then
  91.         if table.contains(subdTopics,string.gsub(event[2],baseUrl..query,"")) then
  92.             http.request(event[2])
  93.         end
  94.         return middle(eventName)
  95.     else
  96.         if eventName==nil or event[1]==eventName then
  97.             return table.unpack(event)
  98.         else
  99.             return middle(eventName)
  100.         end
  101.     end
  102.  
  103. end
  104.  
  105. function middleRaw(eventName) --os.pullEvent = endernet.middle
  106.     --Define variables that I'd prefer not to leak out but are evaluated later
  107.     local event
  108.     local topic
  109.     local message
  110.  
  111.     --Yield should work, because if it's overwritten, the computer won't work
  112.     event = {coroutine.yield()}
  113.  
  114.     --Parse the event
  115.     if event[1]=="http_success" then
  116.         topic = string.gsub(event[2],baseUrl..query,"")
  117.         if table.contains(subdTopics,topic) then
  118.             http.request(event[2])
  119.         end
  120.         if eventName==nil or eventName=="endernet_receive" then
  121.             if string.find(event[2],baseUrl..query)~=nil then
  122.                 message = event[3].readAll()
  123.                 return "endernet_receive",topic,message
  124.             else
  125.                 return table.unpack(event)
  126.             end
  127.         else
  128.             return middle(eventName)
  129.         end
  130.     elseif event[1]=="http_failure" then
  131.         if table.contains(subdTopics,string.gsub(event[2],baseUrl..query,"")) then
  132.             http.request(event[2])
  133.         end
  134.         return middle(eventName)
  135.     else
  136.         if eventName==nil or event[1]==eventName then
  137.             return table.unpack(event)
  138.         else
  139.             return middle(eventName)
  140.         end
  141.     end
  142.  
  143. end
Advertisement
Add Comment
Please, Sign In to add comment