灵符lingfu

LingSys

↓ 0 downloads

Your machine's memory, GPU, motherboard and network, drawn and played in the Ling design system: LingSys, a small build for USB liquid-cooler displays, LingNet for IPv4/IPv6, DNS and packets, and a LingOS edition. 64-bit and 32-bit Windows builds included.

Keywords system-monitor, visualization, network, ipv6, gpu, lcd, lingos, ling
lingfu install LingSys
LicenseMIT
AuthorsLing Lin <taellinglin@gmail.com>
READMECode / FilesVersionsDependency GraphDependentsSecurityBuildsSettings

Code / Files (48)

src/97_netmain.ling
# ───────────────────────────── LINGNET main ──────────────────────────────────
#   ling run --interp hatchnet.ling [WxH] [full] [sim] [shot]
# Keys: TAB / LEFT / RIGHT view, 1-6 view, UP / DOWN device (ALL, then each adapter), H hud, Q or ESC quit.
# The bar along the bottom is clickable: views on the left, devices on the right. On DEVICES, click a card.

# rects of the six view tabs
fn nbar_tabs(W, FH) {
    bind out = []
    bind x = 18.0
    bind j = 0
    while j < 6 {
        bind w = text_w(NET_VIEWS[j], 12.0) + 28.0
        bind out = list_push(out, [x, FH - 62.0, w, 26.0])
        bind x = x + w + 8.0
        bind j = j + 1
    }
    return out
}

# rects of the device chips: ALL first, then each adapter
fn nbar_chips(labels, W, FH) {
    bind tabs = nbar_tabs(W, FH)
    bind last = tabs[5]
    bind x = last[0] + last[2] + 26.0
    bind out = []
    bind i = 0
    while i < len(labels) {
        bind w = text_w(labels[i], 11.0) + 22.0
        bind out = list_push(out, [x, FH - 60.0, w, 22.0])
        bind x = x + w + 6.0
        bind i = i + 1
    }
    return out
}

# 0..5 a view tab, 10 + k device chip k (0 = ALL), -1 nothing
fn nbar_hit(mx, my, W, FH, labels) {
    bind tabs = nbar_tabs(W, FH)
    bind j = 0
    while j < 6 {
        if inr(mx, my, tabs[j]) == 1 { return j }
        bind j = j + 1
    }
    bind chips = nbar_chips(labels, W, FH)
    bind k = 0
    while k < len(chips) {
        if inr(mx, my, chips[k]) == 1 { return 10 + k }
        bind k = k + 1
    }
    return 0 - 1
}

fn net_bar(Z, view, sel, labels) {
    bind W = Z[Z_W]
    bind FH = Z[Z_FH]
    bind sat = Z[Z_SAT]
    bind hs = Z[Z_HS]
    bind mx = Z[Z_MX]
    bind my = Z[Z_MY]
    bind tabs = nbar_tabs(W, FH)
    bind j = 0
    while j < 6 {
        bind r = tabs[j]
        bind hov = inr(mx, my, r)
        bind on = 0
        if j == view { bind on = 1 }
        bind fl = 30.0 + 18.0 * hov
        if on == 1 { bind fl = 82.0 }
        col(6, sat * 0.8, fl, hs)
        brect(r[0], r[1], 1.0, 0.0, 0.0, r[2], r[3])
        if on == 1 {
            col(6, sat * 0.8, 26.0, hs)
            bhatch(r[0], r[1], 1.0, 2.0, 2.0, r[2] - 2.0, r[3] - 2.0, 5.0)
        }
        bind tl = 54.0 + 24.0 * hov
        if on == 1 { bind tl = 94.0 }
        col(6, sat, tl, hs)
        daemon_text(NET_VIEWS[j], r[0] + 14.0, r[1] + r[3] / 2.0, 12.0)
        bind j = j + 1
    }
    bind chips = nbar_chips(labels, W, FH)
    bind k = 0
    while k < len(chips) {
        bind r = chips[k]
        bind hov = inr(mx, my, r)
        bind hue = 2
        if k > 0 { bind hue = FLEET_HUE[(k - 1) % 8] }
        bind on = 0
        if k - 1 == sel { bind on = 1 }
        bind fl = 30.0 + 18.0 * hov
        if on == 1 { bind fl = 82.0 }
        col(hue, sat * 0.8, fl, hs)
        brect(r[0], r[1], 1.0, 0.0, 0.0, r[2], r[3])
        if on == 1 {
            col(hue, sat * 0.8, 26.0, hs)
            bhatch(r[0], r[1], 1.0, 2.0, 2.0, r[2] - 2.0, r[3] - 2.0, 5.0)
        }
        bind tl = 52.0 + 24.0 * hov
        if on == 1 { bind tl = 94.0 }
        col(hue, sat, tl, hs)
        daemon_text(labels[k], r[0] + 11.0, r[1] + r[3] / 2.0, 11.0)
        bind k = k + 1
    }
}

