Advertisement
Timendainum

nperid

Nov 24th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. -- opt/nperi/nperid
  2. -- Forked by Timendainum
  3. -- adapted from script by Lyqyd
  4. ---------------------------------------------------
  5.  
  6. ---------------------------------------------------
  7. -- declarations
  8. connections = {}
  9. local port = 88
  10.  
  11. ---------------------------------------------------
  12. -- functions
  13.  
  14. function nPeriDaemon ()
  15.     local function safeSend(theConnection, mType, ...)
  16.         if connections[theConnection] then
  17.             return nets.send(theConnection, mType, ...)
  18.         else
  19.             print("No connection to send!")
  20.             return false
  21.         end
  22.     end
  23.  
  24.     while true do
  25.         local conn, messType, tMessage = nets.listenIdle(port)
  26.         --txt.sPrint("Recieved wakeup...", conn, messType, unpack(tMessage))
  27.         if connections[conn] and connections[conn].status == "open" then
  28.             if messType == "close" then
  29.                 --print("Received close.")
  30.                 connection.close(conn, disconnect, true)
  31.                 connections[conn].status = "closed"
  32.             elseif messType == "instruction" then
  33.                 --txt.sPrint("Received instruction: ", unpack(tMessage))
  34.                 -- parse message
  35.                 if tMessage[1] then
  36.                     if tMessage[1] == "isPresent" then
  37.                         --print("Processing isPresent request")
  38.                         if tMessage[2] then
  39.                             safeSend(conn, "data", peripheral.isPresent(tMessage[2]))
  40.                         else
  41.                             safeSend(conn, "response", "Invalid arguments.")
  42.                         end
  43.                     elseif tMessage[1] == "getType" then
  44.                         --print("Processing getType request")
  45.                         if tMessage[2] then
  46.                             safeSend(conn, "data", peripheral.getType(tMessage[2]))
  47.                         else
  48.                             safeSend(conn, "response", "Invalid arguments.")
  49.                         end
  50.                     elseif tMessage[1] == "getMethods" then
  51.                         --print("processing getMethods request")
  52.                         if tMessage[2] then
  53.                                 safeSend(conn, "data", peripheral.getMethods(tMessage[2]))
  54.                         else
  55.                             safeSend(conn, "response", "Invalid arguments.")
  56.                         end
  57.                     elseif tMessage[1] == "call" then
  58.                         --print("processing call request")
  59.                         if tMessage[2] and tMessage[3] then
  60.                             local tArgs = { }
  61.                             for k,v in ipairs(tMessage) do
  62.                                 if k > 3 then
  63.                                     table.insert(tArgs, v)
  64.                                 end
  65.                             end
  66.                                 safeSend(conn, "data", peripheral.call(tMessage[2], tMessage[3], unpack(tArgs)))
  67.                         else
  68.                             safeSend(conn, "response", "Invalid arguments.")
  69.                         end
  70.                     elseif tMessage[1] == "getNames" then
  71.                         --print("processing getNames request")
  72.                         safeSend(conn, "data", peripheral.getNames())
  73.                     elseif tMessage[1] == "stop" then
  74.                         print("received stop")
  75.                         return true
  76.                     else
  77.                         print("Invalid command")
  78.                         safeSend(conn, "response", "Invalid command.")
  79.                     end
  80.                 else
  81.                     txt.sPrint("Invalid instruction: ", unpack(tMessage))
  82.                     safeSend(conn, "response", "Invalid instruction.")
  83.                 end
  84.             end
  85.             safeSend(conn, "done", "ready")
  86.         elseif messType == "query" then
  87.                 --print("received query")
  88.                 local connect = {}
  89.                 connect.status = "open"
  90.                 connect.name = connection.name(conn)
  91.                 table.insert(connections, conn, connect)
  92.                 safeSend(conn, "response", "ready")
  93.         end
  94.         term.restore()
  95.     end
  96. end
  97.  
  98. net.daemonAdd("nperid", nPeriDaemon , port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement