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

IPnet: GPS based IP system for internet protocol in Computercraft

Started by Creator, 30 January 2015 - 07:20 PM
Creator #1
Posted 30 January 2015 - 08:20 PM
Hello guys,

I'm working on a project that will recreate "internet" in minecraft thanks to CC. The idea is that I have MainRouters, SubRouters, and Clients. The Main routers transmit information over large distances. The clients receive the info. The SubRouters act as bridges between them. For the moment, the code I have written should permit the MainRouters to identify each other. The main routers should also identify the SubRouters. The subRouters should be able to identify the Clients. I have implemented a debugPrint function that take the value of debugOpt. If it is true it will print the text in the arguments. For the moment it doesn't semm like they identify themselves. The should be able to identify each other no matter the order in which they are booted. I hope you can help me. Leave any suggestion in the comments. If you wish to help givve a suggestion how it would be possible because I don't own a server. Just leave any way in which I could contact you (Skype, Facebook, Email). We will work out a solution.

Thanks

MainRouter ( there can be a bunch of them, they have the same importance)

Spoiler

--Variables--
adjascentRouters = {}
subRouters = {}
configuration = {}
debugOpt = true
--Too many variables--

--Functions--
function clear()
  term.setCursorPos(1,1)
  term.clear()
end

function debugPrint(txt)
  if debugOpt then
	print(txt)
  end
end

