-- minux netlib client api
-- basic protocol meant for standard rednet communication or launching another protocol
-- written by shorun, special thanks to LDDestroier and Missooni for being such great learning tools.

-- we load the expect function
local doexpectfile = "/rom/modules/main/cc/expect.lua"
local doexpect = dofile(doexpectfile)

-- ping function, client sideused to check if a system is live
function ping(client)
    doexpect(1, client, "number")
   -- if client == os.getComputerID() == true then return true end
    rednet.send(client, "ping")
    local attempt = 0
    local keepalive = true
    local response = false
    local tempid = false
    while attempt ~= 3 and keepalive == true do
        tempid, response = rednet.receive(1)
        if tempid == client then
            keepalive = false
        else
            attempt = attempt + 1
        end
    end
    if response == nil then
           response = false
    end
    return response
end

-- ping function, used to check if a system is live
function getping(client)
    doexpect(1, client, "number")
    rednet.send(client, true)
    return true
end

-- sendstring, used to send a single string with verification
function sendstring(client, data)
    doexpect(1,client, "number")
    doexpect(2,data,"string")
    if client == os.getComputerID() then
        _G.transferstring = data
        rednet.send(client, "wakeup!")
        return true
    else
        response = false
        local attempt = 0
        while response == false and attempt ~= 3 do
            attempt = attempt + 1
            rednet.send(client, data)
            local tempclient, tempdata = rednet.receive(1)
            if tempdata == data then
                response = true
            end
            if client == os.getComputerID() == true then response = true end
            rednet.send(client, response)
        end
    end
    return response
end

-- getstring, used to send a single string with verification
function getstring(client, timeout)
    doexpect(1, client, "number")
    doexpect(2, client, "number", "nil")
    local tempid, tempdata = rednet.receive(timeout)
    if tempid == client then
        rednet.send(client, tempdata)
        tempid , verdata = rednet.receive(1)
        if verdata == nil then tempdata = false end
    end
    if _G.transferstring ~= nil then
        tempdata = _G.transferstring
        _G.transferstring = nil
    end
    return tempdata
end

-- sendtable, used to send a table
function sendtable(client, data)
    doexpect(1,client, "number")
    doexpect(2,data,"table")
    local tempfile = fs.open("/temp/netlib/table.conv","w")
    count = 1
    while data[count] ~= nil do
        if data[count] ~= nil then tempfile.writeLine(data[count]) end
        count = count +1
    end
    tempfile.close()
    local tempfile = fs.open("/temp/netlib/table.conv","r")
    local data = tempfile.readAll()
    tempfile.close()
    if client == os.getComputerID() == true then
        _G.transferstring = data
        rednet.send(client, "wakeup!")
        return true
    else
        local attempt = 0
        local keepalive = true
        while keepalive == true and attempt ~= 3 do
            rednet.send(client, data)
            tempid, tempdata = rednet.receive(1)
            if tempdata == data then
                rednet.send(client, true)
                keepalive = false
                return true
            end
        end
    end
    if keepalive == true then return false end
end

-- gettable, used to send a table with verification
function gettable(client, timeout)
    doexpect(1,client,"number")
    doexpect(2,client,"number","nil")
    local tempid, tempdata = rednet.receive(timout)
    if tempid == client or _G.transferstring ~= nil then
        if _G.transferstring ~= nil then
            tempdata = _G.transferstring
            _G.transferstring = nil
        else
            rednet.send(client, tempdata)
            tempid , verdata = rednet.receive(1)
            if verdata ~= true then
                return false
            end
        end
        local tempfile = fs.open("/temp/netlib/table.tmp","w")
        tempfile.write(tempdata)
        tempfile.close()
        tempfile = fs.open("/temp/netlib/table.tmp","r")
        count = 1
        returntable = {}
        returntable[count] = tempfile.readLine()
        while returntable[count] ~= nil do
            count = count + 1
            returntable[count] = tempfile.readLine()
        end
        tempfile.close()
        return returntable
    end
    return false
end
