A simple UDP broadcast protocol used for service discovery in a local network and an API to add services in real-time.
Server.py is an actual implementation of SO_BROADCAST protocl using berkely socket API. Server.py reads the content of service.txt and broadcasts it to local-network's boradcast IP using UDP protocol as it's transport mechanism. The server can broadcast packets at regular time intervals, any changes to service.txt is automatically updated during the next broadcast cycle.AddService API provides procedures to add and remove services from service.txt file, it is by default created in $HOME/.services directory.
call check_availability() to make sure the file exists, else a new service file will be created.
add_service() will add a new service entry.
from service_discovery import AddService
#import AddService API
#you have to always call this function to make sure service.txt is present
AddService.check_availability()
#create a new service definition:
serv = {
'name' : 'WaterServer',
'addr' : '192.168.43.190',
'description' : "Demo server",
'port' : 5000
}
#register the service:
AddService.add_service(serv)
#remove method:
AddService.remove_service(serv)
Now you can run server.py to broadcast service file.
You can configure server.py to run on startup on POSIX machines, so services will always be broadcasted and new services can be added and removed by API in parallel.
EventLoop thread of server.py implements a procedure called process_broadcast_data() which reads data from service.txt file, you can implement your own procedures to create different broadcast formats.