Posted 06 March 2012 - 06:31 PM
Hello (and sorry for my english),
i made programms and apis for Internet in Mincraft
routerapi
mcn
i made programms and apis for Internet in Mincraft
Spoiler
router programSpoiler
route = {}
shell.run("route")
print("populate route")
for k,v in pairs(route) do
print("pour "..k.." envoyer a ".. v)
end
rednet.open("top")
mcn.announce()
while true do
to,from,msg,router,packet = mcn.receive()
term.write("message entrant de " .. from)
if router ~= from then
print(" via " .. router)
else
print('')
end
print("message pour " .. to)
if route[to] ~= nil then
routing = route[to]
end
if to ~= os.getComputerID() then
if routing ~= nil then
rednet.send(routing,packet)
if to ~= routing then
print("routage sur " .. routing)
else
print("envoi direct")
end
else
print("EREUR : Pas de route pour " .. to)
end
else
print(msg)
route = routerapi.process(from,to,msg,router,route)
end
end
routerapi
Spoiler
function process(from,to,msg,router,route)
print("processing message ...")
if msg == "packet01" then
print("processing pong")
mcn.send(router,"packet02")
print("Envoie Pong")
elseif msg == "packet00" then
print('processing annonce')
if route[from] == nil and from ~= os.getComputerID() then
print('new route')
route[from] = router
routefile = io.open('route','r')
if not routefile then
routefile = io.open('route','w')
routefile:close()
routefile = io.open('route','r')
end
routestr = routefile.read('','*a')
if routestr == nil then routestr = '' end
routestr = "route[".. from .."] = ".. router .."\n"..routestr
routefile:close()
routefile = io.open('route','w')
routefile:write(routestr)
routefile:close()
print("route enregistrer")
rednet.broadcast("all"..mcn.Gdiv..from..mcn.Gdiv.."packet00")
print("envoie annonce relaier")
end
end
return route
end
Spoiler
-- Packets
-- 00 = announce
-- 01 = search router
-- 02 = response of packet01
Gdiv = "<|>"
local sender = os.getComputerID()
function explode(div,str) -- credit: http://richard.warburton.it
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
function send(ID,message)
rednet.broadcast("all"..Gdiv .. sender ..Gdiv.."packet01")
local i = 0
while i ~= 10 do
local to,sender2,msg,nearRouter,packet = restrictReveive(2,true)
if to == sender and msg == "packet02" then
rednet.send(nearRouter,ID..Gdiv ..sender..Gdiv ..message)
return true
end
end
return false
end
function explodePacket(Packet)
local packet = explode(Gdiv,Packet)
return packet[1],packet[2],packet[3] --to,sender,msg
end
function receive(timeout)
local to = nil
local send = nil
local msg = nil
local id,packet = rednet.receive(timeout)
if id ~= nil then
to,send,msg = explodePacket(packet)
end
if(to == "all") then to = os.getComputerID() end
return to,send,msg,id,packet
end
function restrictReveive(timeout,pck2)
if pck2 == nil then pck2 = false end
local to,sender,msg,nearRouter,packet = receive(timeout)
if to == sender then
if msg ~= "packet00" or msg ~= "packet01" or (pck2 and msg ~= "packet02")then
return to,sender,msg,nearRouter,packet
else return nil,nil,nil,nil,nil
end
end
end
function openPort()
print("Avez vous un port d'ouvert sur votre ordinateur ? [O]/N")
choise = read()
if choise == "N" or "n" then
print("1 - Haut")
print("2 - Bas")
print("3 - Droite")
print("4 - Gauche")
print("5 - Derriere")
print("6 - Devant")
print("")
choise = read()
if choise == "1" then
rednet.open("top")
elseif choise == "2" then
rednet.open("bottom")
elseif choise == "3" then
rednet.open("right")
elseif choise == "4" then
rednet.open("left")
elseif choise == "5" then
rednet.open("back")
elseif choise == "6" then
rednet.open("front")
else
print("ERREUR : reponse invalide")
end
end
end
function announce()
rednet.broadcast("all"..Gdiv .. sender ..Gdiv .."packet00")
end