feat: add fswatch, sysinfo, and udp demo scripts plus reorder manual sidebar links
Add three new example scripts demonstrating the fswatch, sysinfo, and udp modules with file watching, system metrics, and UDP echo server/client patterns. Reorder the manual API sidebar navigation to list modules alphabetically, removing deprecated entries like websocket, tls, net, json, regex, jinja, os, signal, subprocess, sqlite, and timer while adding argparse, dataset, fswatch, and html.
This commit is contained in:
Vendored
+54
@@ -0,0 +1,54 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "fswatch" for FileWatcher, FsEvent
|
||||
import "timer" for Timer
|
||||
import "io" for File, FileFlags
|
||||
|
||||
System.print("=== FSWatch Module Demo ===\n")
|
||||
|
||||
var testFile = "/tmp/fswatch_demo.txt"
|
||||
|
||||
System.print("--- Setting up test file ---")
|
||||
var f = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.create | FileFlags.truncate)
|
||||
f.writeBytes("initial content", 0)
|
||||
f.close()
|
||||
System.print("Created test file: %(testFile)")
|
||||
|
||||
System.print("\n--- Starting file watcher ---")
|
||||
var watcher = FileWatcher.new(testFile)
|
||||
var eventCount = 0
|
||||
|
||||
watcher.start { |event|
|
||||
eventCount = eventCount + 1
|
||||
System.print("Event %(eventCount):")
|
||||
System.print(" Filename: %(event.filename)")
|
||||
System.print(" Is rename: %(event.isRename)")
|
||||
System.print(" Is change: %(event.isChange)")
|
||||
}
|
||||
|
||||
System.print("Watcher active: %(watcher.isActive)")
|
||||
|
||||
System.print("\n--- Modifying file ---")
|
||||
|
||||
Timer.immediate {
|
||||
var f2 = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.truncate)
|
||||
f2.writeBytes("first modification", 0)
|
||||
f2.close()
|
||||
System.print("Made first modification")
|
||||
}
|
||||
|
||||
Timer.sleep(150)
|
||||
|
||||
Timer.immediate {
|
||||
var f3 = File.openWithFlags(testFile, FileFlags.writeOnly | FileFlags.truncate)
|
||||
f3.writeBytes("second modification", 0)
|
||||
f3.close()
|
||||
System.print("Made second modification")
|
||||
}
|
||||
|
||||
Timer.sleep(150)
|
||||
|
||||
System.print("\n--- Stopping watcher ---")
|
||||
watcher.stop()
|
||||
System.print("Watcher active: %(watcher.isActive)")
|
||||
System.print("Total events received: %(eventCount)")
|
||||
Vendored
+65
@@ -0,0 +1,65 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "sysinfo" for SysInfo
|
||||
|
||||
var formatBytes = Fn.new { |bytes|
|
||||
if (bytes < 1024) return "%(bytes) B"
|
||||
if (bytes < 1024 * 1024) return "%(((bytes / 1024) * 10).floor / 10) KB"
|
||||
if (bytes < 1024 * 1024 * 1024) return "%(((bytes / 1024 / 1024) * 10).floor / 10) MB"
|
||||
return "%(((bytes / 1024 / 1024 / 1024) * 10).floor / 10) GB"
|
||||
}
|
||||
|
||||
System.print("=== SysInfo Module Demo ===\n")
|
||||
|
||||
System.print("--- System Information ---")
|
||||
System.print("Hostname: %(SysInfo.hostname)")
|
||||
System.print("Uptime: %(SysInfo.uptime) seconds")
|
||||
|
||||
System.print("\n--- Memory Information ---")
|
||||
var total = SysInfo.totalMemory
|
||||
var free = SysInfo.freeMemory
|
||||
var used = total - free
|
||||
var resident = SysInfo.residentMemory
|
||||
var constrained = SysInfo.constrainedMemory
|
||||
|
||||
System.print("Total: %(formatBytes.call(total))")
|
||||
System.print("Free: %(formatBytes.call(free))")
|
||||
System.print("Used: %(formatBytes.call(used))")
|
||||
System.print("Process RSS: %(formatBytes.call(resident))")
|
||||
if (constrained > 0) {
|
||||
System.print("Constrained: %(formatBytes.call(constrained))")
|
||||
}
|
||||
|
||||
System.print("\n--- CPU Information ---")
|
||||
var cpus = SysInfo.cpuInfo
|
||||
System.print("CPU Count: %(cpus.count)")
|
||||
|
||||
for (i in 0...cpus.count) {
|
||||
var cpu = cpus[i]
|
||||
System.print(" CPU %(i): %(cpu["model"]) @ %(cpu["speed"]) MHz")
|
||||
}
|
||||
|
||||
System.print("\n--- Load Average ---")
|
||||
var load = SysInfo.loadAverage
|
||||
System.print("1 min: %(load[0])")
|
||||
System.print("5 min: %(load[1])")
|
||||
System.print("15 min: %(load[2])")
|
||||
|
||||
System.print("\n--- Network Interfaces ---")
|
||||
var interfaces = SysInfo.networkInterfaces
|
||||
for (name in interfaces.keys) {
|
||||
System.print("%(name):")
|
||||
for (addr in interfaces[name]) {
|
||||
System.print(" %(addr["family"]): %(addr["address"])")
|
||||
System.print(" Internal: %(addr["internal"])")
|
||||
System.print(" Netmask: %(addr["netmask"])")
|
||||
System.print(" MAC: %(addr["mac"])")
|
||||
}
|
||||
}
|
||||
|
||||
System.print("\n--- High Resolution Timer ---")
|
||||
var t1 = SysInfo.hrtime
|
||||
var t2 = SysInfo.hrtime
|
||||
System.print("Time 1: %(t1) ns")
|
||||
System.print("Time 2: %(t2) ns")
|
||||
System.print("Delta: %(t2 - t1) ns")
|
||||
Vendored
+60
@@ -0,0 +1,60 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "udp" for UdpSocket, UdpMessage
|
||||
import "timer" for Timer
|
||||
import "scheduler" for Scheduler
|
||||
|
||||
System.print("=== UDP Module Demo ===\n")
|
||||
|
||||
System.print("--- Basic UDP Echo Server ---")
|
||||
|
||||
var server = UdpSocket.new()
|
||||
server.bind("127.0.0.1", 8888)
|
||||
System.print("Server listening on %(server.localAddress):%(server.localPort)")
|
||||
|
||||
var client = UdpSocket.new()
|
||||
client.bind("127.0.0.1", 0)
|
||||
System.print("Client bound to %(client.localAddress):%(client.localPort)")
|
||||
|
||||
var messageCount = 0
|
||||
var maxMessages = 3
|
||||
|
||||
Scheduler.add {
|
||||
while (messageCount < maxMessages) {
|
||||
var msg = server.receive()
|
||||
if (msg == null) break
|
||||
|
||||
System.print("Server received: '%(msg[0])' from %(msg[1]):%(msg[2])")
|
||||
|
||||
server.send("Echo: %(msg[0])", msg[1], msg[2])
|
||||
messageCount = messageCount + 1
|
||||
}
|
||||
server.close()
|
||||
System.print("Server closed")
|
||||
}
|
||||
|
||||
Scheduler.add {
|
||||
var messages = ["Hello", "UDP", "World"]
|
||||
for (m in messages) {
|
||||
client.send(m, "127.0.0.1", 8888)
|
||||
System.print("Client sent: '%(m)'")
|
||||
|
||||
var response = client.receive()
|
||||
if (response != null) {
|
||||
System.print("Client received: '%(response[0])'")
|
||||
}
|
||||
}
|
||||
client.close()
|
||||
System.print("Client closed")
|
||||
}
|
||||
|
||||
Timer.sleep(500)
|
||||
|
||||
System.print("\n--- Broadcast Configuration ---")
|
||||
var bcast = UdpSocket.new()
|
||||
bcast.bind("0.0.0.0", 0)
|
||||
bcast.setBroadcast(true)
|
||||
System.print("Broadcast enabled on port %(bcast.localPort)")
|
||||
bcast.close()
|
||||
|
||||
System.print("\nDemo complete!")
|
||||
Reference in New Issue
Block a user