| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 r"""Tool for viewing masters, their hosts and their ports. | 6 r"""Tool for viewing masters, their hosts and their ports. |
| 7 | 7 |
| 8 Has three modes: | 8 Has three modes: |
| 9 a) In normal mode, simply prints the list of all known masters, sorted by | 9 a) In normal mode, simply prints the list of all known masters, sorted by |
| 10 hostname, along with their associated ports, for the perusal of the user. | 10 hostname, along with their associated ports, for the perusal of the user. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 41795, # Crestron Control Port | 62 41795, # Crestron Control Port |
| 63 45824, # Server for the DAI family of client-server products | 63 45824, # Server for the DAI family of client-server products |
| 64 47001, # WinRM | 64 47001, # WinRM |
| 65 47808, # BACnet Building Automation and Control Networks | 65 47808, # BACnet Building Automation and Control Networks |
| 66 48653, # Robot Raconteur transport | 66 48653, # Robot Raconteur transport |
| 67 49151, # Reserved | 67 49151, # Reserved |
| 68 # There are no reserved ports in the 50000-65535 range. | 68 # There are no reserved ports in the 50000-65535 range. |
| 69 ]) | 69 ]) |
| 70 | 70 |
| 71 | 71 |
| 72 PORT_TYPE_MAP = { | 72 PORT_RANGE_MAP = { |
| 73 'port': Master.Base.MASTER_PORT, | 73 'port': Master.Base.MASTER_PORT_RANGE, |
| 74 'slave_port': Master.Base.SLAVE_PORT, | 74 'slave_port': Master.Base.SLAVE_PORT_RANGE, |
| 75 'alt_port': Master.Base.MASTER_PORT_ALT, | 75 'alt_port': Master.Base.MASTER_PORT_ALT_RANGE, |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 # A map of (full) master host to master class used in 'get_master_class' | 79 # A map of (full) master host to master class used in 'get_master_class' |
| 80 # lookup. | 80 # lookup. |
| 81 MASTER_HOST_MAP = dict((m.master_host, m) | 81 MASTER_HOST_MAP = dict((m.master_host, m) |
| 82 for m in Master.get_base_masters()) | 82 for m in Master.get_base_masters()) |
| 83 | 83 |
| 84 | 84 |
| 85 def get_args(): | 85 def get_args(): |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 Outputs lists of masters whose ports conflict and who have misconfigured | 239 Outputs lists of masters whose ports conflict and who have misconfigured |
| 240 ports. If any misconfigurations are found, returns a non-zero error code. | 240 ports. If any misconfigurations are found, returns a non-zero error code. |
| 241 """ | 241 """ |
| 242 | 242 |
| 243 # Return value. Will be set to 1 the first time we see an error. | 243 # Return value. Will be set to 1 the first time we see an error. |
| 244 ret = 0 | 244 ret = 0 |
| 245 | 245 |
| 246 # Look for masters using the wrong ports for their port types. | 246 # Look for masters using the wrong ports for their port types. |
| 247 bad_port_masters = [] | 247 bad_port_masters = [] |
| 248 for master in masters: | 248 for master in masters: |
| 249 for port_type, port_digit in PORT_TYPE_MAP.iteritems(): | 249 for port_type, port_range in PORT_RANGE_MAP.iteritems(): |
| 250 if not str(master[port_type]).startswith(str(port_digit)): | 250 if not port_range.contains(master[port_type]): |
| 251 ret = 1 | 251 ret = 1 |
| 252 bad_port_masters.append(master) | 252 bad_port_masters.append(master) |
| 253 break | 253 break |
| 254 output([ ('Masters with misconfigured ports based on port type', | 254 output([ ('Masters with misconfigured ports based on port type', |
| 255 field('name')) ], | 255 field('name')) ], |
| 256 bad_port_masters) | 256 bad_port_masters) |
| 257 | 257 |
| 258 # Look for masters using the wrong ports for their hostname. | 258 # Look for masters using the wrong ports for their hostname. |
| 259 bad_host_masters = [] | 259 bad_host_masters = [] |
| 260 for master in masters: | 260 for master in masters: |
| 261 digits = get_master_port(master) | 261 digits = get_master_port(master) |
| 262 if digits: | 262 if digits: |
| 263 for port in PORT_TYPE_MAP.iterkeys(): | 263 for port_type, port_range in PORT_RANGE_MAP.iteritems(): |
| 264 if str(master[port])[1:3] != digits: | 264 if ('%04d' % port_range.offset_of(master[port_type]))[0:2] != digits: |
| 265 ret = 1 | 265 ret = 1 |
| 266 bad_host_masters.append(master) | 266 bad_host_masters.append(master) |
| 267 break | 267 break |
| 268 output([ ('Masters with misconfigured ports based on hostname', | 268 output([ ('Masters with misconfigured ports based on hostname', |
| 269 field('name')) ], | 269 field('name')) ], |
| 270 bad_host_masters) | 270 bad_host_masters) |
| 271 | 271 |
| 272 # Look for masters configured to use the same ports. | 272 # Look for masters configured to use the same ports. |
| 273 web_ports = {} | 273 web_ports = {} |
| 274 slave_ports = {} | 274 slave_ports = {} |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 if len(lst) > 1: | 327 if len(lst) > 1: |
| 328 ret = 1 | 328 ret = 1 |
| 329 for m in lst: | 329 for m in lst: |
| 330 conflicting_alt_ports.append( | 330 conflicting_alt_ports.append( |
| 331 { 'alt_port': port, 'name': m['name'], 'host': m['host'] }) | 331 { 'alt_port': port, 'name': m['name'], 'host': m['host'] }) |
| 332 output([ ('Alt port', field('alt_port')), | 332 output([ ('Alt port', field('alt_port')), |
| 333 ('Master', field('name')), | 333 ('Master', field('name')), |
| 334 ('Host', field('host')) ], | 334 ('Host', field('host')) ], |
| 335 conflicting_alt_ports) | 335 conflicting_alt_ports) |
| 336 | 336 |
| 337 # Look for masters whose port, slave_port, alt_port aren't separated by 10000. | 337 # Look for masters whose port, slave_port, alt_port aren't separated by 5000. |
| 338 bad_sep_masters = [] | 338 bad_sep_masters = [] |
| 339 for master in masters: | 339 for master in masters: |
| 340 if (getint(master['slave_port']) - getint(master['port']) != 10000 or | 340 if (getint(master['slave_port']) - getint(master['alt_port']) != 5000 or |
| 341 getint(master['alt_port']) - getint(master['slave_port']) != 10000): | 341 getint(master['alt_port']) - getint(master['port']) != 5000): |
| 342 ret = 1 | 342 ret = 1 |
| 343 bad_sep_masters.append(master) | 343 bad_sep_masters.append(master) |
| 344 output([ ('Master', field('name')), | 344 output([ ('Master', field('name')), |
| 345 ('Host', field('host')), | 345 ('Host', field('host')), |
| 346 ('Web port', field('port')), | 346 ('Web port', field('port')), |
| 347 ('Slave port', field('slave_port')), | 347 ('Slave port', field('slave_port')), |
| 348 ('Alt port', field('alt_port')) ], | 348 ('Alt port', field('alt_port')) ], |
| 349 bad_sep_masters) | 349 bad_sep_masters) |
| 350 | 350 |
| 351 return ret | 351 return ret |
| 352 | 352 |
| 353 | 353 |
| 354 def build_port_str(master_class, port_type, digits): | 354 def build_port_str(master_class, port_type, digits): |
| 355 port_base = PORT_TYPE_MAP[port_type] | 355 port_range = PORT_RANGE_MAP[port_type] |
| 356 port = '%d%02d%02d' % (port_base, master_class.master_port_base, digits) | 356 port = str(port_range.compose_port( |
| 357 master_class.master_port_base * 100 + digits)) |
| 357 assert len(port) == 5, "Invalid port generated (%s)" % (port,) | 358 assert len(port) == 5, "Invalid port generated (%s)" % (port,) |
| 358 return port | 359 return port |
| 359 | 360 |
| 360 | 361 |
| 361 def find_port(master_class_name, masters, output, opts): | 362 def find_port(master_class_name, masters, output, opts): |
| 362 """Finds a triplet of free ports appropriate for the given master.""" | 363 """Finds a triplet of free ports appropriate for the given master.""" |
| 363 try: | 364 try: |
| 364 master_class = getattr(Master, master_class_name) | 365 master_class = getattr(Master, master_class_name) |
| 365 except AttributeError: | 366 except AttributeError: |
| 366 raise ValueError('Master class %s does not exist' % master_class_name) | 367 raise ValueError('Master class %s does not exist' % master_class_name) |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 | 478 |
| 478 return ret | 479 return ret |
| 479 | 480 |
| 480 | 481 |
| 481 def main(): | 482 def main(): |
| 482 return real_main(include_internal=False) | 483 return real_main(include_internal=False) |
| 483 | 484 |
| 484 | 485 |
| 485 if __name__ == '__main__': | 486 if __name__ == '__main__': |
| 486 sys.exit(main()) | 487 sys.exit(main()) |
| OLD | NEW |