Jump to content




Simultaneous coroutines?

networking utility

15 replies to this topic

#1 mmine1

  • New Members
  • 2 posts
  • LocationEarth

Posted 17 December 2012 - 04:28 PM

Ok, so here's the deal:

I'm fairly new to ComputerCraft and Lua, however I know the basics thanks to a friend very fluent in it. I've been working on a basic IRC client for a couple days now, and my problem is this: how can I make a simultaneous coroutine? I've had to split sending an receiving messages over to computers (one with a large monitor, one only the terminal) because I haven't been able to let rednet rednet.open() and rednet.broadcast() at the same time without having only two clients take turns making one message then waiting for the next sender to make another message before sending another one. If anyone can help me out, that would be great.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 04:42 PM

So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()

Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".

NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.

Is this what you were after?

#3 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 17 December 2012 - 05:51 PM

 TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:

So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()

Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".

NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.

Is this what you were after?

That is completely unrelated to the question.

OP: Do something like this:

function receive()
id, msg, dist = rednet.receive()
end

function broadcast()
write("> ")
input = read()
rednet.broadcast(input)
end

parallel.waitForAny(receive, broadcast)

While you could write coroutines, the parallel API is a lot easier to use.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 17 December 2012 - 07:58 PM

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

While you could write coroutines, the parallel API is a lot easier to use.

What do you think the parallel API does with the functions you pass it?

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 08:00 PM

 Lyqyd, on 17 December 2012 - 07:58 PM, said:

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

While you could write coroutines, the parallel API is a lot easier to use.

What do you think the parallel API does with the functions you pass it?

I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 17 December 2012 - 08:03 PM

 TheOriginalBIT, on 17 December 2012 - 08:00 PM, said:

 Lyqyd, on 17 December 2012 - 07:58 PM, said:

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

While you could write coroutines, the parallel API is a lot easier to use.

What do you think the parallel API does with the functions you pass it?

I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.

Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 08:33 PM

 Lyqyd, on 17 December 2012 - 08:03 PM, said:

 TheOriginalBIT, on 17 December 2012 - 08:00 PM, said:

 Lyqyd, on 17 December 2012 - 07:58 PM, said:

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

While you could write coroutines, the parallel API is a lot easier to use.

What do you think the parallel API does with the functions you pass it?

I can understand why he recommended, it does make coroutines a little easier. however even parallel isn't simultaneous coroutine like the OP wanted. Coroutines only run one at a time.

Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.

I am aware.

#8 ChunLing

  • Members
  • 2,027 posts

Posted 17 December 2012 - 11:08 PM

I think that the intended meaning is that using the parallel API wouldn't be the same as writing a coroutine, it would be using a pre-written coroutine.

Unless you rewrote the parallel API.

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 December 2012 - 02:26 AM

 Lyqyd, on 17 December 2012 - 08:03 PM, said:

Yes, I am aware. The point was that he was suggesting "using the parallel API" instead of writing coroutines, which is nonsensical.
I'm pretty positive that using parallel instead of manually making your own coroutines can reduce your program by a couple lines, that and it usually makes your script's logic look more sound.

#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 18 December 2012 - 04:28 AM

Yes, using the parallel API to manage your coroutines can save you a few lines, if it's what you need. You do still have to write coroutines for it to run, though. :)

#11 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 18 December 2012 - 04:58 AM

 Lyqyd, on 17 December 2012 - 07:58 PM, said:

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

While you could write coroutines, the parallel API is a lot easier to use.

What do you think the parallel API does with the functions you pass it?

Yes, I know how the parallel API works, I was simply stating that it was more user-friendly.

#12 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 18 December 2012 - 05:03 AM

i wouldnt use coroutines for something like this, its too simple
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines

#13 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 18 December 2012 - 05:05 AM

 PixelToast, on 18 December 2012 - 05:03 AM, said:

i wouldnt use coroutines for something like this, its too simple
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines

But then you have to concatenate an empty string and the keypress, and incorporate backspace to remove the last character, and incorporate enter to finish it off. Its not worth it to me, when i could use read() and parallel.waitForAny().

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 December 2012 - 10:44 AM

 Dlcruz129, on 18 December 2012 - 05:05 AM, said:

 PixelToast, on 18 December 2012 - 05:03 AM, said:

i wouldnt use coroutines for something like this, its too simple
just loop os.pullEvent and look for "rednet_message" and "key"
this allows you to make a custom UI without confusing coroutines

But then you have to concatenate an empty string and the keypress, and incorporate backspace to remove the last character, and incorporate enter to finish it off. Its not worth it to me, when i could use read() and parallel.waitForAny().

read() can be terminated with ctrl + t

os.pullEventRaw cant be.

now as for messing with strings not hard,

outside the main loop
text = ""

event readers
event, param = os.pullEventRaw()

if event == "char" then
   text = text..param
eslseif event == "key" then
   if param == keys.backspace then
	 text = text:sub(1, text:len() - 1)
   elseif param == keys.enter then
	 processInput( text )
	 text = ""
   end
end

term.clear()
print( text )

Done. Not hard. Typed in a few seconds.

#15 mmine1

  • New Members
  • 2 posts
  • LocationEarth

Posted 18 December 2012 - 11:35 AM

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

 TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:

So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()

Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".

NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.

Is this what you were after?

That is completely unrelated to the question.

OP: Do something like this:

function receive()
id, msg, dist = rednet.receive()
end

function broadcast()
write("> ")
input = read()
rednet.broadcast(input)
end

parallel.waitForAny(receive, broadcast)

While you could write coroutines, the parallel API is a lot easier to use.

so I've tried to input that with it, and I still can only send messages but not receive them, however a computer with a program to scan the network still picks it up.

#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 December 2012 - 11:37 AM

 mmine1, on 18 December 2012 - 11:35 AM, said:

 Dlcruz129, on 17 December 2012 - 05:51 PM, said:

 TheOriginalBIT, on 17 December 2012 - 04:42 PM, said:

So the option I would suggest here is to use os.pullEvent() or os.pullEventRaw()

Every time an event triggers in the os it goes onto a stack when the pullEvent is called it pulls an event off that stack. so you can use this and check for "rednet_message".

NOTE: open rednet at the top of the program, close at the bottom or anywhere you deliberately leave the program.

Is this what you were after?

That is completely unrelated to the question.

OP: Do something like this:

function receive()
id, msg, dist = rednet.receive()
end

function broadcast()
write("> ")
input = read()
rednet.broadcast(input)
end

parallel.waitForAny(receive, broadcast)

While you could write coroutines, the parallel API is a lot easier to use.

so I've tried to input that with it, and I still can only send messages but not receive them, however a computer with a program to scan the network still picks it up.

I say try using pullEvents :) I've got a system working perfect with that.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users