Stary2001

server

Jan 5th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. local funcs = {}
  4.  
  5. local _print = print
  6.  
  7. local _term_write = term.write
  8. local _term_clear = term.clear
  9. local _term_clearLine = term.clearLine
  10. local _term_scroll = term.scroll
  11. local _term_setCursorPos = term.setCursorPos
  12.  
  13. local _io_write = io.write
  14. local _io_read = io.read
  15.  
  16. local _os_pullEvent = os.pullEvent
  17.  
  18. local buf = {}
  19.  
  20. local client
  21.  
  22. function myWrite(s)
  23. buf[#buf+1]=s
  24. end
  25.  
  26. function myRead()
  27. rednet.send(client,"\1read")
  28.  
  29. local _,msg = rednet.receive()
  30.  
  31. return msg
  32. end
  33.  
  34. function myClear()
  35. rednet.send(client,"\1clear")
  36. end
  37.  
  38. function myClearLine()
  39. rednet.send(client,"\1clearLine")
  40. end
  41.  
  42. function myScroll()
  43. rednet.send(client,"\1scroll")
  44. end
  45.  
  46. function mySetCursorPos(x,y)
  47. rednet.send(client,"\1setCursorPos"..x..","..y)
  48. end
  49.  
  50. local keybuf = {}
  51. local charbuf = {}
  52.  
  53. function myPullEvent(filt)
  54. if #keybuf ~= 0 then
  55. if not filt or filt=="key" then
  56. local k = keybuf[1]
  57. table.remove(keybuf,1)
  58. return "key",k
  59. end
  60. end
  61.  
  62. if #charbuf ~= 0 then
  63. if not filt or filt=="char" then
  64. local c = charbuf[1]
  65. table.remove(charbuf,1)
  66. return "char",tonumber(c)
  67. end
  68. end
  69.  
  70. return _os_pullEvent(filt)
  71. end
  72.  
  73. function hookIO()
  74. rawset(_G,"print",myWrite)
  75.  
  76. rawset(_G["term"],"write",myWrite)
  77. rawset(_G["term"],"clear",myClear)
  78. rawset(_G["term"],"clearLine",myClearLine)
  79. rawset(_G["term"],"scroll",myScroll)
  80. rawset(_G["term"],"setCursorPos",mySetCursorPos)
  81.  
  82. rawset(_G["io"],"write",myWrite)
  83. rawset(_G["io"],"read",myRead)
  84.  
  85. rawset(_G["os"],"pullEvent",myPullEvent)
  86. end
  87.  
  88. function unhookIO()
  89. rawset(_G,"print",_print)
  90.  
  91. rawset(_G["term"],"write",_term_write)
  92. rawset(_G["term"],"clear",_term_clear)
  93. rawset(_G["term"],"clearLine",_term_clearLine)
  94. rawset(_G["term"],"scroll",_term_scroll)
  95. rawset(_G["term"],"setCursorPos",_term_setCursorPos)
  96.  
  97. rawset(_G["io"],"write",_io_write)
  98. rawset(_G["io"],"read",_io_read)
  99.  
  100. rawset(_G["os"],"pullEvent",_os_pullEvent)
  101. end
  102.  
  103. function funcs.exe(id,args)
  104. local prog = args[1]
  105. table.remove(args,1)
  106.  
  107. local run =
  108. function()
  109. hookIO()
  110. shell.run(prog,unpack(args))
  111. --unhookIO()
  112. end
  113.  
  114. local reader =
  115. function()
  116. while #buf~=0 do
  117. rednet.send(id,buf[1])
  118. table.remove(buf,1)
  119. end
  120. end
  121.  
  122. client = id
  123. run()
  124. reader()
  125.  
  126. rednet.send(id,"\1done")
  127. end
  128.  
  129. function funcs.key(id,args)
  130. keybuf[#keybuf+1] = args[1]
  131. end
  132.  
  133. function funcs.char(id,args)
  134. charbuf[#charbuf+1] = args[1]
  135. end
  136.  
  137. function process(id,msg)
  138. local rest = string.gmatch(msg,"cmd:(.+)$")()
  139.  
  140. local func,args = string.gmatch(rest,"^(.-):(.+)$")()
  141. local t = {}
  142. for v in string.gmatch(args,"[^:]+") do
  143. t[#t+1]=v
  144. end
  145.  
  146. funcs[func](id,t)
  147. end
  148.  
  149. while true do
  150. local id,msg = rednet.receive()
  151. if msg:sub(1,4) == "cmd:" then
  152. process(id,msg)
  153. end
  154. end
  155.  
  156. rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment