shiskebab

server/client proof of concept

Jul 24th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. CLIENT SERVER PROOF OF CONCEPT
  2.  
  3. CLIENT::::::
  4.  
  5. local compid
  6. local alv3
  7.  
  8. local function Startup()
  9.   term.clear()
  10.   term.setCursorPos (1,1)
  11.   rednet.open("right")
  12.   print ("   this Computer's id: ", os.getComputerID())
  13.   print ("   Set receiving computer's id:")
  14.   print ("   +--------------------------+   ")
  15.   print ("                              |   ")
  16.   print ("   +--------------------------+   ")
  17.   print ()
  18.   print ("   message:")
  19.   print ("   +--------------------------+   ")
  20.   print ("                              |   ")
  21.   print ("   +--------------------------+   ")
  22.   term.setCursorPos(5,4)
  23.   repeat
  24.         compid = tonumber(read())
  25.   until compid
  26.   term.setCursorPos(5,4)
  27.   print ("id saved")
  28.   term.setCursorPos(5,9)
  29.   alv3 = read()
  30.   term.setCursorPos(5,9)
  31.   print ("message saved")
  32.   sleep(0.5)
  33.   Sender()
  34.   Receive()
  35. end
  36.  
  37. function Sender()
  38.   term.clear()
  39.   term.setCursorPos (1,1)
  40.   print ("sending message in: ")
  41.   print ("3")
  42.   sleep(1)
  43.   print ("2")
  44.   sleep(1)
  45.   print ("1")
  46.   sleep(1)
  47.   rednet.send(compid, alv3)
  48.   sleep(1)
  49.   print ("message sent")
  50.   sleep(1)
  51. end
  52.  
  53. function Receive()
  54.   term.clear()
  55.   term.setCursorPos (1,1)
  56.   print ("   this Computer's id: ", os.getComputerID())
  57.   print ("   waiting for message:")
  58.   print ("   +--------------------------+   ")
  59.   print ("                              |   ")
  60.   print ("   +--------------------------+   ")
  61.   print ()
  62.   print ("   Terminal:")
  63.   print ("   +--------------------------+   ")
  64.   print ("                              |   ")
  65.   print ("   +--------------------------+   ")
  66.  
  67.   while true do
  68.    rednet.open("right")
  69.    senderID, message = rednet.receive()
  70.      term.setCursorPos (5,4)
  71.      print (message)
  72.      sleep(0.5)
  73.      term.setCursorPos (5,9)
  74.      print (senderID)
  75.      local event = os.pullEvent("key")
  76.      rednet.close("right")
  77.      Startup()
  78.      break
  79.   end
  80.   end
  81.  
  82. Startup()
  83.  
  84. SERVER::::::
  85.  
  86. local compid
  87. local MFile = "messages.txt"
  88.  
  89. local function Startup() -- set computerLabel, other computers id, and Launch WriteFile()
  90.   term.clear()
  91.   term.setCursorPos (1,1)
  92.   print ("   this Computer's id: ", os.getComputerID())
  93.   print ("   Set this Computer's label:")
  94.   print ("   +--------------------------+   ")
  95.   print ("                              |   ")
  96.   print ("   +--------------------------+   ")
  97.   print ()
  98.   print ("   Set The Computer Terminal's ID")
  99.   print ("   +--------------------------+   ")
  100.   print ("                              |   ")
  101.   print ("   +--------------------------+   ")
  102.   term.setCursorPos(5,4)
  103.   local IgnLabel = read()
  104.   os.setComputerLabel(IgnLabel)
  105.   term.setCursorPos(5,4)
  106.   print ("Label successful")
  107.   term.setCursorPos(5,9)
  108.   repeat
  109.         compid = tonumber(read())
  110.   until compid
  111.   term.setCursorPos(5,9)
  112.   print ("ID saved")
  113.   sleep(2)
  114.   print ("storing message")
  115.   WriteFile()
  116.   sleep(1)
  117.   print ("sending stored messages")
  118.   sleep(2)
  119.   SendFile()
  120.   print ("message sent!")
  121.   sleep(5)
  122.   Startup()
  123. end
  124.  
  125. function WriteFile() -- store incoming transmission into MFile variable
  126.   rednet.open("right")
  127.   local id, message = rednet.receive()
  128.    rednet.close()
  129.    if fs.isDir("saves") == false then
  130.     fs.makeDir("saves")
  131.     local file = fs.open("saves/"..MFile, "a")
  132.        file.writeLine(message)
  133.        file.close()
  134.     else
  135.      local file = fs.open("saves/"..MFile, "a")
  136.         file.writeLine(message)
  137.         file.close()
  138.   end
  139. end
  140.  
  141. function SendFile() -- send contents of MFile to another computer
  142.   file = fs.open("saves/"..MFile, "r")
  143.    
  144.     local shipment = file.readAll()
  145.     file.close()
  146.     rednet.open("right")
  147.     rednet.send(compid, shipment)
  148.     rednet.close("right")
  149.   end
  150.  
  151. Startup()
Advertisement
Add Comment
Please, Sign In to add comment