-
Notifications
You must be signed in to change notification settings - Fork 1
/
skv.sm
134 lines (110 loc) · 3.07 KB
/
skv.sm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Author: Markus Stenberg <[email protected]>
//
// Copyright (c) 2012 cisco Systems, Inc.
//
%{
require 'skv_const'
%}
%start Client::Init
%class SKV
%map Client
%%
// State Transition End State Action(s)
Init
Entry { init_client(skv_const.INITIAL_LISTEN_TIMEOUT); }
{
Initialized [ctxt:is_server() ] Server::Init { }
Initialized Connecting { }
}
Default
{
HaveUpdate(k: Key, v:Value) nil { store_local_update(k, v); }
// RemoveListener(l: Listener) nil { delete_listener(l); }
}
Connecting
Entry { start_retry_timer(skv_const.CONNECT_TIMEOUT); socket_connect(); }
Exit { clear_timeout_maybe(); }
{
Connected WaitVersion { }
// XXX - should the failure behavior be same for failure + timeout?
ConnectFailed [ ctxt:is_long_lived() ] Server::Init { clear_socket_maybe(); }
ConnectFailed [ ctxt:should_auto_retry() ] ClientFailRetry { clear_socket_maybe(); }
ConnectFailed Terminal::ClientFailConnect { clear_socket_maybe(); }
Timeout [ ctxt:is_long_lived() ] Server::Init { clear_socket_maybe(); }
Timeout [ ctxt:should_auto_retry() ] ClientFailRetry { clear_socket_maybe(); }
Timeout Terminal::ClientFailConnect { clear_socket_maybe(); }
}
WaitVersion
Entry { wrap_socket_json(); }
Exit { }
{
ReceiveVersion(v: Value) [ctxt:protocol_is_current_version(v)] WaitUpdates {}
ReceiveVersion(v: Value) Terminal::ClientFailConnect {}
ConnectionClosed Connecting { clear_json(); }
// receiving of updates handled here; however, it doesn't affect system state machine
}
WaitUpdates
Entry { send_local_state();
//send_listeners();
}
Exit { clear_json(); }
{
HaveUpdate(k: Key, v:Value) nil { store_local_update(k, v); send_update_kv(k, v); }
// AddListener(k: String, cb:Callback) nil { l = add_listener(k, cb); send_add_listener(l); }
// RemoveListener(l: Listener) nil { send_remove_listener(l); delete_listener(l); }
ConnectionClosed Connecting { }
ReceiveUpdate(json:Json, k: Key, v: Value) nil { client_remote_update(json, k, v); }
}
ClientFailRetry
Entry { increase_retry_timer(); start_retry_timer(); }
Exit { clear_timeout(); }
{
Timeout Connecting { }
}
%%
%map Server
%%
Default
{
HaveUpdate(k: Key, v:Value) nil { store_local_update(k, v); }
// AddListener(k: String, cb: Callback) nil { add_listener(k, cb); }
// RemoveListener(l: Listener) nil { delete_listener(l); }
}
Init
Entry { init_server(); }
{
Initialized Binding {}
}
Binding
Entry { bind(); }
{
Bound WaitConnections { }
BindFailed [ctxt:is_server()] Terminal::ServerBindFailed { }
BindFailed InitWait { }
}
InitWait
Entry { increase_retry_timer(); start_retry_timer(); }
Exit { clear_timeout(); }
{
Timeout Client::Connecting { }
}
WaitConnections
Entry { }
{
// new connection loop here - no affect on the state machine though
HaveUpdate(k: Key, v:Value) nil { store_local_update(k, v); send_update_to_clients(k, v); }
ReceiveUpdate(json:JsonCodec, k: Key, v: Value) nil { server_remote_update(json, k, v); }
}
%%
%map Terminal
%%
ClientFailConnect
Entry { fail("unable to connect to SKV"); }
{
}
ServerBindFailed
Entry { fail("unable to listen on selected port"); }
{
}
%%