Advertisement
cyber_Ahn

Ficsit-Networks-Server

Apr 13th, 2022 (edited)
2,995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. ---------------------var---------------------------------------------
  2. port_register = 5256
  3. port_data = 8080
  4. port_req = 8081
  5. screen_id = "BE69C4D44606577BC3D57292903CFE77"
  6. disk = "840440164E4A30C928FA46BE79FD3B86"
  7. file = "backup.db"
  8. local server_name = {}
  9. local server_ip = {}
  10. local server_ping = {}
  11. local ping_c = 0
  12. local server_data_ip = {}
  13. local server_data_typ = {}
  14. local server_data_item = {}
  15. local server_data_amount = {}
  16. ---------------------get-hardware----------------------------------------
  17. local gpu = computer.getPCIDevices(findClass("GPUT1"))[1]
  18. if not gpu then
  19.  error("No GPU T1 found!")
  20. end
  21.  
  22. local screen = computer.getPCIDevices(findClass("FINComputerScreen"))[1]
  23.  
  24. if not screen then
  25.  local comp_b=""
  26.  local comp = component.findComponent(findClass("Screen"))
  27.  for i=1, #comp do
  28.   if comp[i] == screen_id then
  29.    comp_b = comp[i]
  30.   end
  31.  end screen = component.proxy(comp_b)
  32. end
  33. gpu:bindScreen(screen)
  34. gpu:setSize(120,34)
  35. w,h = gpu:getSize()
  36. gpu:setSize(w,h)
  37. gpu:setText(0,0,"Start Terminal......")
  38. gpu:flush()
  39. event.pull(10)
  40. gpu:fill(0,0,w,h," ")
  41. gpu:flush()
  42. ---------------------set_network----------------------------------------
  43. local NetworkCard = computer.getPCIDevices(findClass("NetworkCard"))[1]
  44. netcard = component.proxy(NetworkCard.id)
  45. netcard:open(port_register)
  46. netcard:open(port_data)
  47. ---------------------function--------------------------------------------
  48. function network_send(receiver, port_s, data)
  49.  netcard = component.proxy(NetworkCard.id)
  50.  netcard:open(port_s)
  51.  netcard:send(receiver, port_s, data)
  52. end
  53.  
  54. function search_drives()
  55.  fs = filesystem
  56.  fs.initFileSystem("/dev")
  57.  drive_list = {}
  58.  for _, drive in pairs(fs.childs("/dev")) do
  59.   if drive ~= "serial" then
  60.    table.insert(drive_list,drive)
  61.   end
  62.  end
  63.  return drive_list
  64. end
  65.  
  66. function write_file(disk_uuid, FileName, Contents)
  67.  fs = filesystem
  68.  fs.initFileSystem("/dev")
  69.  fs.mount("/dev/"..disk_uuid, "/")
  70.  local file = fs.open("/"..FileName.."", "a")
  71.  file:write(Contents,"#")
  72.  file:close()
  73. end
  74.  
  75. function clear_file(disk_uuid, FileName)
  76.  fs = filesystem
  77.  fs.initFileSystem("/dev")
  78.  fs.mount("/dev/"..disk_uuid, "/")
  79.  local file = fs.open("/"..FileName.."", "w")
  80.  file:write("")--,"\n")
  81.  file:close()
  82. end
  83.  
  84. function read_file(disk_uuid, FileName)
  85.  fs = filesystem
  86.  data = {}
  87.  fs.initFileSystem("/dev")
  88.  fs.mount("/dev/"..disk_uuid, "/")
  89.  local file = fs.open("/"..FileName.."", "r")
  90.  cach = ""
  91.  cach = file:read(2048)
  92.  if cach ~= nil then
  93.   data = split(cach, "#")
  94.  end
  95.  file:close()
  96.  return data
  97. end
  98.  
  99. function update_file(disk_uuid,FileName, Contens, Seperator)
  100.  data_update = {}
  101.  data_r = read_file(disk, file)
  102.  for i=1, #data_r do
  103.   if data_r[i] ~= "" then
  104.    cach_1 = split(data_r[i], Seperator)[2]
  105.    cach_2 = split(Contens, Seperator)[2]
  106.    if(cach_1 == cach_2) then
  107.     table.insert(data_update, Contens);
  108.    else
  109.     table.insert(data_update, data_r[i]);
  110.    end
  111.   end
  112.  end
  113.  clear_file(disk,file)
  114.  for i=1, #data_update do
  115.   write_file(disk,file, data_update[i])
  116.  end
  117. end
  118.  
  119. function load_backup()
  120.  print("Load data Please wait ...")
  121.  lo_data = read_file(disk, file)
  122.  for i=1, #lo_data do
  123.   cach = split(lo_data[i],"|")
  124.   ip = "00000000"
  125.   typ = cach[1]
  126.   item = cach[2]
  127.   amount = cach[3]
  128.   if item ~= nil then
  129.   table.insert(server_data_ip,ip)
  130.   table.insert(server_data_typ,typ)
  131.   table.insert(server_data_item,item)
  132.   table.insert(server_data_amount,amount)
  133.   end
  134.  end
  135.  print("Ready")
  136. end
  137.  
  138. function split(s, delimiter)
  139.  result = {};
  140.  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  141.   table.insert(result, match);
  142.  end
  143.  return result;
  144. end
  145.  
  146. function in_table(table, element)
  147.  for _, value in pairs(table) do
  148.   if value == element then
  149.    return true
  150.   end
  151.  end
  152.  return false
  153. end
  154.  
  155. function table_pos(table,stri)
  156.  local index={}
  157.  for k,v in pairs(table) do
  158.    index[v]=k
  159.  end
  160.  return index[stri]
  161. end
  162.  
  163. function screen_draw()
  164.  gpu:fill(0,0,w,h," ")
  165.  gpu:flush()
  166.  gpu:setText(0,0,"Registed Server: "..#server_ip)
  167.  gpu:setText(30,0,"Saved Data Lines: "..#server_data_ip)
  168.  gpu:setText(0,1,"Name")
  169.  gpu:setText(20,1,"|IP")
  170.  gpu:setText(0,2,"--------------------")
  171.  gpu:setText(20,2,"|----------------------------------")
  172.  line = 2
  173.  for i=1, #server_ip do
  174.   gpu:setText(0,line+i,server_name[i])
  175.   gpu:setText(20,line+i,"|"..server_ip[i])
  176.  end
  177.  line = 1
  178.  gpu:setText(55,0,"Data:")
  179.  for i=1, #server_data_ip do
  180.   gpu:setText(55,line+i,server_data_ip[i].."|"..server_data_typ[i].."|"..server_data_item[i].."|"..server_data_amount[i])
  181.  end
  182.  gpu:flush()
  183. end
  184.  
  185. function send_data()
  186.  for i=1, #server_ip do
  187.   if(server_name[i] ~= "Main_PC") then
  188.    print("Send data to "..server_ip[i])
  189.    for j=1, #server_data_typ do
  190.     network_send(server_ip[i], port_req, server_data_typ[j].."|"..server_data_item[j].."|"..server_data_amount[j])
  191.    end
  192.   end
  193.  end
  194. end
  195.  
  196. function ping()
  197.  server_name = {}
  198.  server_ip = {}
  199.  print("Ping")
  200. end
  201. ---------------------main_prog---------------------------------------------
  202. s_drives = search_drives()
  203. for i=1, #s_drives do
  204.  print("Drives: "..s_drives[i])
  205. end
  206. disk = s_drives[1]
  207. print(disk)
  208. --load_backup()
  209. computer.beep(5)
  210. clear_file(disk,file)
  211. while true do
  212.  event.listen(netcard)
  213.  typ, to, ip, port, data = event.pull()
  214.  if typ == "NetworkMessage" then
  215.   if port == port_register then
  216.    if in_table(server_ip,ip) == false then
  217.     table.insert(server_name,data)
  218.     table.insert(server_ip,ip)
  219.     print("Registed Server: "..#server_ip)
  220.     send_data()
  221.    end
  222.   end
  223.  
  224.   if port == port_data then
  225.    cach = split(data,"|")
  226.    t_pos = 0
  227.    if in_table(server_data_item,cach[2]) == false then
  228.     table.insert(server_data_ip,ip)
  229.     table.insert(server_data_typ,cach[1])
  230.     table.insert(server_data_item,cach[2])
  231.     table.insert(server_data_amount,cach[3])
  232.     write_file(disk,file, data)
  233.    else
  234.     t_pos = table_pos(server_data_item,cach[2])
  235.     if(server_data_amount[t_pos] ~= cach[3]) then
  236.      --update amount for a item
  237.      server_data_amount[t_pos]=cach[3]
  238.      update_file(disk,file, data, "|")
  239.     end
  240.    end
  241.   end
  242.   if ping_c == 150 then
  243.    ping()
  244.    ping_c = 0
  245.   end
  246.   ping_c = ping_c + 1
  247.  end
  248.  screen_draw()
  249. end  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement