Files
timban c08fc277a9 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>
2026-06-13 08:43:32 -07:00

27 lines
927 B
Swift

import Foundation
import Darwin
/// Resolves a pid to its real executable name via `proc_pidpath`, because
/// nettop's name column is unreliable (it reported `claude` as `2.1.177`).
/// Results are cached; drive from a single thread/actor.
public final class ProcessNamer {
private var cache: [Int32: String] = [:]
public init() {}
public func name(for pid: Int32) -> String? {
guard pid > 0 else { return nil }
if let hit = cache[pid] { return hit }
var buffer = [CChar](repeating: 0, count: 4096) // PROC_PIDPATHINFO_MAXSIZE
let length = proc_pidpath(pid, &buffer, UInt32(buffer.count))
guard length > 0 else { return nil }
let path = String(cString: buffer)
// `/Applications/Google Chrome.app/.../Google Chrome Helper` -> last component
let name = (path as NSString).lastPathComponent
cache[pid] = name
return name
}
}