Search code examples
gomutexchannelgoroutine

emulate radio communication using channel or mutex


I need to emulate a radio communication network composed of N nodes, with these properties:

  1. nodes either send then receive data, or receive then send data, but not at the same time.
  2. data sent over-the-air are received by all nodes which are in receive mode at that time.
  3. if two or more nodes sends data simultaneously, data are lost.
  4. there is no time synchronization among nodes.

In Go, if I use a channel to emulate the transmission media, data are serialized, and only one receiver gets the data, not all of them.

Also, I cannot think of a way to "ruin" the data if two sender try to send at the same time. Whether I use a mutex or not, one of the sender will successfully get its message sent.


Solution

  • Why don't you create a publisher and subscriber module using Golang Channels.

    Create a centralized queuing system where all your sender and receiver nodes register themself. If any node sends the data it will go to that module and from the sender's list, it picks the senders channel and starts writing on it. The same applies to receivers also.

    You have to create one channel per node and register it to a central pub/sub module. This will definitely solve your problem.