Initial commit: NetworkCity — network traffic as a neon city

A native macOS network monitor that renders live traffic as a top-down
neon city: your Mac is downtown, remote orgs are glowing districts, and
traffic is cars of light driving the roads.

Architecture:
- NetworkCityCore: swappable data layer behind a ConnectionSource protocol
  (nettop-backed today), plus pure/tested logic — traffic diffing, org
  classification (reverse-DNS + CIDR), traffic classes, hub-and-spoke
  expansion policy, label anti-overlap, and latency→distance layout.
- NetworkCityApp: SwiftUI + SpriteKit city — auto-expanding districts,
  protocol-coloured cars, click-to-inspect panel, and latency-as-distance
  (districts glide to their measured RTT).
- nettop-probe: CLI proof of the data layer.

44 tests passing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-13 08:43:32 -07:00
commit c08fc277a9
33 changed files with 2499 additions and 0 deletions
@@ -0,0 +1,40 @@
import Foundation
import CoreGraphics
/// Places district labels so their boxes don't overlap. Each district starts at
/// a hash-derived seed; if its (padded) label box collides with an already-placed
/// one, we spiral outward rotating and pushing away from downtown until we
/// find clear space. Existing placements never move, so the map stays stable as
/// new districts appear.
///
/// Pure and deterministic given its inputs; no SpriteKit, fully unit-testable.
public enum LayoutSolver {
/// - Parameters:
/// - baseAngle/baseRadius: the hash-derived seed in polar coords.
/// - localRect: the label box *relative to the node center* (labels sit
/// below the dot, so this is usually offset downward).
/// - existing: already-placed label boxes in world space.
/// - pad: minimum gap to keep between boxes.
/// - Returns: a world-space center for the new district.
public static func placeNonOverlapping(
baseAngle: Double,
baseRadius: Double,
localRect: CGRect,
existing: [CGRect],
pad: CGFloat = 16,
maxAttempts: Int = 400
) -> CGPoint {
for attempt in 0..<maxAttempts {
let angle = baseAngle + Double(attempt) * 0.45 // rotate as we go
let radius = baseRadius + Double(attempt) * 7 // and push outward
let center = CGPoint(x: cos(angle) * radius, y: sin(angle) * radius)
let box = localRect.offsetBy(dx: center.x, dy: center.y).insetBy(dx: -pad, dy: -pad)
if !existing.contains(where: { $0.intersects(box) }) {
return center
}
}
// Give up gracefully at the seed (extremely crowded map).
return CGPoint(x: cos(baseAngle) * baseRadius, y: sin(baseAngle) * baseRadius)
}
}