Implement a Communication Handler with Exceptions
Context
You are building a single-line communication handler that can connect exactly two Caller instances at a time. The environment already provides:
-
Caller: a class with at least a .name attribute (assume implemented elsewhere).
-
CommsHandlerABC: an abstract base class specifying the interface for a communication handler.
Your task is to implement a concrete CommsHandler subclass that enforces the specified behaviors and to define a custom CommunicationException.
Requirements
Classes to Implement
-
CommsHandler(CommsHandlerABC)
-
Methods to implement:
-
connect(self, user1: Caller, user2: Caller) -> str
-
hangup(self, user1: Caller, user2: Caller) -> str
-
clear_all(self) -> None
-
Behavior:
-
connect:
-
If user1 is user2, raise CommunicationException with the exact message:
"{user1.name} cannot connect with {user2.name}!"
-
If user1 and user2 are different and the communication line is already in use, raise CommunicationException with:
"Connection in use. Please try later"
-
Otherwise, store the two connected users and return:
"Connection established between {user1.name} and {user2.name}"
-
hangup:
-
If user1 is user2, raise CommunicationException with:
"{user1.name} cannot hangup with {user2.name}"
-
If user1 and user2 are different and are currently communicating, disconnect them and return:
"{user1.name} and {user2.name} are disconnected"
-
If they are not communicating, raise CommunicationException with:
"{user1.name} and {user2.name} not found in the communication channel"
-
clear_all:
-
Clear the communication channel regardless of current users (no return value).
-
CommunicationException(Exception)
-
Accepts an error message in its constructor and is raised as specified.
Notes
-
Use identity checks (is) when comparing callers (i.e., same object instance), not name equality.
-
The handler supports only one active connection at a time.
-
Provide brief tests or usage examples demonstrating the specified behaviors.