]>
Commit | Line | Data |
---|---|---|
a6f639f8 | 1 | # Copyright (c) 2009, 2010, 2012 Nicira, Inc. |
26bb0f31 | 2 | # |
99155935 BP |
3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
4 | # you may not use this file except in compliance with the License. | |
5 | # You may obtain a copy of the License at: | |
26bb0f31 | 6 | # |
99155935 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
26bb0f31 | 8 | # |
99155935 BP |
9 | # Unless required by applicable law or agreed to in writing, software |
10 | # distributed under the License is distributed on an "AS IS" BASIS, | |
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | # See the License for the specific language governing permissions and | |
13 | # limitations under the License. | |
14 | ||
8ea171ab RB |
15 | from __future__ import print_function |
16 | ||
99155935 | 17 | import errno |
99155935 BP |
18 | import sys |
19 | ||
20 | import ovs.reconnect | |
21 | ||
26bb0f31 EJ |
22 | now = 0 |
23 | r = None | |
24 | ||
25 | ||
26 | def do_enable(_): | |
99155935 BP |
27 | r.enable(now) |
28 | ||
26bb0f31 EJ |
29 | |
30 | def do_disable(_): | |
99155935 BP |
31 | r.disable(now) |
32 | ||
26bb0f31 EJ |
33 | |
34 | def do_force_reconnect(_): | |
99155935 BP |
35 | r.force_reconnect(now) |
36 | ||
26bb0f31 | 37 | |
99155935 BP |
38 | def error_from_string(s): |
39 | if not s: | |
40 | return 0 | |
41 | elif s == "ECONNREFUSED": | |
42 | return errno.ECONNREFUSED | |
43 | elif s == "EOF": | |
b17a80ee | 44 | return ovs.reconnect.EOF |
99155935 BP |
45 | else: |
46 | sys.stderr.write("unknown error '%s'\n" % s) | |
47 | sys.exit(1) | |
48 | ||
26bb0f31 | 49 | |
99155935 BP |
50 | def do_disconnected(arg): |
51 | r.disconnected(now, error_from_string(arg)) | |
26bb0f31 EJ |
52 | |
53 | ||
54 | def do_connecting(_): | |
99155935 BP |
55 | r.connecting(now) |
56 | ||
26bb0f31 | 57 | |
99155935 BP |
58 | def do_connect_failed(arg): |
59 | r.connect_failed(now, error_from_string(arg)) | |
60 | ||
26bb0f31 EJ |
61 | |
62 | def do_connected(_): | |
99155935 BP |
63 | r.connected(now) |
64 | ||
26bb0f31 | 65 | |
a6f639f8 BP |
66 | def do_activity(_): |
67 | r.activity(now) | |
99155935 | 68 | |
26bb0f31 | 69 | |
99155935 BP |
70 | def do_run(arg): |
71 | global now | |
72 | if arg is not None: | |
73 | now += int(arg) | |
74 | ||
75 | action = r.run(now) | |
76 | if action is None: | |
77 | pass | |
78 | elif action == ovs.reconnect.CONNECT: | |
8ea171ab | 79 | print(" should connect") |
99155935 | 80 | elif action == ovs.reconnect.DISCONNECT: |
8ea171ab | 81 | print(" should disconnect") |
99155935 | 82 | elif action == ovs.reconnect.PROBE: |
8ea171ab | 83 | print(" should send probe") |
99155935 BP |
84 | else: |
85 | assert False | |
86 | ||
26bb0f31 | 87 | |
99155935 BP |
88 | def do_advance(arg): |
89 | global now | |
90 | now += int(arg) | |
91 | ||
26bb0f31 EJ |
92 | |
93 | def do_timeout(_): | |
99155935 BP |
94 | global now |
95 | timeout = r.timeout(now) | |
c03afda6 | 96 | if timeout is not None and timeout >= 0: |
8ea171ab | 97 | print(" advance %d ms" % timeout) |
99155935 BP |
98 | now += timeout |
99 | else: | |
8ea171ab | 100 | print(" no timeout") |
99155935 | 101 | |
26bb0f31 | 102 | |
99155935 BP |
103 | def do_set_max_tries(arg): |
104 | r.set_max_tries(int(arg)) | |
105 | ||
26bb0f31 | 106 | |
5eda645e | 107 | def diff_stats(old, new, delta): |
99155935 BP |
108 | if (old.state != new.state or |
109 | old.state_elapsed != new.state_elapsed or | |
110 | old.backoff != new.backoff): | |
111 | print(" in %s for %d ms (%d ms backoff)" | |
112 | % (new.state, new.state_elapsed, new.backoff)) | |
113 | ||
114 | if (old.creation_time != new.creation_time or | |
a6f639f8 | 115 | old.last_activity != new.last_activity or |
99155935 | 116 | old.last_connected != new.last_connected): |
a6f639f8 BP |
117 | print(" created %d, last activity %d, last connected %d" |
118 | % (new.creation_time, new.last_activity, new.last_connected)) | |
99155935 BP |
119 | |
120 | if (old.n_successful_connections != new.n_successful_connections or | |
121 | old.n_attempted_connections != new.n_attempted_connections or | |
122 | old.seqno != new.seqno): | |
123 | print(" %d successful connections out of %d attempts, seqno %d" | |
124 | % (new.n_successful_connections, new.n_attempted_connections, | |
125 | new.seqno)) | |
126 | ||
5eda645e | 127 | if (old.is_connected != new.is_connected): |
99155935 BP |
128 | if new.is_connected: |
129 | negate = "" | |
130 | else: | |
5eda645e AE |
131 | negate = "dis" |
132 | print(" %sconnected" % negate) | |
133 | ||
134 | if (old.last_connected != new.last_connected or | |
3c057118 | 135 | (new.msec_since_connect is not None and |
5eda645e | 136 | old.msec_since_connect != new.msec_since_connect - delta) or |
26bb0f31 EJ |
137 | (old.total_connected_duration != new.total_connected_duration - delta |
138 | and not (old.total_connected_duration == 0 and | |
139 | new.total_connected_duration == 0))): | |
5eda645e AE |
140 | print(" last connected %d ms ago, connected %d ms total" |
141 | % (new.msec_since_connect, new.total_connected_duration)) | |
142 | ||
143 | if (old.last_disconnected != new.last_disconnected or | |
3c057118 | 144 | (new.msec_since_disconnect is not None and |
5eda645e | 145 | old.msec_since_disconnect != new.msec_since_disconnect - delta)): |
eba18f00 | 146 | print(" disconnected at %d ms (%d ms ago)" |
5eda645e | 147 | % (new.last_disconnected, new.msec_since_disconnect)) |
eba18f00 | 148 | |
26bb0f31 EJ |
149 | |
150 | def do_set_passive(_): | |
99155935 BP |
151 | r.set_passive(True, now) |
152 | ||
26bb0f31 EJ |
153 | |
154 | def do_listening(_): | |
99155935 BP |
155 | r.listening(now) |
156 | ||
26bb0f31 | 157 | |
99155935 BP |
158 | def do_listen_error(arg): |
159 | r.listen_error(now, int(arg)) | |
160 | ||
26bb0f31 | 161 | |
99155935 BP |
162 | def main(): |
163 | commands = { | |
164 | "enable": do_enable, | |
165 | "disable": do_disable, | |
166 | "force-reconnect": do_force_reconnect, | |
167 | "disconnected": do_disconnected, | |
168 | "connecting": do_connecting, | |
169 | "connect-failed": do_connect_failed, | |
170 | "connected": do_connected, | |
a6f639f8 | 171 | "activity": do_activity, |
99155935 BP |
172 | "run": do_run, |
173 | "advance": do_advance, | |
174 | "timeout": do_timeout, | |
175 | "set-max-tries": do_set_max_tries, | |
176 | "passive": do_set_passive, | |
177 | "listening": do_listening, | |
178 | "listen-error": do_listen_error | |
179 | } | |
180 | ||
99155935 BP |
181 | global now |
182 | global r | |
183 | ||
184 | now = 1000 | |
185 | r = ovs.reconnect.Reconnect(now) | |
186 | r.set_name("remote") | |
187 | prev = r.get_stats(now) | |
8ea171ab | 188 | print("### t=%d ###" % now) |
99155935 BP |
189 | old_time = now |
190 | old_max_tries = r.get_max_tries() | |
191 | while True: | |
192 | line = sys.stdin.readline() | |
193 | if line == "": | |
194 | break | |
195 | ||
8ea171ab | 196 | print(line[:-1]) |
99155935 BP |
197 | if line[0] == "#": |
198 | continue | |
199 | ||
200 | args = line.split() | |
201 | if len(args) == 0: | |
202 | continue | |
203 | ||
204 | command = args[0] | |
205 | if len(args) > 1: | |
206 | op = args[1] | |
207 | else: | |
208 | op = None | |
209 | commands[command](op) | |
210 | ||
211 | if old_time != now: | |
8ea171ab RB |
212 | print() |
213 | print("### t=%d ###" % now) | |
99155935 BP |
214 | |
215 | cur = r.get_stats(now) | |
5eda645e | 216 | diff_stats(prev, cur, now - old_time) |
99155935 BP |
217 | prev = cur |
218 | if r.get_max_tries() != old_max_tries: | |
219 | old_max_tries = r.get_max_tries() | |
8ea171ab | 220 | print(" %d tries left" % old_max_tries) |
99155935 | 221 | |
5eda645e AE |
222 | old_time = now |
223 | ||
26bb0f31 | 224 | |
99155935 BP |
225 | if __name__ == '__main__': |
226 | main() |