From 369fb56365c98855f31f57a05f425ab54c7fa603 Mon Sep 17 00:00:00 2001 From: Pete Bristow Date: Wed, 30 Mar 2016 19:55:26 +0100 Subject: [PATCH 1/2] basic truncate / sample apps --- src/apps/basic/README.src.md | 22 +++++++++++++++++++++ src/apps/basic/basic_apps.lua | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/apps/basic/README.src.md b/src/apps/basic/README.src.md index 7473b17abf..b38c5c9c74 100644 --- a/src/apps/basic/README.src.md +++ b/src/apps/basic/README.src.md @@ -37,6 +37,17 @@ from the inputs onto the output. | | +--------+ +## Sample +The `Sample` app forwards packets every `n`th packet from `input` +to `output` all others are dropped. + + DIAGRAM: Sample + +--------+ + | | + input ----* Sample *---- output + | | + +--------+ + ## Split The `Split` app splits packets from multiple inputs across multiple @@ -87,6 +98,17 @@ merge and/or duplicate packet streams | | +--------+ +## Truncate +The `Truncate` app sends all packets received on `input` to `output` +and truncates or zero pads packets to length `n` in the process + + DIAGRAM: Sample + +----------+ + | | + input ----* Truncate *---- output + | | + +----------+ + ## Repeater The `Repeater` app collects all packets received from the `input` link diff --git a/src/apps/basic/basic_apps.lua b/src/apps/basic/basic_apps.lua index ca2cb35093..b966fd3e7f 100644 --- a/src/apps/basic/basic_apps.lua +++ b/src/apps/basic/basic_apps.lua @@ -12,6 +12,43 @@ local transmit, receive = link.transmit, link.receive local ffi = require("ffi") local C = ffi.C +--- # `Truncate` app: truncate or zero pad packet to length n +Truncate = {} +function Truncate:new(n) + return setmetatable({n = n}, {__index=Truncate}) +end +function Truncate:push() + for _ = 1, link.nreadable(self.input.input) do + local p = receive(self.input.input) + if p.length > self.n then + p.length = self.n + else + for i = self.n - p.length, self.n do + p.data[i] = 0 + end + end + transmit(self.output.output,p) + end +end + +--- # `Sample` app: let through every nth packet +Sample = {} +function Sample:new(n) + return setmetatable({n = n, seen = 1}, {__index=Sample}) +end +function Sample:push() + for _ = 1, link.nreadable(self.input.input) do + local p = receive(self.input.input) + if self.n == self.seen then + transmit(self.output.output, p) + self.seen = 1 + else + self.seen = self.seen + 1 + packet.free(p) + end + end +end + --- # `Source` app: generate synthetic packets Source = {} From 41beb994b361cc0c345f0671b0f5fcd7a9c270af Mon Sep 17 00:00:00 2001 From: Pete Bristow Date: Tue, 26 Apr 2016 23:52:20 +0100 Subject: [PATCH 2/2] use ffi.fill --- src/apps/basic/basic_apps.lua | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/apps/basic/basic_apps.lua b/src/apps/basic/basic_apps.lua index b966fd3e7f..e92aae988f 100644 --- a/src/apps/basic/basic_apps.lua +++ b/src/apps/basic/basic_apps.lua @@ -6,11 +6,8 @@ local app = require("core.app") local freelist = require("core.freelist") local packet = require("core.packet") local link = require("core.link") -local transmit, receive = link.transmit, link.receive - - local ffi = require("ffi") -local C = ffi.C +local transmit, receive = link.transmit, link.receive --- # `Truncate` app: truncate or zero pad packet to length n Truncate = {} @@ -20,13 +17,8 @@ end function Truncate:push() for _ = 1, link.nreadable(self.input.input) do local p = receive(self.input.input) - if p.length > self.n then - p.length = self.n - else - for i = self.n - p.length, self.n do - p.data[i] = 0 - end - end + ffi.fill(p.data, math.min(0, self.n - p.length)) + p.length = self.n transmit(self.output.output,p) end end