class documentation

class WebSocketServer(object):

View In Hierarchy

WebSocket Server implementation.

Method addConnection Add a new connection to the list of connections.
Method associateConnectionWithOriginator Associate a connection with an originator.
Method closeConnectionForOriginator Close a connection for an originator.
Method configure Configure the WebSocket server.
Method dissociateConnectionFromOriginator Dissociate a connection from an originator.
Method handleIncomingConnection Handle a new incoming WebSocket connection. This connection stays open until the client closes it, or the server is stopped.
Method incrementConnection Increment the counter for a connection.
Method init Initialization of the WebSocket Server.
Method pause Stop handling requests.
Method receiveLoop Receive loop for the WebSocket server. This is the main entry point for handling a received message, whether the connection was initiated by the server or the client.
Method removeConnection Remove a connection from the list of connections. Also remove the association between the connection and the originator.
Method run Initialize and run the WebSocket server as a BackgroundWorker/Actor.
Method sendWSRequest Send a request to another WebSocket server.
Method shutdown Shutdown the WebSocket server.
Method unpause Continue handling requests.
Method validate Validate the WebSocket server configuration.
Class Variable __slots__ Define slots for instance variables.
Class Variable requestManager Injected RequestManager instance.
Class Variable securityManager Injected SecurityManager instance.
Instance Variable actor The actor for running the synchronous WebSocket server in the background.
Instance Variable associatedConnections The list of currently handled WebSocket connections, associated with an originator.
Instance Variable connectionUsedCounter A counter for each opened WS connection opened by the CSE.
Instance Variable isPaused Flag whether the server is currently paused. Requests are not handled when the server is paused.
Instance Variable operationEvents Events for the different operations.
Instance Variable websocketServer The WebSocket server object.
Instance Variable wsConnections The list of currently handled WebSocket connections.
Method _checkIsServerRunning Check if the server is running. If not, send an error message to the client.
Method _configUpdate Callback for the configUpdate event.
Method _getWSSendingTargetName Get the target name for sending a WebSocket request.
Method _handleAuthentication Handle the authentication of a new connection.
Method _handleDeleteEvent Callback and handler for the deleteResource event.
Method _handleReceivedMessage Handle a received message. This is the main entry point for handling a received message, whether the message is a request or a response.
Method _run WebSocket server main loop.
Method _stop Stop the WebSocket server.
def addConnection(self, websocket: WSConnection, cseInitiated: bool = False):

Add a new connection to the list of connections.

Parameters
websocket:WSConnectionThe WebSocket connection.
cseInitiated:boolUndocumented
def associateConnectionWithOriginator(self, websocket: WSConnection, originator: str):

Associate a connection with an originator.

If the originator is already associated with another connection then that connection is closed.

Parameters
websocket:WSConnectionThe WebSocket connection.
originator:strThe originator.
def closeConnectionForOriginator(self, originator: str):

Close a connection for an originator.

Parameters
originator:strThe originator.
def configure(self, config: Configuration):

Configure the WebSocket server.

Parameters
config:ConfigurationThe configuration object to update with the WebSocket server configuration.
def dissociateConnectionFromOriginator(self, originator: str) -> bool:

Dissociate a connection from an originator.

Parameters
originator:strThe originator to dissociate.
Returns
boolTrue if the originator was associated with a connection, False otherwise.
def handleIncomingConnection(self, websocket: ServerConnection):

Handle a new incoming WebSocket connection. This connection stays open until the client closes it, or the server is stopped.

Note

This method is not called for connections initiated by the server.

Parameters
websocket:ServerConnectionThe WebSocket connection.
def incrementConnection(self, websocket: WSConnection):

Increment the counter for a connection.

Parameters
websocket:WSConnectionThe WebSocket connection.
def init(self):

Initialization of the WebSocket Server.

def pause(self):

Stop handling requests.

