Instead of sending server responses back to the router, which could potentially create a bottleneck at the router, I had the router keep count of the available frequencies. When an allocate request passes through the router, the router decreases the server's available count; and when a deallocate request passes through the router, the router increases the server's available count (I didn't address the case where a client tries to deallocate a frequency it doesn't own). I also let the clients, which are running infinite loops, fail after I stop the router/servers.
In my code, a server
is a triple {ServerPid, Freqs, AvailableCount}
. For allocate requests, the router sorts the servers using lists:keysort()
on the third term to get the server with the highest AvailableCount. For deallocate requests, the router finds the server corresponding to the frequency: if no servers correspond to the frequency, then the router sends the client an error message; otherwise the router sends a deallcoate request