Advertisement
m3Zz

Server

Mar 1st, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. --ping command:
  2. pcmd="Ping"
  3. --modem side:
  4. side="right"
  5. --ping timeout:
  6. timeout=0.2
  7. --ping-range:
  8. minid=10
  9. maxid=20
  10.  
  11. function clear()
  12.  term.clear()
  13.  term.setCursorPos(1,1)
  14. end
  15.  
  16. function startup()
  17.  i1=0
  18.  clear()
  19.  print("Starting server...")
  20.  rednet.open(side) --enable the modem attached to the right side of the PC
  21.  sleep(0.1)
  22.  print("0%")
  23.  sleep(0.1)
  24.  print("20%")
  25.  sleep(0.1)
  26.  print("37%")
  27.  sleep(0.1)
  28.  print("80%")
  29.  sleep(0.1)
  30.  print("95%")
  31.  sleep(0.1)
  32.  print("100%")
  33.  sleep(0.2)
  34.  clear()
  35.  print("Server has started!")
  36.  load()
  37.  clear()
  38.  ownid=os.getComputerID()
  39. end
  40.  
  41. function ping()
  42. data={}
  43. for i=minid, maxid do
  44.  rednet.send(i,pcmd)
  45.  tmp, msg = rednet.receive(timeout)
  46.  if msg==nil then
  47.   msg=0
  48.   print("Nothing on "..i.."!")
  49.  else
  50.   temp=tmp.." "..msg
  51.   data[i1]=temp
  52.   i1=i1+1
  53.   print("Found something on "..i.."!")
  54.  end
  55.  end
  56. i1=0
  57. sleep(0.5)
  58. clear()
  59. end
  60.  
  61. function output()
  62.  for i=0, table.getn(data) do
  63.  if data[i]==ownid.." "..pcmd then
  64.  
  65.  else
  66.   print(data[i])
  67.  end
  68. end
  69. end
  70.  
  71. function refresh()
  72.  clear()
  73.  print("Saving data...")
  74.  save()
  75.  sleep(0.5)
  76.  clear()
  77.  print("Reconfiguring...")
  78.  print("Please be patient!")
  79.  sleep(0.5)
  80.  ping()
  81.  sleep(0.5)
  82.  output()
  83. end
  84.  
  85. function save()
  86.  doc=fs.open("data", "w") --doc = fs.open(filename, mode(w))
  87.  doc.writeLine(pcmd)
  88.  doc.writeLine(side)
  89.  doc.writeLine(timeout)
  90.  doc.writeLine(minid)
  91.  doc.writeLine(maxid)
  92.  doc.close()
  93. end
  94.  
  95. function load()
  96.  doc=fs.open("data", "r")
  97.  if doc==nil then
  98.  
  99.  else
  100.   pcmd=doc.readLine()
  101.   side=doc.readLine()
  102.   timeout=tonumber(doc.readLine())
  103.   minid=tonumber(doc.readLine())
  104.   maxid=tonumber(doc.readLine())
  105.   doc.close()
  106.  end
  107. end
  108.  
  109. function configure()
  110. clear()
  111. x,y=term.getSize()
  112.  for i=0,x-1 do
  113.   write("-")
  114.  end
  115. print("Enter the new ping command!")
  116. print("Default: 'Ping'")
  117. print("Current: ".."'"..pcmd.."'")
  118. pcmd=read()
  119. print("Set to: "..pcmd)
  120. sleep(0.5)
  121. clear()
  122.  for i=0,x-1 do
  123.   write("-")
  124.  end
  125. print("Enter the new modem side!")
  126. print("Default: 'right'")
  127. print("Current: ".."'"..side.."'")
  128. side=read()
  129. print("Set to: "..side)
  130. sleep(0.5)
  131. clear()
  132.  for i=0,x-1 do
  133.   write("-")
  134.  end
  135. print("Enter the new timeout for the ping!")
  136. print("Default: '0.2'")
  137. print("Current: ".."'"..timeout.."'")
  138. timeout=tonumber(read())
  139. print("Set to: "..timeout)
  140. sleep(0.5)
  141. clear()
  142.  for i=0,x-1 do
  143.   write("-")
  144.  end
  145. print("Enter the new min. ping range!")
  146. print("Default: '10'")
  147. print("Current: ".."'"..minid.."'")
  148. minid=tonumber(read())
  149. print("Set to: "..minid)
  150. sleep(0.5)
  151. clear()
  152.  for i=0,x-1 do
  153.   write("-")
  154.  end
  155. print("Enter the new max. ping range!")
  156. print("Default: '20'")
  157. print("Current: ".."'"..maxid.."'")
  158. maxid=tonumber(read())
  159. print("Set to: "..maxid)
  160. sleep(0.5)
  161. refresh()
  162. end
  163.  
  164. function main()
  165. load()
  166. output()
  167. print("Please enter an ID:                     Own ID:"..ownid)
  168. id=read()
  169. id=tonumber(id)
  170. clear()
  171. output()
  172. print("Please enter a command:                 Own ID:"..ownid)
  173. cmd=read()
  174. clear()
  175. if cmd=="stop" and id==ownid then
  176.  clear()
  177.  print("Logging out...")
  178.  rednet.close(side) --disable modem on the right side of the PC
  179.  error()
  180. else
  181. if cmd=="settings" and id==ownid then
  182.  configure()
  183. else
  184. if cmd=="refresh" and id==ownid then
  185.  refresh()
  186. else
  187.  if cmd=="stop" then
  188.    rednet.send(id,cmd) --Send a message
  189.    print("Command sent!")
  190.    id,message = rednet.receive(timeout) --Wait until a message arrives or 0.2 seconds pass
  191.    print("")
  192.    print("Reply message:")
  193.    print(message)
  194.    sleep(2)
  195.    clear()
  196.    ping()
  197.  else
  198.   rednet.send(id,cmd) --Send a message
  199.   print("Command sent!")
  200.   id,message = rednet.receive(timeout) --Wait until a message arrives or 0.2 seconds pass
  201.   print("")
  202.   print("Reply message:")
  203.   print(message)
  204.   sleep(2)
  205.  end
  206.  end
  207. end
  208. end
  209. clear()
  210. main()
  211. end
  212.  
  213. startup()
  214. print("Getting connected ID's...")
  215. ping()
  216. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement