From cf7c286cfbc879121126248d21bef70dfec2e5bc Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Nov 2015 19:00:43 +0100 Subject: util.session: What is a session? --- util/session.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 util/session.lua (limited to 'util') diff --git a/util/session.lua b/util/session.lua new file mode 100644 index 00000000..735b6165 --- /dev/null +++ b/util/session.lua @@ -0,0 +1,11 @@ + +local function new_session(typ) + local session = { + type = typ .. "_unauthed"; + }; + return session; +end + +return { + new = new_session; +} -- cgit v1.2.3 From 3bf4a8eef76681cf3ef9dd4421a045c69cbfe860 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Nov 2015 19:09:51 +0100 Subject: util.session: What is the identity of a session? --- util/session.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'util') diff --git a/util/session.lua b/util/session.lua index 735b6165..6993842c 100644 --- a/util/session.lua +++ b/util/session.lua @@ -6,6 +6,13 @@ local function new_session(typ) return session; end +local function set_id(session) + local id = typ .. tostring(session):match("%x+$"):lower(); + session.id = id; + return session; +end + return { new = new_session; + set_id = set_id; } -- cgit v1.2.3 From cfc3a5ad7be12ee67e1d14df04dc03acc73394f3 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Nov 2015 19:04:54 +0100 Subject: util.session: What does the session say? --- util/session.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'util') diff --git a/util/session.lua b/util/session.lua index 6993842c..7703c265 100644 --- a/util/session.lua +++ b/util/session.lua @@ -1,3 +1,4 @@ +local logger = require "util.logger"; local function new_session(typ) local session = { @@ -12,7 +13,14 @@ local function set_id(session) return session; end +local function set_logger(session) + local log = logger.init(id); + session.log = log; + return session; +end + return { new = new_session; set_id = set_id; + set_logger = set_logger; } -- cgit v1.2.3 From 40f592ecf0d56591183a1b18ba7d7a0ba5fa87cf Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Nov 2015 19:26:27 +0100 Subject: util.session: How does a session relate do a connection? --- util/session.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'util') diff --git a/util/session.lua b/util/session.lua index 7703c265..03ddcf47 100644 --- a/util/session.lua +++ b/util/session.lua @@ -19,8 +19,15 @@ local function set_logger(session) return session; end +local function set_conn(session, conn) + session.conn = conn; + session.ip = conn:ip(); + return session; +end + return { new = new_session; set_id = set_id; set_logger = set_logger; + set_conn = set_conn; } -- cgit v1.2.3 From 273a126b4b19876418f091ad462897576325040b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 24 Nov 2015 19:31:37 +0100 Subject: util.session: How would you even send anything to a session? --- util/session.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'util') diff --git a/util/session.lua b/util/session.lua index 03ddcf47..fda7fccd 100644 --- a/util/session.lua +++ b/util/session.lua @@ -1,3 +1,4 @@ +local initialize_filters = require "util.filters".initialize; local logger = require "util.logger"; local function new_session(typ) @@ -25,9 +26,40 @@ local function set_conn(session, conn) return session; end +local function set_send(session) + local conn = session.conn; + if not conn then + function session.send(data) + log("debug", "Discarding data sent to unconnected session: %s", tostring(data)); + return false; + end + return session; + end + local filter = initialize_filters(session); + local w = conn.write; + session.send = function (t) + if t.name then + t = filter("stanzas/out", t); + end + if t then + t = filter("bytes/out", tostring(t)); + if t then + local ret, err = w(conn, t); + if not ret then + session.log("debug", "Error writing to connection: %s", tostring(err)); + return false, err; + end + end + end + return true; + end + return session; +end + return { new = new_session; set_id = set_id; set_logger = set_logger; set_conn = set_conn; + set_send = set_send; } -- cgit v1.2.3 From dd9e60beba3cdea9b29aff668a4af411f03918b9 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 25 Nov 2015 20:49:41 +0100 Subject: util.cache: Make sure cache size is specified as an integer --- util/cache.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'util') diff --git a/util/cache.lua b/util/cache.lua index 9453697c..32670751 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -2,6 +2,8 @@ local cache_methods = {}; local cache_mt = { __index = cache_methods }; local function new(size) + size = assert(tonumber(size), "cache size must be a number"); + size = math.floor(size); assert(size > 0, "cache size must be greater than zero"); local data = {}; return setmetatable({ data = data, count = 0, size = size, head = nil, tail = nil }, cache_mt); -- cgit v1.2.3 From c81eea619aabb5d3322bb9baf89f48764f92802b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Nov 2015 00:07:48 +0000 Subject: util.cache: Small update to prefix private fields with an underscore, add a :count() method (same as util.queue) and add an optional on_evict callback --- util/cache.lua | 75 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 32 deletions(-) (limited to 'util') diff --git a/util/cache.lua b/util/cache.lua index 32670751..72b74351 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -1,13 +1,3 @@ -local cache_methods = {}; -local cache_mt = { __index = cache_methods }; - -local function new(size) - size = assert(tonumber(size), "cache size must be a number"); - size = math.floor(size); - assert(size > 0, "cache size must be greater than zero"); - local data = {}; - return setmetatable({ data = data, count = 0, size = size, head = nil, tail = nil }, cache_mt); -end local function _remove(list, m) if m.prev then @@ -16,29 +6,32 @@ local function _remove(list, m) if m.next then m.next.prev = m.prev; end - if list.tail == m then - list.tail = m.prev; + if list._tail == m then + list._tail = m.prev; end - if list.head == m then - list.head = m.next; + if list._head == m then + list._head = m.next; end - list.count = list.count - 1; + list._count = list._count - 1; end local function _insert(list, m) - if list.head then - list.head.prev = m; + if list._head then + list._head.prev = m; end - m.prev, m.next = nil, list.head; - list.head = m; - if not list.tail then - list.tail = m; + m.prev, m.next = nil, list._head; + list._head = m; + if not list._tail then + list._tail = m; end - list.count = list.count + 1; + list._count = list._count + 1; end +local cache_methods = {}; +local cache_mt = { __index = cache_methods }; + function cache_methods:set(k, v) - local m = self.data[k]; + local m = self._data[k]; if m then -- Key already exists if v ~= nil then @@ -49,27 +42,33 @@ function cache_methods:set(k, v) else -- Remove from list _remove(self, m); - self.data[k] = nil; + self._data[k] = nil; end - return; + return true; end -- New key if v == nil then - return; + return true; end -- Check whether we need to remove oldest k/v - if self.count == self.size then - self.data[self.tail.key] = nil; - _remove(self, self.tail); + if self._count == self.size then + local tail = self._tail; + local on_evict = self._on_evict; + if on_evict then + on_evict(tail.key, tail.value); + end + _remove(self, tail); + self._data[tail.key] = nil; end m = { key = k, value = v, prev = nil, next = nil }; - self.data[k] = m; + self._data[k] = m; _insert(self, m); + return true; end function cache_methods:get(k) - local m = self.data[k]; + local m = self._data[k]; if m then return m.value; end @@ -77,7 +76,7 @@ function cache_methods:get(k) end function cache_methods:items() - local m = self.head; + local m = self._head; return function () if not m then return; @@ -88,6 +87,18 @@ function cache_methods:items() end end +function cache_methods:count() + return self._count; +end + +local function new(size, on_evict) + size = assert(tonumber(size), "cache size must be a number"); + size = math.floor(size); + assert(size > 0, "cache size must be greater than zero"); + local data = {}; + return setmetatable({ _data = data, _count = 0, size = size, _head = nil, _tail = nil, _on_evict = on_evict }, cache_mt); +end + return { new = new; } -- cgit v1.2.3