I have a problem creating a UDP crossbar multiplexer in python. What I need is to create a program where there are many sources and sinks for UDP data.
For the purposes of this:
In the image above, you can see that Sink 1 is connected to Source 3, Sink 2 is connected to Source 1, etc. Notice that Source 1 is also connected to Sink n.
The goal is to have any data that is received on a Source replicated out to any Sinks attached to that Source. I need to be able to change the Source/Sink connections on the fly. Any data received by a Source port when there are no Sinks connected to it get binned/ignored (not queued).
I have spent a few days on this now and getting really frustrated. I cant seem to figure out a way of storing the Source/Sink connections in a sensible way, and I cant figure out how to get my socket class to have a thread for each Source as all the examples I see are for creating a multi-threaded server using a single port.
I am not looking for someone to write this for me, just a couple of pointers and I can figure it out from there as it would be better for me to fully understand the code rather than copy/paste.
Thanks
The goal is to have any data that is received on a Source replicated out to any Sinks attached to that Source.
That should be straightforward enough.
I cant seem to figure out a way of storing the Source/Sink connections in a sensible way,
For each Source you need to store the set of zero or more Sinks that packets arriving at the Source are to be forwarded to. Assuming you are representing a Source as some kind of Python object, you could give it an attribute that is a Python Dictionary (or if you prefer a Python List) and add/remove Sinks to that list/dictionary as appropriate. Then whenever the Source receives a UDP packet, you simply iterate over the list/dictionary and call send()
or sendto()
on each Sink that is currently in the group.
I cant figure out how to get my socket class to have a thread for each Source as all the examples I see are for creating a multi-threaded server using a single port.
I suggest avoiding multithreading as it isn't necessary for this use-case and introduces a lot of complications that are better off avoided. Instead:
Note that for best performance you might also need to monitor sockets for write-ready so that you don't end up dropping outgoing UDP packets if/when a particular UDP socket's outgoing-data-buffer is full, but for an initial implementation you can cheat a little bit to simplify things by just assuming that your UDP sockets outgoing-data-buffers will always have enough space for any packets you want to send()
.