[Lua][Red Net][Programming] Red Net Question
#1
Posted 21 June 2012 - 01:59 AM
#2
Posted 21 June 2012 - 08:52 AM
(THIS has only been tested on off line) Please report any bugs to me and i will do what i can to fix them.
#3
Posted 21 June 2012 - 01:27 PM
#4
Posted 21 June 2012 - 01:50 PM
#5
Posted 21 June 2012 - 01:58 PM
EmTeaKay, on 21 June 2012 - 01:27 PM, said:
in computer craft copy past this in. if the http api is active your computer will automatically download my program to the computer you are using.
pastebin get EP6TyqtV BasicIRCthe main problem is that read() is not designed to work in a coroutine and you need your own ver to make it interruptible (when a message arrives.)
tfoote, on 21 June 2012 - 01:50 PM, said:
#6
Posted 21 June 2012 - 04:09 PM
IRC lite
that is the smallest I can make if I'm shore that other's could make a shorter ver But for now this is as small as I can make it without sacrificing functionality. I have saved 44 lines from 185 to 141 .
#7
Posted 21 June 2012 - 04:10 PM
#8
Posted 21 June 2012 - 04:17 PM
It does as this request requires.
EmTeaKay, on 21 June 2012 - 01:59 AM, said:
tfoote, on 21 June 2012 - 04:10 PM, said:
#9
Posted 21 June 2012 - 04:57 PM
Oh! wait, there is one: the parallel api.
It would look something like this:
local function send() local msg = read() rednet.broadcast(msg) -- you can change it to send to an specific id end local function receive() local id, msg = rednet.receive() -- do whatever you want with the message end parallel.waitForAny(send, receive)So good Dan added this to CC
#10
Posted 21 June 2012 - 05:12 PM
MysticT, on 21 June 2012 - 04:57 PM, said:
Oh! wait, there is one: the parallel api.
It would look something like this:
local function send() local msg = read() rednet.broadcast(msg) -- you can change it to send to an specific id end local function receive() local id, msg = rednet.receive() -- do whatever you want with the message end parallel.waitForAny(send, receive)So good Dan added this to CC
a this only broadcasts
b have you tryed to use read in a coroutine before also what happens if you are mid message and you recive a message. there are many things you are not taking into consideration there.
I would like to expand my programing skill and knowledge could you please post a working system using the above that doesn't cut you off mid message and updates the screen correctly.
#11
Posted 21 June 2012 - 05:31 PM
This is a more complete example:
local nW, nH = term.getSize()
local function send()
while true do
term.setCursorPos(1, nH - 1)
term.clearLine()
local msg = read()
rednet.broadcast(msg) -- you could change this to rednet.send() and use a predefined id, or make the user input the id.
end
end
local function receive()
while true do
local id, msg = rednet.receive()
local x, y = term.getCursorPos()
term.setCursorPos(1, 1) -- write messages on the first line
print(id, ": ", msg)
term.setCurorPos(x, y)
end
end
parallel.waitForAny(send, receive)
There's still some things that would need to be changed, but it should work. It would greatly depend on what you'r trying to do, if you want a chat system, you need to print the messages in the right place, but for other things you might just need to do something for the received message, in that case it's not necesary.
#12
Posted 21 June 2012 - 06:18 PM
MysticT, on 21 June 2012 - 05:31 PM, said:
This is a more complete example:
local nW, nH = term.getSize() local function send() while true do term.setCursorPos(1, nH - 1) term.clearLine() local msg = read() rednet.broadcast(msg) -- you could change this to rednet.send() and use a predefined id, or make the user input the id. end end local function receive() while true do local id, msg = rednet.receive() local x, y = term.getCursorPos() term.setCursorPos(1, 1) -- write messages on the first line print(id, ": ", msg) term.setCurorPos(x, y) end end parallel.waitForAny(send, receive)There's still some things that would need to be changed, but it should work. It would greatly depend on what you'r trying to do, if you want a chat system, you need to print the messages in the right place, but for other things you might just need to do something for the received message, in that case it's not necesary.
He was looking for a program that does what the program I produced does a basic IRC
EmTeaKay, on 21 June 2012 - 01:27 PM, said:
so if i add a loop around the "parallel.waitForAny(send, receive)" line the program will run till it hits a time limit on the rednet.receive() function or a message is received or a message is typed and sent. Going by your code if you receive a message or the time limit runs down while in the process of sending the message you were writing would be lost. This is why I chose to use coroutines directly instead of the parallel system witch is based on them anyway.
Could you please try my program and then try to replicate its functionality with parallel function. By my understanding of lua and coroutine it will not work as you expect it to.
#13
Posted 21 June 2012 - 06:44 PM
local nScreenW, nScreenH = term.getSize()
local nLine = 0
local function clear()
term.clear()
term.setCursorPos(1, 1)
end
local function connect()
for _,s in ipairs(rs.getSides()) do
if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
rednet.open(side)
return true
end
end
return false
end
local function send()
while true do
term.setCursorPos(1, nScreenH - 1)
local msg = read()
term.setCursorPos(1, nScreenH - 1)
term.clearLine()
write("To: ")
local id = tonumber(read())
if id then
rednet.send(id, msg)
end
end
end
local function receive()
while true do
local id, msg = rednet.receive()
local x, y = term.getCursorPos()
if nLine >= nScreenH then
for i = 1, nScreenH - 2 do
term.setCursorPos(1, i)
term.clearLine()
end
nLine = 1
end
term.setCursorPos(1, nLine)
nLine = nLine + print(id, ": ", msg)
term.setCursorPos(x, y)
end
end
if not connect() then
print("No modem found")
return
end
clear()
parallel.waitForAny(send, receive)
Still not perfect, and needs some changes, but I don't have time now to do it.It's not tested, bit it should let you send and receive messages. The received messages are wrote to a new line. If the screen is full, it clears it and starts writing messages from the start (not the best, but it's the easiest solution I found).
#14
Posted 21 June 2012 - 07:28 PM
I concede it is possible to use parallel.waitForAny() for this but equally it would work with parallel.waitForAll()
this program demonstrates some of the short falls for the reed function. it doesn't redraw a the screen unless you have made a change. so it will not update when test is dumped on top of it. this is why my code is so long as i have to include a modified reed function.
I would like to see a finished polished ver of this code. also I request you try mine to see how I coped with the various problems like text over write.
#15
Posted 21 June 2012 - 08:20 PM
BigSHinyToys, on 21 June 2012 - 07:28 PM, said:
BigSHinyToys, on 21 June 2012 - 07:28 PM, said:
For other things it would be better/easier to use parallel. Mostly when you don't need to use read() or redraw the screen.
BigSHinyToys, on 21 June 2012 - 07:28 PM, said:
I readed your code, and there's a lot of things that could be improved, and some things that has to be removed. Like:
local function writer() while true do coroutine.yield() writer2() end endwhy do you even need that function?
#16
Posted 21 June 2012 - 09:59 PM
function writeChar(char) --Future update will be able to make longer messages term.setCursorPos(charNum + string.len(username) + 3, cursorY) --right now limit is 48 minus username length and 2 extra characters if charNum <= 48 - (string.len(username) + 2) then --so it all fits on-screen term.write(char) message[charNum] = char charNum = charNum + 1 end end
function handleEvent()
local evt, p1, p2 = os.pullEvent()
if evt == "char" then
writeChar(p1)
elseif evt == "key" then
handleKey(p1)
elseif evt == "rednet_message" then
messageReceive(p1, p2)
elseif evt == "timer" then
handleTimers(p1)
elseif evt == "peripheral" then
handleModem(p1)
updatePast("Modem detected on " .. p1) --custom function that will display messages, pass message as parameter.
updatePast("Network restored.")
elseif evt == "peripheral_detach" then
updatePast("Modem detached on " .. p1)
updatePast("No network connectivity.")
end
end
Also, small preview for next update.
#17
Posted 22 June 2012 - 03:01 AM
MysticT, on 21 June 2012 - 08:20 PM, said:
You'r right, read() has some problems when doing something like this, that's why I don't think this is the best way to do a chat program (neither coroutines nor parallel), since all you have to do is handle some events. I made a chat program and it doesn't use coroutines or parallel (wich are basically the same), it's on the program library if you want to take a look at it.
For other things it would be better/easier to use parallel. Mostly when you don't need to use read() or redraw the screen.
I don't think I will finish that code, since it was just an example, and I don't think it's the best way to do this.
I readed your code, and there's a lot of things that could be improved, and some things that has to be removed. Like:
local function writer() while true do coroutine.yield() writer2() end endwhy do you even need that function?
local reciveHandel = coroutine.create(recive) local writerHandel = coroutine.create(writer) -- writer function starts run here local sendHandel = coroutine.create(send) while true do -- start a loop local e,e1,e2,e3,e4,e5 = os.pullEvent() coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5) coroutine.resume(writerHandel) -- write function over writes test printed by recive function coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5) -- reed over writes all end result all information stays where I want it on the screen. end
so you could remove it but the to : or mes : would be over write by part of a received message as the messages scroll up.
Bossman201, on 21 June 2012 - 09:59 PM, said:
#18
Posted 22 June 2012 - 04:03 PM
BigSHinyToys, on 22 June 2012 - 03:01 AM, said:
MysticT, on 21 June 2012 - 08:20 PM, said:
You'r right, read() has some problems when doing something like this, that's why I don't think this is the best way to do a chat program (neither coroutines nor parallel), since all you have to do is handle some events. I made a chat program and it doesn't use coroutines or parallel (wich are basically the same), it's on the program library if you want to take a look at it.
For other things it would be better/easier to use parallel. Mostly when you don't need to use read() or redraw the screen.
I don't think I will finish that code, since it was just an example, and I don't think it's the best way to do this.
I readed your code, and there's a lot of things that could be improved, and some things that has to be removed. Like:
local function writer() while true do coroutine.yield() writer2() end endwhy do you even need that function?
local reciveHandel = coroutine.create(recive) local writerHandel = coroutine.create(writer) -- writer function starts run here local sendHandel = coroutine.create(send) while true do -- start a loop local e,e1,e2,e3,e4,e5 = os.pullEvent() coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5) coroutine.resume(writerHandel) -- write function over writes test printed by recive function coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5) -- reed over writes all end result all information stays where I want it on the screen. end
so you could remove it but the to : or mes : would be over write by part of a received message as the messages scroll up.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











