--- title: Behavioral Modeling - WebSocket Protocol categories: behavioral ... # Simplified Web Socket Protocol > Note: this is a contrived example, not the actual protocol. ```{.clafer .links} ``` First, we'd like to express the states of the websocket and the transitions among them. When a websocket is created it starts in the state `ready`, which we model as the `initial` state. Upon entering each state initially, the websocket is not accepting any requests. Furthermore, in the states `closed` and `failed`, the websocket is not accepting any requests permanently. The request, if exists, points to either `open` or `close`. The children of the request indicate `success` or `failure` of the request. ```clafer enum Request = open | close abstract WebSocket xor states initial ready [ initially no request ] [ request=open -[request.success]-> opened ] [ request=open -[request.failure]-> failed ] opened [ initially no request ] [ request=close -[request.success]-> closed ] [ request=close -[request.failure]-> failed ] closed --> closed [ no request ] failed --> failed [ no request ] xor request -> Request ? success failure ``` ## Example scenarios The example scenarios serve as test cases, whereby we can state the intended behavior of the websocket. In each case, the constraint must be consistent with the behavior specified above; however, it can be underspecified. A chain of transitions is a sequence, read from left to right, and `initially` anchors it to the start of the trace. Without `initially`, the chain would have to hold in every state of the trace, which contradicts the behavior above. For example, the socket can remain in the state `opened` for an arbitrary number of snapshots until the request to close is received. We express that using the transition `-->>`. In the other cases, state changes must occur in the next snapshot as required by `-->`. ### Web socket successfully opened and closed ```clafer webSocketOpenedClosed : WebSocket [ initially (ready --> request=open && request.success --> opened -->> request=close && request.success --> closed --> closed) ] ``` We call such a temporal constraint a *trace specification*. #### Trace 1 Since, the trace specification and the behavior of the websocket are consistent, a trace generator can produce the following trace. After the last state, the trace loops back to that state: the websocket remains `closed` forever.
``` webSocketOpenedClosed states ready ``` ``` webSocketOpenedClosed request -> open success states ready ``` ``` webSocketOpenedClosed states opened ``` ``` webSocketOpenedClosed request -> close success states opened ``` ``` webSocketOpenedClosed states closed ```
### Web socket successfully opened but failed to be closed All the Clafer code on a page forms one model, and all its concrete clafers exist at the same time. Therefore, this scenario and the next one are not part of the model of this page. To try one, open the model in the IDE and use it to replace the scenario above. ``` webSocketOpenedFailed : WebSocket [ initially (ready --> request=open && request.success --> opened -->> request=close && request.failure --> failed --> failed) ] ``` #### Trace 2
``` webSocketOpenedFailed states ready ``` ``` webSocketOpenedFailed request -> open success states ready ``` ``` webSocketOpenedFailed states opened ``` ``` webSocketOpenedFailed request -> close failure states opened ``` ``` webSocketOpenedFailed states failed ```
### Web socket failed to be opened ``` webSocketFailed : WebSocket [ initially (ready --> request=open && request.failure --> failed --> failed) ] ``` #### Trace 3
``` webSocketFailed states ready ``` ``` webSocketFailed request -> open failure states ready ``` ``` webSocketFailed states failed ```