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

Networking

Started by Liraal, 24 February 2012 - 05:07 PM
Liraal #1
Posted 24 February 2012 - 06:07 PM
Hey, I made a little API for rednet apps, but it doesn't seem to work. Getting all weird errors, and I can't snipe them myself after several hours of coding. Can anyone help?
[attachment=36:net.txt]
Espen #2
Posted 25 February 2012 - 05:10 PM
I'm not 100% sure because I'm not in-game to test it at the moment, but this here…
while a ~= "rednet_message" or b ~= user_resolve( server ) or c ~= "messageT" do
  local a, b, c = os.pullEventRaw()
end
… might not work, because the variables a, b and c which you declared local inside of the loop are only accessible from WITHIN the loop, but not from outside of the loop. And that should include the while-line itself too, if I'm not mistaken.
Instead try the following:
local a, b, c
while a ~= "rednet_message" or b ~= user_resolve( server ) or c ~= "messageT" do
  a, b, c = os.pullEventRaw()
end
This way the are declared local not to the loop, but to the scope it shares with the loop.

I haven't yet checked any other parts of your code yet, since it's not trivial to follow with a tired mind, one of which is inside of my skull at the moment. :(/>/>
But this should at least get you out of infinite loops.
Cheers
Liraal #3
Posted 25 February 2012 - 07:44 PM
Good idea, man! :)/>/>
It's too complicated for me to grasp it whole after writing it :(/>/>
MysticT #4
Posted 26 February 2012 - 01:17 AM
You could also use:

repeat
  local a, b, c = os.pullEventRaw()
until a == "rednet_message" and b == user_resolve( server ) and c == "messageT" do
That way, the variables will be only in the loop scope, just in case you use those variable names somewhere else.
Liraal #5
Posted 26 February 2012 - 09:10 AM
That too, but the biggest issue is the joining/leaving system, but i think i have a working idea.
Liraal #6
Posted 29 February 2012 - 04:40 PM
Newest version, still unable to connect :/ Can anyone help?


local side="back"
local nickname="Terminal"
local users={{nickname, os.computerID()}}
local server="Server"
function change_nickname(a)
nickname=a
return true
end
function change_server(a)
server=a
return true
end
function user_set(a)
users=a
end
function user_print()
for i=1, #users, 1 do
print(users[i][1], users[i][2])
end
end
function user_get()
rednet.broadcast("users")
local a,b,c=os.pullEvent("rednet_message")
while c~="users_list" do a,b,c=os.pullEvent("rednet_message") end
local a, b, c=os.pullEventRaw()
while a~="rednet_message" or b~=user_resolve(server) or c~="messageT" do a, b, c=os.pullEventRaw() end
users=receiveT(user_resolve(server))
return true
end
function user_send(:unsure:/>/>/>
rednet.send(b, "users_list")
sendT(b, users)
end
function user_add(B)/>/>/>
local a,d,c=os.pullEventRaw()
while a~="rednet_message" or d~=b do a,d,c=os.pullEventRaw() end
local tmp={c, b}
table.insert(users, tmp)
return true
end
function user_del(B)/>/>/>
for i=1, #users, 1 do
local tmp=users[i]
if tmp[1]==b then table.remove(users, i)end
end
user_sort()
end
function user_sort()
table.sort(users, tcomp())
end
function user_resolve(B)/>/>/>
for i=1, #users, 1 do
local tmp=users[i]
if tmp[2]==b then return tmp[1] end
end
return false
end
function tcomp(a ,B)/>/>/>
if a[1]<b[1] then return true else return false end
end
function setSide(a)
side=a
return true
end
function join()
rednet.open(side)
rednet.broadcast("join")
sleep(0.1)
rednet.broadcast(nickname)
if nickname~=server then user_get() end
return true
end
function leave()
rednet.broadcast("leave")
rednet.close()
return true
end
function sendS(a, m)
rednet.send(a, "messageS")
sleep(0.1)
rednet.send(a, m)
return true
end
function sendT(a, m)
rednet.send(a, "messageT")
sleep(0.1)
rednet.send(a, #m)
for i=1, #m, 1 do
rednet.send(a, m[i])
end
return true
end
function receive(a,b,c)
while a=="rednet_message" do
if c~="join" and c~="leave" and c~="messageS" and c~="messageT" then return b,c end
if c=="join" then user_add(B)/>/>/> end
if c=="messageS" then coroutine.yield(receiveS(B)/>/>/>) end
if c=="messageT" then coroutine.yield(receiveT(B)/>/>/>) end
if c=="leave" then user_del(B)/>/>/> end
if c=="users" then user_send(B)/>/>/> end
end
end
function receiveS(B)/>/>/>
local a,d,c=os.pullEventRaw()
while a~="rednet_message" or d~=b do local a,d,c=os.pullEventRaw() end
return c
end
function receiveT(B)/>/>/>
local tmp={}
local a,d,c=os.pullEventRaw()
while a~="rednet_message" or d~=b do local a,d,c=os.pullEventRaw() end
for i=1, c, 1 do
table.insert(tmp, e)
local a,d,e=os.pullEventRaw()
while a~="rednet_message" or d~=b do local a,d,c=os.pullEventRaw() end
end
return tmp
end
function send(a, m)
a=user_resolve(a)
sendS(a, m)
end
function run()
while true do
local a, b, c=os.pullEventRaw()
if a=="key" and b==205 then
print("Do you really want to exit?")
if ui.yN() then
return true
end
end
if a=="key" and b==14 then
write("Enter nickname") local tmp1=read() write("Enter Message") local tmp2=read() send(tmp1, tmp2) print("")
end
if a=="rednet_message" then receive(a,b,c) end
end
end
function start()
join()
run()
leave()
end
function startServer()
change_nickname(server)
user_set({{nickname, os.computerID()}})
run()
leave()
end