-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.lua
More file actions
114 lines (97 loc) · 3.34 KB
/
router.lua
File metadata and controls
114 lines (97 loc) · 3.34 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
local routerID = "Router" .. os.getComputerID()
local routingTable = {} -- Maps destination computerID to next-hop computerID
local neighbors = {} -- Keeps track of neighbor IDs
local assignedAddresses = {}
local nextAddress = 1
function handleDHCPRequest(senderID)
local address = "192.168." .. os.getComputerID() .. "." .. nextAddress
assignedAddresses[address] = senderID
nextAddress = nextAddress + 1
rednet.send(senderID, address)
end
function handleSendMessage(senderID, message)
local destinationIP, content = message:match("^(%S+)%s+(.*)$")
local destinationID = assignedAddresses[destinationIP]
if destinationID then
rednet.send(destinationID, content)
else
local nextHop = routingTable[destinationIP]
if nextHop then
rednet.send(nextHop, message)
else
print("Unknown destination:", destinationIP)
end
end
end
function handleMessage(senderID, message)
if message == "DHCP request" then
handleDHCPRequest(senderID)
elseif message:find("^192.168.") then
handleSendMessage(senderID, message)
else
-- Handle other message types
end
end
function handleDiscovery(senderID, message)
print("Received discovery message:", message, "from:", senderID)
local senderRouter = message:match("^(%S+)%s*(.*)$")
if senderRouter and senderRouter ~= routerID then
-- Update the routing table
routingTable[senderRouter] = senderID
-- Forward the discovery message to neighbors
for _, neighbor in ipairs(neighbors) do
if neighbor ~= senderID then
print("Forwarding discovery message to:", neighbor)
rednet.send(neighbor, message, "networkName")
end
end
end
end
local function broadcastDiscovery()
local discoveryMessage = routerID
rednet.broadcast(discoveryMessage, "networkName")
end
local function handleMessage(senderID, message)
if message:find("^Router") then
handleDiscovery(senderID, message)
end
end
local function listenForMessages()
while true do
local senderID, message, protocol = rednet.receive()
if protocol == "networkName" then
handleMessage(senderID, message)
end
end
end
local function printRoutingTable()
while true do
os.sleep(5)
print("Routing Table:")
for destination, nextHop in pairs(routingTable) do
print(destination, "->", nextHop)
end
end
end
-- Open the rednet interface on all available sides
for _, side in pairs(peripheral.getNames()) do
if peripheral.getType(side) == "modem" then
rednet.open(side)
local neighborID = rednet.lookup("networkName", side)
if neighborID then
table.insert(neighbors, neighborID)
end
end
end
print("Router ID:", routerID)
print("Neighbors:", table.concat(neighbors, ", "))
-- Initialize the network by broadcasting the router's presence
broadcastDiscovery()
local function periodicBroadcast()
while true do
os.sleep(10) -- Wait for 10 seconds
broadcastDiscovery()
end
end
-- Add this function to your parallel tasks
parallel.waitForAll(listenForMessages, printRoutingTable, periodicBroadcast)