Advertisement
FluttyProger

inetSever

Sep 19th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local internet = require"internet"
  2. local serialization = require"serialization"
  3. local thread = require"thread"
  4. local on = require"opennet"
  5. local unicode = require"unicode"
  6. local term = require"term"
  7. local component     = require"component"
  8.  
  9. -- Check for all required libs
  10. assert(internet, "Interned card expected")
  11. assert(thread,   "Thread library expected")
  12. assert(on,       "Opennet library expected")
  13.  
  14. local unpack = table.unpack
  15. local gpu    = component.gpu
  16.  
  17. -- Init
  18. local ip = on.getIP()
  19. thread.init()
  20.  
  21. -- Additional function actions
  22. local proc = {
  23.   request = function(...)
  24.     local s = {}
  25.     for chunk in internet.request(...) do
  26.       table.insert(s,chunk)
  27.     end
  28.     io.write("\n|  Chunks received: "..#s, " \"".. ((s[1] and s[1]:sub(1,10)) or "") .."\"...")
  29.     return s
  30.   end
  31. }
  32.  
  33. local function processReceived(r)
  34.  
  35.  
  36.   -- Check if this message from internet
  37.   if r and r[2] == "internet" and type(r[3])=="string" then
  38.     local obj = serialization.unserialize(r[3])
  39.     local fncName = obj.__functionName
  40.     obj.__functionName = nil
  41.  
  42.     io.write("\n| Calling function: " .. fncName .. "() ...")
  43.  
  44.     local returnedObj
  45.     if proc[fncName] then
  46.       returnedObj = {proc[fncName](unpack(obj))}
  47.     else
  48.       local fnc = internet[fncName]
  49.       if type(fnc)=="function" then
  50.         returnedObj = {fnc(unpack(obj))}
  51.         io.write("\n|         Returned: ", unpack(returnedObj))
  52.       end
  53.     end
  54.  
  55.     io.write("\n|     Sending back...\n")
  56.     on.send(r[1], serialization.serialize(returnedObj))
  57.   end
  58.  
  59. end
  60.  
  61. -- Interface
  62. gpu.setResolution(gpu.maxResolution())
  63. local W,H = gpu.getResolution()
  64. local loaderSymbols = "◓◑◒◐"
  65. local loadStep = unicode.len(loaderSymbols)
  66.  
  67. -- Shows small loading circle on screen
  68. local function workIndicator(isWorking)
  69.   local col = isWorking and 0xFF0000 or 0x00FF00
  70.   local oldCol = gpu.getForeground()
  71.   gpu.setForeground(col)
  72.   gpu.fill(W, 1, 1, 1, unicode.sub(loaderSymbols, loadStep, loadStep))
  73.   gpu.setForeground(oldCol)
  74.   loadStep = (loadStep % unicode.len(loaderSymbols)) + 1
  75. end
  76.  
  77. -- Receiving loop
  78. while true do
  79.   local r = {on.receive(0.5)}
  80.  
  81.   -- Write log
  82.   if #r > 0 then
  83.     workIndicator(true)
  84.     io.write("\n|         Received: ")
  85.     for _,v in pairs(r) do io.write(tostring(v):sub(1,20),(#tostring(v)>20) and "..." or ""," ") end
  86.   end
  87.  
  88.   -- Process it
  89.   local ok, err = pcall(processReceived,r)
  90.   if not ok then
  91.     io.stderr:write("\nError during processing:"..tostring(err).."\n")
  92.   end
  93.  
  94.   -- Show that we are working
  95.   workIndicator()
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement