"""Module containing a basic Broker whichmay be inherited by specialized classes."""importthreading
[docs]classSingleton(type):"""Global singleton to ensure only one broker exists"""_instances={}def__call__(cls,*args,**kwargs):ifclsnotincls._instances:cls._instances[cls]=super(Singleton,cls).__call__(*args,**kwargs)returncls._instances[cls]
[docs]defregister_client(self,client):"""Append the given client to the list of clients."""withself.lock:self._clients.add(client)
[docs]defdelete_client(self,client):"""Delete the given client from the list of clients"""withself.lock:try:self._clients.remove(client)exceptKeyError:pass
[docs]defdelete_all_clients(self):"""Delete all clients from the list of clients"""withself.lock:forclientinlist(self._clients):try:self._clients.remove(client)exceptKeyError:pass
def__repr__(self):"""Overwrite build-in function."""returnf"{self.__class__.__name__} with registered agents: {self._clients}"