shithub: lua9

Download patch

ref: a14f718fd61c18085880e28865384747cefa06e2
parent: 58417631c1338bce2a8f63ffb68a462a31794995
author: telephil9 <telephil9@gmail.com>
date: Tue Oct 27 16:28:55 EDT 2020

plumbmon: sample program to demonstrate the use of the plumb module

	This sample demonstrates the use of the plumb module but also the new
	plan9 module.
	It simply goes through a list of plumb ports and run a listener in a
	subprocess, all received plumb events are then logged to the console.

--- /dev/null
+++ b/samples/plumbmon.lua
@@ -1,0 +1,23 @@
+#!/bin/ape/lua9
+
+-- lua rewrite of plumbmon by sirjofri (see http://github.com/sirjofri/plumbmon)
+
+function log_message(port, data)
+	local s = string.format("%s [%s] %s", os.date("%Y-%m-%d %H:%M"), port, data)
+	print(s)
+end
+
+function make_listener(port)
+	return function()
+			fd = plumb.open(port, plan9.OREAD)
+			while true do
+				local m = plumb.recv(fd)
+				log_message(port, m.data)
+			end
+		end
+end
+
+local ports = { "edit", "web", "image", "seemail" }
+for k,v in ipairs(ports) do
+	plan9.fork(make_listener(v))
+end