fn net_hud(Z, view, sel, nx, live, hudOn) {
    bind W = Z[Z_W]
    bind FH = Z[Z_FH]
    bind sat = Z[Z_SAT]
    bind hs = Z[Z_HS]
    col(2, sat, 72.0, hs)
    daemon_text("LINGNET", 18.0, 26.0, 20.0)
    bind mode = "SIM"
    if live == 1 { bind mode = "LIVE" }
    if live == 2 { bind mode = "STALE" }
    col(6, sat, 66.0, hs)
    daemon_text(NET_VIEWS[view] + "  " + dev_name(nx, sel) + "  " + mode, 18.0, 52.0, 13.0)
    if hudOn == 1 {
        ink(52.0)
        daemon_text(Z[Z_STAT], 18.0, FH - 18.0, 13.0)
        bind keyhint = "TAB VIEW  UP/DN DEVICE  H HUD  ESC"
        ink(40.0)
        daemon_text(keyhint, W - text_w(keyhint, 10.0) - 18.0, FH - 18.0, 10.0)
    }
}

bind NET_SHOT = [0, 1, 2, 3, 4, 5]

bind start = do {
    bind args = get_args()
    bind full = 0
    bind forceSim = 0
    bind shot = 0
    bind W = 1280
    bind H = 720
    bind ai = 0
    while ai < len(args) {
        bind a = args[ai]
        if a == "full" { bind full = 1 }
        if a == "sim" { bind forceSim = 1 }
        if a == "shot" { bind shot = 1 }
        bind pr = split(a, "x")
        if len(pr) == 2 {
            if to_number(pr[0]) > 0 {
                if to_number(pr[1]) > 0 {
                    bind W = floor(to_number(pr[0]))
                    bind H = floor(to_number(pr[1]))
                }
            }
        }
        bind ai = ai + 1
    }
    if full == 1 { open_fullscreen("LingNet") } else { open_window(W, H, "LingNet") }
    bind W = get_width()
    bind FH = get_height()
    bind Hc = FH - 66.0
    set_blend(1)
    set_frame_blur(0.45)

    bind t0 = time_now()
    bind tprev = 0.0
    bind lastRead = 0.0 - 10.0
    bind lastHs = 0.0
    bind view = 0
    bind sel = 0 - 1
    bind showHud = 1
    bind prevDown = 0
    bind shotIdx = 0
    bind shotN = 0
    bind live = 0
    bind tsN = 0.0
    bind chgN = 0.0
    bind tgt = sim_netx(0.0)
    bind vecsS = tgt[NX_VEC]
    bind protoS = tgt[NX_PR]
    bind hn = hn_new()
    bind energy = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    bind ev = []
    bind hist = []
    bind rot = 0.0
    bind hueShift = 0.0
    bind act = 0.0
    bind rdA = 0.0
    bind wrA = 0.0

    while window_is_open() {
        bind t = time_now() - t0
        bind dt = clamp(t - tprev, 0.001, 0.1)
        bind tprev = t
        bind n = len(tgt[NX_AD])

        # ---- input
        bind mx = mouse_x()
        bind my = mouse_y()
        bind click = 0
        if mouse_down() {
            if prevDown == 0 { bind click = 1 }
            bind prevDown = 1
        } else {
            bind prevDown = 0
        }
        if key_pressed("escape") { close_window() }
        if key_pressed("q") { close_window() }
        if key_pressed("tab") { bind view = (view + 1) % 6 }
        if key_pressed("right") { bind view = (view + 1) % 6 }
        if key_pressed("left") { bind view = (view + 5) % 6 }
        if key_pressed("1") { bind view = 0 }
        if key_pressed("2") { bind view = 1 }
        if key_pressed("3") { bind view = 2 }
        if key_pressed("4") { bind view = 3 }
        if key_pressed("5") { bind view = 4 }
        if key_pressed("6") { bind view = 5 }
        if key_pressed("down") {
            bind sel = sel + 1
            if sel >= n { bind sel = 0 - 1 }
        }
        if key_pressed("up") {
            bind sel = sel - 1
            if sel < 0 - 1 { bind sel = n - 1 }
        }
        if key_pressed("h") { bind showHud = 1 - showHud }
        bind labels = ["ALL"]
        bind li = 0
        while li < n {
            bind labels = list_push(labels, str_slice(tgt[NX_AD][li][1], 0, 10))
            bind li = li + 1
        }
        if click == 1 {
            bind bh = nbar_hit(mx, my, W, FH, labels)
            if bh >= 10 { bind sel = bh - 11 }
            if bh >= 0 {
                if bh < 6 { bind view = bh }
            }
            if bh < 0 {
                if view == 0 {
                    bind dh = dev_hit(mx, my, W, Hc, min(n, 6))
                    if dh >= 0 { bind sel = dh }
                }
            }
        }
        if shot == 1 {
            bind view = NET_SHOT[shotIdx]
            bind shotN = shotN + 1
            bind need = 200
            if shotIdx == 0 { bind need = 500 }
            if shotN == need {
                screenshot("hatchnet_" + NET_VIEWS[view])
                bind shotIdx = shotIdx + 1
                bind shotN = 0
                if shotIdx >= len(NET_SHOT) {
                    bind shotIdx = len(NET_SHOT) - 1
                    close_window()
                }
            }
        }

        # ---- data, about every 0.6 s
        if t - lastRead > 0.6 {
            bind lastRead = t
            bind live = 0
            if forceSim == 0 {
                if file_exists("netx.snap") {
                    bind nn = parse_netx(read_file("netx.snap"))
                    if nn[0] == 1 {
                        bind tgt = nn
                        bind live = 1
                        if nn[NX_T] != tsN {
                            bind tsN = nn[NX_T]
                            bind chgN = t
                        }
                        if t - chgN > 20.0 { bind live = 2 }
                    }
                }
            }
            if live == 0 { bind tgt = sim_netx(t) }
        }
        bind k = clamp(dt * 2.5, 0.0, 1.0)
        bind vecsS = glide_ll(vecsS, tgt[NX_VEC], k)
        bind protoS = glide(protoS, tgt[NX_PR], k)
        bind nx = [1, tgt[NX_AD], tgt[NX_M], tgt[NX_ADDR], tgt[NX_GW], tgt[NX_DNS], tgt[NX_NB], protoS, tgt[NX_CN], tgt[NX_K], tgt[NX_DC], tgt[NX_PQ], 0.0, vecsS]
        bind n = len(tgt[NX_AD])
        if sel >= n { bind sel = 0 - 1 }

        # ---- the numbers behind the look
        bind v = sel_vec(nx, sel)
        bind used = lg(v[0] + v[1], 9.0)
        bind kk = clamp(dt * 2.0, 0.0, 1.0)
        bind rdA = mix(rdA, lg(v[0], 8.0), kk)
        bind wrA = mix(wrA, lg(v[1], 8.0), kk)
        bind act = (rdA + wrA) / 2.0
        bind rot = rot + dt * (0.06 + 0.9 * act)
        bind hueShift = 10.0 * sin(t * 0.05) + 26.0 * wrA - 12.0 * rdA
        bind sat = 52.0 + 40.0 * used
        bind p = protoS
        bind enT = lane_fracs(v)
        if view == 3 { bind enT = [lg(p[0], 4.5), lg(p[1], 4.5), lg(p[2], 4.5), lg(p[3], 4.5), lg(p[4], 4.0), lg(p[5], 4.0), lg(p[6] + p[7], 3.0)] }
        if view == 4 {
            bind dc = tgt[NX_DC]
            bind enT = [lg(dc[0], 2.5), lg(dc[1], 2.5), lg(dc[2], 2.5), lg(dc[3], 2.5), lg(dc[4], 2.5), lg(dc[5], 2.5), lg(dc[6], 2.5)]
        }
        bind ne = []
        bind i = 0
        while i < 7 {
            bind ne = list_push(ne, mix(energy[i], enT[i], clamp(dt * 3.0, 0.0, 1.0)))
            bind i = i + 1
        }
        bind energy = ne

        # ---- pulses: bytes in are circles, bytes out are squares; they leave the connections shown in FLOW
        bind ev = ev_cull(ev, t, 1.9)
        bind hist = ev_cull(hist, t, 14.5)
        bind nodes = sel_conns(nx, sel)
        bind nn2 = max(len(nodes), 1)
        bind rate = [14.0 * rdA, 14.0 * wrA]
        bind src = 0
        while src < 2 {
            bind want = rate[src] * dt
            bind cnt = floor(want)
            if rand() < want - cnt { bind cnt = cnt + 1 }
            bind m = 0
            while m < cnt {
                bind cat = pick([0.6, 0.0, 0.3, 0.0, 0.1, 0.0, 0.0], rand())
                if src == 1 { bind cat = pick([0.0, 0.6, 0.0, 0.3, 0.0, 0.1, 0.0], rand()) }
                bind proc = floor(rand() * nn2)
                bind ev = ev_add(ev, src, cat, t, rand(), 0.3 + 0.7 * rand(), proc)
                bind hist = ev_add(hist, src, cat, t, rand(), 0.3 + 0.7 * rand(), proc)
                bind m = m + 1
            }
            bind src = src + 1
        }
        bind h4 = []
        bind i = 0
        while i < len(hist) {
            bind h4 = list_push(h4, hist[i])
            bind h4 = list_push(h4, hist[i + 1])
            bind h4 = list_push(h4, hist[i + 2])
            bind h4 = list_push(h4, hist[i + 4])
            bind i = i + 6
        }
        if t - lastHs > 0.5 {
            bind lastHs = t
            bind hn = hn_sample(hn, nx)
        }

        # ---- the status line
        bind dms = "---"
        bind pq = tgt[NX_PQ]
        bind i = 0
        while i < len(pq) {
            if pq[i][3] > 0.5 {
                if pq[i][4] == 1 {
                    if dms == "---" { bind dms = num_str(round(pq[i][2])) + " MS" }
                }
            }
            bind i = i + 1
        }
        bind stat = "IN " + fmt_bytes(v[0]) + "/S   OUT " + fmt_bytes(v[1]) + "/S   PKTS " + fmt_rate(v[2]) + " IN " + fmt_rate(v[3]) + " OUT   V4 " + fmt_rate(p[0]) + "/S  V6 " + fmt_rate(p[1]) + "/S   DNS " + dms

        bind Dp = [nx, sel, 0, hn, 0, 0, lastHs]
        bind Z = [t, rot, hueShift, sat, W, Hc, [], 1.0, act, 0, 0, 0, ev, [], [], [], [], [], h4, used, energy, [], live, FH, Dp, stat, mx, my, 0, view, enT, [], ["", 0.0 - 100.0, 0]]
        fill(0, 0, 0)
        if view == 0 { view_net_devices(Z) }
        if view == 1 { view_net_flow2(Z) }
        if view == 2 { view_net_packets(Z) }
        if view == 3 { view_net_protocols(Z) }
        if view == 4 { view_net_dns(Z) }
        if view == 5 { view_net_addresses(Z) }
        net_bar(Z, view, sel, labels)
        net_hud(Z, view, sel, nx, live, showHud)
        present()
    }
}

← Back to index