Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component = require("component") local computer = require("computer")
- -- The driver
- driver = component.proxy(component.list("modem")())
- me = driver.address
- update = nil
- running = true
- netport = 666
- -- Code
- tb = string.char -- To byte
- fb = string.byte -- From byte
- requests = {}
- nodes = {}
- nearby = {}
- buffer = {}
- ttl = 32 -- 32 hops
- function _send(s)
- driver.broadcast(netport,s)
- end
- function _lsend(s)
- -- send through cable!
- end
- function find(tab,who)
- for i,v in ipairs(tab) do
- if (v==who) then return i end
- end
- return 0
- end
- function handle()
- local e,interface,origin,oport,dist,data = computer.pullSignal(update)
- if (e=="modem_message") then
- local c = data:sub(1,1)
- if (dist==0) then
- -- CABLE LOCAL
- local destl = fb(data:sub(2,2)) or 0
- print("DEST ADD LEN: "..destl)
- local desta = data:sub(3,3+destl)
- print("Dest addr: "..desta)
- if c=="C" then
- -- Connect to
- _send("R"..tb(ttl)..tb(destl)..desta..tb(me)..me)
- requests[#requests+1] = desta
- _lsend("CONNECTING")
- elseif c=="V" then
- -- Verify
- a = find(nodes,desta)
- if (a>0) then
- -- We are conencted!
- _lsend("CONNECTED")
- else
- _lsend("NOT CONNECTED")
- end
- elseif c=="D" then
- -- Send data
- a = find(nodes,desta)
- if (a>0) then
- _send("D"..tb(ttl)..tb(destl)..desta..tb(me)..me) -- MAYBE TTL THAT GOT RECEIVED FROM REPLY!????
- end
- elseif c=="R" then
- -- Receive data (uC sends data buffer to client)
- for i,v in pairs(buffer) do
- _lsend(tb(i)..i..v)
- end
- end
- -- NEARBY NODES
- if c=="H" then
- -- New node nearby
- elseif c=="I" then
- -- We got a nearby host! Save him
- elseif c=="Q" then
- -- A host left us
- -- FAR NETWORK
- elseif c=="E" then
- -- Routed data
- elseif c=="R" then
- -- Route request
- elseif c=="X" then
- -- Route reply!
- end
- end
- if (e=="key_down") then
- running = false
- end
- end
- while running do
- ok,err = pcall(handle)
- if (not ok) then
- computer.beep(2000,0.5)
- end
- print(err)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement