Revision 2c7c650b4a5f752eb4ebd62b74ecd39cf13ad599 (click the page title to view the current version)
Changes from 2c7c650b4a5f752eb4ebd62b74ecd39cf13ad599 to 9b0d959aea55e085083652864435023cc39f0785---
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.
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.
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.
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
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
```
```{.clafer .ide}
```
## 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 `-->`.
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*.
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.
<table>
<tr>
<td valign="top">
```
webSocketOpenedClosed
states
ready
```
</td>
<td valign="top">
```
webSocketOpenedClosed
request -> open
success
states
ready
```
</td>
<td valign="top">
```
webSocketOpenedClosed
states
opened
```
</td>
<td valign="top">
```
webSocketOpenedClosed
request -> close
success
states
opened
```
</td>
<td valign="top">
```
webSocketOpenedClosed
states
closed
```
</td>
</table>
### 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
<table>
<tr>
<td valign="top">
```
webSocketOpenedFailed
states
ready
```
</td>
<td valign="top">
```
webSocketOpenedFailed
request -> open
success
states
ready
```
</td>
<td valign="top">
```
webSocketOpenedFailed
states
opened
```
</td>
<td valign="top">
```
webSocketOpenedFailed
request -> close
failure
states
opened
```
</td>
<td valign="top">
```
webSocketOpenedFailed
states
failed
```
</td>
</table>
### Web socket failed to be opened
```
webSocketFailed : WebSocket
[ initially (ready --> request=open && request.failure --> failed --> failed) ]
```
#### Trace 3
<table>
<tr>
<td valign="top">
```
webSocketFailed
states
ready
```
</td>
<td valign="top">
```
webSocketFailed
request -> open
failure
states
ready
```
</td>
<td valign="top">
```
webSocketFailed
states
failed
```
</td>
</table>