summaryrefslogtreecommitdiffstats
path: root/src/input/zero.lua
blob: 1907dce5a5c60dd0a2ef8f04cb6b340498a135b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- Copyright (c) 2018-2021, OARC, Inc.
-- All rights reserved.
--
-- This file is part of dnsjit.
--
-- dnsjit is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- dnsjit is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with dnsjit.  If not, see <http://www.gnu.org/licenses/>.

-- dnsjit.input.zero
-- Generate empty objects (/dev/zero)
--   local input = require("dnsjit.input.zero").new()
--   input:receiver(filter_or_output)
--   input:run(1e6)
--
-- Input module for generating empty
-- .I core.object.null
-- objects, mostly used for testing.
module(...,package.seeall)

require("dnsjit.input.zero_h")
local ffi = require("ffi")
local C = ffi.C

local t_name = "input_zero_t"
local input_zero_t = ffi.typeof(t_name)
local Zero = {}

-- Create a new Zero input.
function Zero.new()
    local self = {
        _receiver = nil,
        obj = input_zero_t(),
    }
    C.input_zero_init(self.obj)
    ffi.gc(self.obj, C.input_zero_destroy)
    return setmetatable(self, { __index = Zero })
end

-- Return the Log object to control logging of this instance or module.
function Zero:log()
    if self == nil then
        return C.input_zero_log()
    end
    return self.obj._log
end

-- Set the receiver to pass objects to.
function Zero:receiver(o)
    self.obj.recv, self.obj.ctx = o:receive()
    self._receiver = o
end

-- Return the C functions and context for producing objects.
function Zero:produce()
    return C.input_zero_producer(), self.obj
end

-- Generate
-- .I num
-- empty objects and send them to the receiver.
function Zero:run(num)
    C.input_zero_run(self.obj, num)
end

return Zero