function getModemSide()
  local sides = {"bottom","right","left","front","back","top"}
  local side = {}
  for i,v in pairs(sides) do
	if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
	 side[#side + 1] = v
	end
  end
  if #side == 0 then return nil end
  debugPrint("Got modem side")
  return side[1]
end

function getIP()
  local x , y , z = gps.locate()
  ip = {x,y,z,os.getComputerID()}
  debugPrint("Got ip")
  return textutils.serialize(ip)
end

function setConfig()
  local configs = {}
  configs = textutils.unserialize(getIP())
  configs[5] = getModemSide()
  local configFile = fs.open("config","w")
  configFile.write(textutils.serialize(configs))
  configFile.close()
  debugPrint("Wrote configs")
end

function setUp()
  local tempConfigs = {}
  if fs.exists("config") then
	configFile = fs.open("config","r")
	tempConfigs = textutils.unserialize(configFile.readAll())
	configFile.close()
	debugPrint("File exists")
  else
	setConfig()
	configFile = fs.open("config","r")
	tempConfigs = textutils.unserialize(configFile.readAll())
	configFile.close()
	debugPrint("File does not exist")
  end
  if tempConfigs[5] == nil then
	setConfig()
	configFile = fs.open("config","r")
	tempConfigs = textutils.unserialize(configFile.readAll())
	configFile.close()
  elseif peripheral.isPresent(tempConfigs[5]) == false then
	setConfig()
	configFile = fs.open("config","r")
	tempConfigs = textutils.unserialize(configFile.readAll())
	configFile.close()
  elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
	setConfig()
	configFile = fs.open("config","r")
	tempConfigs = textutils.unserialize(configFile.readAll())
	configFile.close()
  end
  if tempConfigs[5] == nil then
	print("No modem attached")
	error()
  end

  modem = peripheral.wrap(tempConfigs[5])
  local idChannel = 65524
  local msgChannel = 65525
  modem.open(idChannel)
  modem.open(msgChannel)

  configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
end

function IPsender(a,b,c,d,e,f,g)
  local serializedMsgBuffer = {a,b,c,d,e,f,g}
  local serializedMsg = textutils.serialize(serializedMsgBuffer)
  modem.transmit(65525,1,serializedMsg)
end

function IPconfirm(a,b,c,d,e)
  local serializedMsgBuffer = {a,b,c,d,e}
  local serializedMsg = textutils.serialize(serializedMsgBuffer)
  modem.transmit(65525,2,serializedMsg)
end

function identification()
  serilaizedConfig = textutils.serialize(configuration)
  modem.transmit(65524,3,serilaizedConfig)
  local pcIsregistered = false

  while true do
	local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
	if ch2 == 3 and ch1 == 65524 then
	  modem.transmit(65524,4,serilaizedConfig)
	   for i,v in pairs(adjascentRouters) do
	   if  v == textutils.unserialize(msg) then
	   pcIsRegistered = true
	   end
	 end
	 if pcIsRegistered == false then
	  adjascentRouters[#adjascentRouters + 1] = textutils.unserialize(msg)
	 end
	 debugPrint("adjacentrouter",textutils.serialize(adjascentRouters))
	elseif ch2 == 4 and ch1 == 65524 then
	 for i,v in pairs(adjascentRouters) do
	  if  v == textutils.unserialize(msg) then
	   pcIsregistered = true
	  end
	 end
	 if pcIsregistered == false then
	  adjascentRouters[#adjascentRouters + 1] = textutils.unserialize(msg)
	 end
	 debugPrint("adjacentrouter",textutils.serialize(adjascentRouters))
	end
  end
end
function identificationOfSubRouters()
serilaizedConfig = textutils.serialize(configuration)
modem.transmit(65524,7,serilaizedConfig)
local subRouterIsregistered = false

while true do
  local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  if ch2 == 6 and ch1 == 65524 then
   modem.transmit(65524,5,serilaizedConfig)
   for i,v in pairs(subRouters) do
	if  v == textutils.unserialize(msg) then
	 subRouterIsregistered = true
	end
   end
   if subRouterIsregistered == false then
	subRouters[#subRouters + 1] = textutils.unserialize(msg)
   end
  elseif ch2 == 8 and ch1 == 65524 then
   for i,v in pairs(subRouters) do
	if  v == textutils.unserialize(msg) then
	 subRouterIsregistered = true
	end
   end
   if subRouterIsregistered == false then
	subRouters[#subRouters + 1] = textutils.unserialize(msg)
   end
  end
  debugPrint("subrouters:,",textutils.serialize(subRouters))
end
end

function checkAndSend()
local msgBuffer = {}
msgBuffer = textutils.unserialize(serializedMsgBuffer)
local destinationPc = {}
destinationPc = msgBuffer[1]
local sendingPc = {}
sendingPc = msgBuffer[2]
local toRouter = {}
toRouter = msgBuffer[3]
local sendingRouter = {}
sendingRouter = msgBuffer[4]
local msgNum = ""
msgNum = msgBuffer[5]
local msgToTransmit = ""
msgToTransmit = msgBuffer[6]
local port = {}
port = msgBuffer[7]
possibleRouters = adjascentRouters
debugPrint(textutils.serialize(possibleRouters))
local vectorLenghtTable = {}

for i,v in pairs(possibleRouters) do
  local xDist = math.abs(destinationPc[1]-v[1])
  local yDist = math.abs(destinationPc[2]-v[2])
  local zDist = math.abs(destinationPc[3]-v[3])
  local vectorBuffer = vector.new(xDist,yDist,zDist)
  vectorLenghtTable[#vectorLenghtTable+1] = vectorBuffer:lenght()
end
local winner = {}
for i,v in pairs(vectorLenghtTable) do
  debugPrint(i,v)
  if i == 1 then
   winner = {i,v}
  else
   if v < winner[2] then
	winner = {i,v}
   end
  end
end
local closestRouter = possibleRouters[winner[1]]
IPsender(destinationPc,sendingPc,closestRouter,configuration,msgNum,msgToTransmit,port)
timer = os.startTimer(5)
local event, side, ch1, ch2, confirmMessageBuffer, distance = os.pullEvent("modem_message" or "timer")
local confirmMessage = {}
confirmMessage = textutils.unserialize(confirmMessageBuffer)
if event == "modem_message" then
  if ch2 == 2 and confirmMessage[3] == configuration and confirmMessage[4] == closestRouter and confirmMessage[5] == msgNum then
   return true
  end
elseif event == "timer" and side == "timer" then
  for i,v in pairs(adjascentRouters) do
   if v == closestRouter then
	adjascentRouters[i] = nil
   end
  end
  local nilHappened = false
  for i,v in pairs(adjascentRouters) do
   if nilHappened then
	adjascentRouters[i-1] = v
   end
   if v == nil then
	nilHappened = true
   end
  end
  nilHappened = false
  return false
else
  return "invalid"
end
end
function messageSender()
local messageNotDelivered = true
while true do
  local event, side, ch1, ch2, message, distance = os.pullEvent("modem_message")
  if ch2 == 1 and ch1 == 65525 then
   local msgBuffer = {}
   msgBuffer = textutils.unserialize(message)
   local destinationPc = {}
   destinationPc = msgBuffer[1]
   local sendingPc = {}
   sendingPc = msgBuffer[2]
   local toRouter = {}
   toRouter = msgBuffer[3]
   local sendingRouter = {}
   sendingRouter = msgBuffer[4]
   local msgNum = ""
   msgNum = msgBuffer[5]
   debugPrint(message)
   local ifSuccessInSending = nil
   if toRouter == configuration then
	IPconfirm(destinationPc,sendingPc,sendingRouter,configuration,msgNum)
	ifSuccessInSending = checkAndSend(message)
	while ifSuccessInSending == false or ifSuccessInSending == "invalid" do
	 ifSuccessInSending = checkAndSend()
	 if #adjascentRouters == 0 then
	  clear()
	  print("No routers available...")
	  identification()
	  break
	 end
	end
   end
  elseif ch2 == 5 then
  
  end
end
end
--Too many functions--
clear()
setUp()
parallel.waitForAny(identification,messageSender,identificationOfSubRouters)

SubRouter

Spoiler

--Variables--
mainRouter = {}
configuration = {}
debugOpt = true
Clients = {}
--Too many variables--
--Functions--
function clear()
term.setCursorPos(1,1)
term.clear()
end
function debugPrint(txt)
if debugOpt then
  print(txt)
end
end
function getModemSide()
local sides = {"bottom","right","left","front","back","top"}
local side = {}
for i,v in pairs(sides) do
  if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
   side[#side + 1] = v
  end
end
if #side == 0 then return nil end
debugPrint("Got modem side")
return side[1]
end
function getIP()
local x , y , z = gps.locate()
ip = {x,y,z,os.getComputerID()}
debugPrint("Got ip")
return textutils.serialize(ip)
end
function setConfig()
local configs = {}
configs = textutils.unserialize(getIP())
configs[5] = getModemSide()

local configFile = fs.open("config","w")
configFile.write(textutils.serialize(configs))
configFile.close()
debugPrint("Wrote configs")
end
function setUp()
local tempConfigs = {}
if fs.exists("config") then
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
  debugPrint("File exists")
else
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
  debugPrint("File does not exist")
end
if tempConfigs[5] == nil then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
elseif peripheral.isPresent(tempConfigs[5]) == false then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
end

if tempConfigs[5] == nil then
  print("No modem attached")
  error()
end

modem = peripheral.wrap(tempConfigs[5])
local idChannelMain = 65524
local msgChannelMain = 65525
local idCannelClient = 65522
local msgChannelClient = 65523
modem.open(idChannelMain)
modem.open(msgChannelMain)
modem.open(idCannelClient)
modem.open(msgChannelClient)

configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
end
function IPsender(a,b,c,d,e,f,g)
local serializedMsgBuffer = {a,b,c,d,e,f,g}
local serializedMsg = textutils.serialize(serializedMsgBuffer)
modem.transmit(65525,1,serializedMsg)
end
function identificationMainRouter()
serilaizedConfig = textutils.serialize(configuration)
local mainRouterNotFound = true
modem.transmit(65524,6,serilaizedConfig)
while mainRouterNotFound do
  local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  if ch2 == 5 and ch1 == 65524 then
   mainRouter = textutils.unserialize(msg)
  
  elseif ch2 == 7  and ch1 == 65524 then
   modem.transmit(65524,8,serilaizedConfig)
   mainRouter = textutils.unserialize(msg)
  end
end
debugPrint("main router:",textutils.serialize(mainRouter))
end
function identificationClients()
serilaizedConfig = textutils.serialize(configuration)
modem.transmit(65522,11,serilaizedConfig)
local subRouterIsregistered = false

while true do
  local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  if ch2 == 10 and ch1 == 65522 then
   modem.transmit(65522,9,serilaizedConfig)
   for i,v in pairs(Clients) do
	if  v == textutils.unserialize(msg) then
	 subRouterIsregistered = true
	end
   end
   if subRouterIsregistered == false then
	Clients[#Clients + 1] = textutils.unserialize(msg)
   end
  
  elseif ch2 == 12 and ch1 == 65522 then
   for i,v in pairs(Clients) do
	if  v == textutils.unserialize(msg) then
	 subRouterIsregistered = true
	end
   end
   if subRouterIsregistered == false then
	Clients[#Clients + 1] = textutils.unserialize(msg)
   end
   debugPrint("clients:",textutils.serialize(Clients))
  end

end
end
clear()
setUp()
parallel.waitForAll(identificationClients,identificationMainRouter)

Client

Spoiler

--Variables--
subRouter = {}
configuration = {}
debugOpt = true
--Too many variables--
--Functions--
function clear()
term.setCursorPos(1,1)
term.clear()
end
function debugPrint(txt)
if debugOpt then
  print(txt)
end
end
function getModemSide()
local sides = {"bottom","right","left","front","back","top"}
local side = {}
for i,v in pairs(sides) do
  if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
   side[#side + 1] = v
  end
end
if #side == 0 then return nil end
debugPrint("Got modem side")
return side[1]
end
function getIP()
local x , y , z = gps.locate()
ip = {x,y,z,os.getComputerID()}
debugPrint("Got ip")
return textutils.serialize(ip)
end
function setConfig()
local configs = {}
configs = textutils.unserialize(getIP())
configs[5] = getModemSide()

local configFile = fs.open("config","w")
configFile.write(textutils.serialize(configs))
configFile.close()
debugPrint("Wrote configs")
end
function setUp()
local tempConfigs = {}
if fs.exists("config") then
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
  debugPrint("File exists")
else
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
  debugPrint("File does not exist")
end
if tempConfigs[5] == nil then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
elseif peripheral.isPresent(tempConfigs[5]) == false then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
  setConfig()
  configFile = fs.open("config","r")
  tempConfigs = textutils.unserialize(configFile.readAll())
  configFile.close()
end

if tempConfigs[5] == nil then
  print("No modem attached")
  error()
end

modem = peripheral.wrap(tempConfigs[5])
local idChannel = 65522
local msgChannel = 65523
modem.open(idChannel)
modem.open(msgChannel)

configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
debugPrint("configuration",textutils.serialize(configuration))
end
function identificationSubRouter()
serilaizedConfig = textutils.serialize(configuration)
local mainRouterNotFound = true
modem.transmit(65522,10,serilaizedConfig)
while mainRouterNotFound do
  local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  if ch2 == 9 then
   subRouter = textutils.unserialize(msg)
   debugPrint(msg)
  elseif ch2 == 11 then
   modem.transmit(65522,12,serilaizedConfig)
   subRouter = textutils.unserialize(msg)
   debugPrint(msg)
  end
end
end
function toReturn()
clear()
setUp()
identificationSubRouter()
end
toReturn()
InDieTasten #2
Posted 30 January 2015 - 09:40 PM
btw, length is written length, not lenght ;)/>
Creator #3
Posted 30 January 2015 - 09:49 PM
Thanx :D/>

What do you think of the iniciative?
HPWebcamAble #4
Posted 31 January 2015 - 12:30 AM
Interesting concept. I don't really have a need for this atm, but it could be useful for others.
There are a few browser/server pairs for CC already, you should look for them if you haven't already seen them

I haven't really looked much at the code, but one thing that stands out is the getModemSide() function

You could really simplify it:

function getModemSide()
  for i,v in pairs(rs.getSides()) do --#rs.getSides() just returns all the sides
	if peripheral.getType(v) == "modem" and peripheral.call(v,"isWireless") then
	  return v
	end
  end
  return false
end
Edited on 30 January 2015 - 11:34 PM
Creator #5
Posted 31 January 2015 - 03:00 PM
Thank you a lot. That is very intersesting. Any other suggestions?
_VoiceActorMan_ #6
Posted 12 February 2015 - 11:47 PM
Instead of GPS why not create a type of P2P-ARP by having nodes send out what computers they are connected to every so often and then relaying and appending the information the caching it. Similar to how P2P bot nets work.
Creator #7
Posted 13 February 2015 - 02:39 PM
Instead of GPS why not create a type of P2P-ARP by having nodes send out what computers they are connected to every so often and then relaying and appending the information the caching it. Similar to how P2P bot nets work.

Your idea is good, but the information sent about the computers they are connected too will rapidly become very large and the transferring speed of the network will not allow such a great number of packets to be sent. Moreover, this way there is a logical way to reach the destination.