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