-
Notifications
You must be signed in to change notification settings - Fork 1
/
rangeextender.lua
68 lines (61 loc) · 2.42 KB
/
rangeextender.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--This program is aimed to provide an in-box way to put a computer anywhere in the world and have it work with the server (as long as its chunkloaded)
--What this means is, instead of putting a modem in the door computer, you put the linked card in the doorcomputer and the other in a computer running this.
--This computer must have a modem (wired recommended plugged into a relay plugged into the server) and at least 1 linking card with no maximum. NOTICE: Wired modem Cannot use linking feature unless the device being linked has a linking card in it with a range extender on the same wired network.
local component = require("component")
local gpu = component.gpu
local event = require("event")
local ser = require("serialization")
local term = require("term")
local thread = require("thread")
local process = require("process")
local uuid = require("uuid")
local computer = component.computer
local modem = component.modem
local links = {}
local modemPort = 1000
local syncPort = 199
for key,_ in pairs(component.list("tunnel")) do
table.insert(links,{["dev"]=component.proxy(key),["uuid"]="none"})
end
modem.open(syncPort)
modem.broadcast(syncPort,"syncport")
local e,_,_,_,_,msg = event.pull(1,"modem_message")
modem.close(syncPort)
if e then
modemPort = tonumber(msg)
else
print("What port is the server running off of?")
local text = term.read()
modemPort = tonumber(text:sub(1,-2))
term.clear()
end
modem.open(modemPort)
term.clear()
print("Range Extender started with " .. #links .. " devices")
while true do
local e, dees, from, port, _, msg, msg2, msg3 = event.pull("modem_message")
if port == 0 then
for i=1,#links,1 do
if links[i].dev.address == dees then
links[i].uuid = msg
end
end
modem.broadcast(modemPort,"rebroadcast",ser.serialize({["uuid"]=msg,["command"]=msg2,["data"]=msg3}))
print("Got message from device: " .. msg)
else
if msg == "rebroadcast" then
msg2 = ser.unserialize(msg2)
for i=1,#links,1 do
if msg2.uuid == links[i].uuid then
links[i].dev.send(msg2.data,msg2.data2)
print("Sending message directly to " .. msg2.uuid)
end
end
else
for i=1,#links,1 do
links[i].dev.send(msg,msg2,msg3)
end
print("Sending message to all devices")
end
end
end