This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wolfnether's profile picture

MCnet

Started by wolfnether, 06 March 2012 - 05:31 PM
wolfnether #1
Posted 06 March 2012 - 06:31 PM
Hello (and sorry for my english),

i made programms and apis for Internet in Mincraft

Spoilerrouter program
Spoiler

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
mcn
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
wolfnether #2
Posted 06 March 2012 - 10:17 PM
change code
wolfnether #3
Posted 07 March 2012 - 03:23 PM
new version
wolfnether #4
Posted 10 March 2012 - 06:18 PM
router program no significant change
routerapi change protocol
mcn new version
Espen #5
Posted 10 March 2012 - 06:39 PM
Hey wolfnether,
it's generally considered bad etiquette to bump a thread with multi-posts.
There aren't any specific rules against that here as far as I know, but I'd advise using the edit-function nonetheless.
Lest you might achieve the opposite of what you were possibly aiming for (i.e. pissing people off).
Just friendly advice, no offense. :mellow:/>/>
Yurameki #6
Posted 11 March 2012 - 06:00 AM
Excuse my ignorance here, but what method is this API meant to be used by? Are we expecting to call this from other programs alone, or is something meant to be running on an existing set of systems somewhere, adding nodes as it runs? Maybe I'm just having trouble reading the code.
wolfnether #7
Posted 11 March 2012 - 08:46 PM
sorry but i don't understand :s

and i have debugged the code!