local liste = {}
connect = false
boucleWait = true

-- Fonction de connexion

local function filtre(periph)
  table.insert(liste, periph)
  return true
end

function connectModem(type)
if peripheral.isPresent("top") or peripheral.isPresent("bottom") or peripheral.isPresent("front") or peripheral.isPresent("back") or peripheral.isPresent("left") or peripheral.isPresent("right") then
  peripheral.find("modem", filtre)
  modem = peripheral.wrap(liste[1])
  modem.open(type)
  local wireless = modem.isWireless()
  if not wireless then
    connect = true
  end
end
local file = fs.open("GPnet/CacheConnect","w")
file.write(connect)
file.close()
end

-- Fonction d'erreur critique

function critERR(numero)
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)

crit01 = "Erreur critique 01 : Time out"

if numero == 01 then
  pX = 51 / 2 - string.len(crit01) / 2
  pY = 10
  print(crit01)
end
sleep(3)
os.shutdown()
end

-- Fonction d'envoi et de reception de donnees

function transmitData(inputCible, inputArg, type)
iRequete = {
  cible = inputCible,
  arg = inputArg
}
requete = textutils.serialize(iRequete)
modem.transmit(type,1,requete)
requete = nil
end

function waitResponding(temps, type)
tOut = os.startTimer(temps)
boucleWait = true
while boucleWait do
local event, side, freq, origine, requete = os.pullEvent()
if event == "timer" then
  boucleWait = false
  return "time out"
elseif event == "modem_message" then
  if freq == type then
    uRequete = textutils.unserialise(requete)
    include = string.find(uRequete.arg,"include")
    
    if uRequete.arg ~= "initConnect" and not include then
      boucleWait = false
      return uRequete.arg
    end
  end
end
end
end

-- Fonction d'integration d'image

function loadIMG(inputCible, fichier, temps)
chemin = "include->"..fichier
net.transmitData(inputCible,chemin,1)
tOut = os.startTimer(temps)
boucleWait = true
while boucleWait do
local event, side, freq, origine, requete = os.pullEvent()
if event == "timer" then
  boucleWait = false
  net.critERR(01)
elseif event == "modem_message" then
  if freq == 1 then
    uRequete = textutils.unserialise(requete)
    include = string.find(uRequete.arg,"include")
    
    if uRequete.arg ~= "initConnect" and not include then
      boucleWait = false
      file = fs.open("GPnet/cacheIMG","w")
      file.write(uRequete.arg)
      file.close()
      return "GPnet/cacheIMG"
    end
  end
end
end
end

-- Fonction de chargement de page additionnelles

function askPage(inputCible, fichier, temps)
chemin = "include->"..fichier
net.transmitData(inputCible,chemin,1)
tOut = os.startTimer(temps)
boucleWait = true
while boucleWait do
local event, side, freq, origine, requete = os.pullEvent()
if event == "timer" then
  boucleWait = false
  return "time out"
elseif event == "modem_message" then
  if freq == 1 then
    uRequete = textutils.unserialise(requete)
    include = string.find(uRequete.arg,"include")
    
    if uRequete.arg ~= "initConnect" and not include then
      boucleWait = false
      return uRequete.arg
    end
  end
end
end
end

-- Fonction de telechargement de fichier

function downloadFile(inputCible, fichier, dest, temps)
chemin = "include->"..fichier
net.transmitData(inputCible,chemin,1)
tOut = os.startTimer(temps)
boucleWait = true
while boucleWait do
local event, side, freq, origine, requete = os.pullEvent()
if event == "timer" then
  boucleWait = true
  net.critERR(01)
elseif event == "modem_message" then
  if freq == 1 then
    uRequete = textutils.unserialise(requete)
    include = string.find(uRequete.arg,"include")
    
    if uRequete.arg ~= "initConnect" and not include then
      boucleWait = false
      destination = fs.open(dest,"w")
      destination.write(uRequete.arg)
      destination.close()
    end
  end
end
end
end

-- Fonction de deconnexion

function disconnect()
local file = fs.open("GPnet/CacheConnect","w")
file.write("false")
file.close()
end

-- Fin