def receiveLoop(self, websocket: WSConnection, wsOriginator: str, ct: ContentSerializationType, authResult: AuthorizationResult):

Receive loop for the WebSocket server. This is the main entry point for handling a received message, whether the connection was initiated by the server or the client.

Parameters
websocket:WSConnectionThe WebSocket connection.
wsOriginator:strThe originator of the connection.
ct:ContentSerializationTypeThe content type.
authResult:AuthorizationResultThe result of the request authentication.
def removeConnection(self, websocket: WSConnection, originator: str):

Remove a connection from the list of connections. Also remove the association between the connection and the originator.

Parameters
websocket:WSConnectionThe WebSocket connection.
originator:strThe originator.
def run(self) -> bool:

Initialize and run the WebSocket server as a BackgroundWorker/Actor.

def sendWSRequest(self, request: CSERequest, url: str, ignoreResponse: bool) -> Result:

Send a request to another WebSocket server.

Parameters
request:CSERequestThe request to send.
url:strThe URL to send the request to.
ignoreResponse:boolFlag whether to ignore the response.
Returns
ResultThe result object of the request.
def shutdown(self) -> bool:

Shutdown the WebSocket server.

def unpause(self):

Continue handling requests.

def validate(self, config: Configuration):

Validate the WebSocket server configuration.

Parameters
config:ConfigurationThe configuration object to validate.
__slots__: list[str] =

Define slots for instance variables.

requestManager: RequestManager =

Injected RequestManager instance.

securityManager: SecurityManager =

Injected SecurityManager instance.

actor: BackgroundWorker | None =

The actor for running the synchronous WebSocket server in the background.

associatedConnections: dict[str, uuid.UUID] =

The list of currently handled WebSocket connections, associated with an originator.

connectionUsedCounter: dict[uuid.UUID, ThreadSafeCounter] =

A counter for each opened WS connection opened by the CSE.

isPaused: bool =

Flag whether the server is currently paused. Requests are not handled when the server is paused.

operationEvents: dict[Operation, tuple[Event, str]] =

Events for the different operations.

websocketServer: WSServer | None =

The WebSocket server object.

wsConnections: dict[uuid.UUID, WSConnection] =

The list of currently handled WebSocket connections.

def _checkIsServerRunning(self, websocket: WSConnection) -> bool:

Check if the server is running. If not, send an error message to the client.

Returns
boolTrue if the server is running, False otherwise.
@onEvent(eventManager.configUpdate)
def _configUpdate(self, eventData: EventData):

Callback for the configUpdate event.

Parameters
eventData:EventDataThe event data containing the name of the updated configuration setting and its new value.
def _getWSSendingTargetName(self, target: str) -> str:

Get the target name for sending a WebSocket request.

Parameters
target:strThe target to send the request to.
Returns
strThe target for sending the request.
def _handleAuthentication(self, websocket: WSConnection) -> AuthorizationResult:

Handle the authentication of a new connection.

Parameters
websocket:WSConnectionThe WebSocket connection.
Returns
AuthorizationResultEneumeration value of the result of the authentication.
@onEvent(eventManager.deleteResource)
def _handleDeleteEvent(self, eventData: EventData):

Callback and handler for the deleteResource event.

In case of an AE deletion, the associated WS connection is dissociated, but left open.

Parameters
eventData:EventDataThe event data containing the deleted resource.
def _handleReceivedMessage(self, websocket: WSConnection, message: str | bytes, wsOriginator: str, contentType: ContentSerializationType, authResult: AuthorizationResult):

Handle a received message. This is the main entry point for handling a received message, whether the message is a request or a response.

Parameters
websocket:WSConnectionThe WebSocket connection.
message:str | bytesThe received message.
wsOriginator:strThe originator of the connection.
contentType:ContentSerializationTypeThe content type.
authResult:AuthorizationResultThe result of the request authentication.
def _run(self):

WebSocket server main loop.

def _stop(self):

Stop the WebSocket server.