One-at-a-time Two-User Communications Handler
Context
Design and implement a communications handler that supports exactly one active conversation at a time between two distinct users. Each user is represented by a Caller with a unique name.
Requirements
-
Define a custom exception type:
-
ConnectionException that accepts a single error-message string.
-
Define an abstract base class CommsHandlerABC with these abstract methods:
-
connect(user1: Caller, user2: Caller) -> str
-
Establish a call and return a success message, or raise ConnectionException.
-
hangup(user1: Caller, user2: Caller) -> str
-
End the call and return a success message, or raise ConnectionException.
-
clear_all() -> None
-
Force-clear any existing call and reset state.
-
Implement a concrete CommsHandler that enforces:
-
Only one active call at any time.
-
A user cannot connect with themselves.
-
While a call is active, any new connect attempt fails with error: "Connection in use. Please try later".
-
hangup succeeds only for the currently connected pair (order-agnostic). Otherwise raise: "{A} and {B} not found in the communication channel." (note trailing period).
-
clear_all resets the state immediately.
I/O Behavior
-
Commands to support (semicolon-separated in a single line or as separate lines):
-
connect
<user1>
<user2>
-
hangup
<user1>
<user2>
-
clear_all
-
Output:
-
On success: prefix with "Success: " and include the handler's returned message.
-
On error: prefix with "Error: " and include the exception's message.
Sample
-
Sample Input:
-
connect Alice Bob; connect Carol Dave; hangup Alice Bob; connect Carol Dave; hangup Carol Dave; connect Eve Eve; hangup Alice Bob
-
Expected Output:
-
Success: Connection established between Alice and Bob; Error: Connection in use. Please try later; Success: Alice and Bob are disconnected; Success: Connection established between Carol and Dave; Success: Carol and Dave are disconnected; Error: Eve cannot connect with Eve; Error: Alice and Bob not found in the communication channel.