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

Wireless Router

Started by cokacola, 11 November 2013 - 03:53 AM
cokacola #1
Posted 11 November 2013 - 04:53 AM
Title: Wireless Router

I am not sure what the best way to achieve this with CC is so I thought I'd ask.
I have a base on mars with galacticraft and I can't use a Computer as a valid block on the wall or the oxygen doesn't work, so I need to use some kind of wireless router outside the base to connect any computers inside.
Basically one or many computers connects to the router, and then it communicates with a server or another computer using the wired modem.
I suppose it's easy with two computers, but having many, the router would also need to identify computers and send the IDs to the server so it knows what to respond.
I assume it will require some kind of threading, since if I remember correctly the read/writes block.
So, what should I look into to achieve this?
TheOddByte #2
Posted 11 November 2013 - 02:27 PM
First of all it would be good if you checked out Rednet on the wiki, After you've done that you could store all IDs in a table on the server that you received from the router and use that table so it knows what to respond to. ( You don't need to use wired modems when there's wireless ones :P/> )
Edited on 11 November 2013 - 01:34 PM
Agoldfish #3
Posted 11 November 2013 - 04:58 PM
First of all it would be good if you checked out Rednet on the wiki, After you've done that you could store all IDs in a table on the server that you received from the router and use that table so it knows what to respond to. ( You don't need to use wired modems when there's wireless ones :P/> )
To make a table with the ID's of the computer do…

local idTable = {0,1,2,3,4,5} --#You can use whatever ids you need to use.
--#To print computer ID 3 do...
print(idTable[3])
That is an example of course. You can do PLENTY of things.
Also, if you need more help with tables. This video, or this topic may help.
cokacola #4
Posted 12 November 2013 - 05:00 AM
Thanks, I'll look into those.
I know how to use rednet and such, the problem is though, the router needs to listen to the server and the client(s) at the same time, which means it needs to listen to two modems with rednet.
I am not sure how to do this with coroutines.
Should I use those or just have two routers(one to the server and one from)?
Bomb Bloke #5
Posted 12 November 2013 - 05:50 AM
You'd be better off using the modem API here. The rednet API basically does that for you, but the key word there is "basically" - in the process it strips away some features you'll be needing for this setup. In particular, using the modem API directly allows you to determine which of your two modems are receiving messages, and it also lets you specify which of your modems to send with.

The basic structure of your program would be an infinite loop that waits for modem_message events. When it receives them, it checks to see which side they came from, then sends them off through the other side. Loop repeats. Simple, and there's certainly no need for co-routines.

You don't really need to be waiting for a message when it's sent (unlike what the wiki tells you about rednet.receive() - this little lie stunted my understanding of the event-system Lua uses for some time), so you don't really need to worry about whether your program is waiting for messages "at all times" or not. Each message (whether sent via the modem or rednet APIs) goes into the event queue of the system it's aimed at and stays there until you pull it - the trick is to make sure that you don't accidentally pull and discard these events without paying them due heed. Use of the sleep function, for eg, will cause your program to starting throwing away all events in the queue until it finds one that tells it to stop sleeping… But the events don't evaporate on their own in the way the wiki implies.

So the only tricky bit is working out a protocol whereby computers on one side of the wall know they need to message your router in order to get data to the server on the other side. You might do this by having the router open up a ton of channels (so it simply receives all traffic by default), or by making all the client computers think that the router is really the server (and having them message their server requests to it rather then to the server's real ID).