Skip to content

Commit

Permalink
unimportant
Browse files Browse the repository at this point in the history
  • Loading branch information
capr committed Dec 11, 2015
1 parent 4793d00 commit 722c9e2
Show file tree
Hide file tree
Showing 6 changed files with 649 additions and 166 deletions.
2 changes: 1 addition & 1 deletion src/apps/socket/unix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function UnixSocket:new (arg)
local sa = S.t.sockaddr_un(file)
local ok, err = sock:connect(sa)
if not ok then
if err.CONNREFUSED or err.AGAIN then return end
if err.CONNREFUSED or err.AGAIN or err.NOENT then return end
assert(nil, err)
end
return sock
Expand Down
228 changes: 228 additions & 0 deletions src/pp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@

--fast, small recursive pretty printer with optional indentation and cycle detection.
--Written by Cosmin Apreutesei. Public Domain.

--pretty-printing of non-structured types

local type, tostring = type, tostring
local string_format, string_dump = string.format, string.dump
local math_huge, math_floor = math.huge, math.floor

local escapes = { --don't add unpopular escapes here
['\\'] = '\\\\',
['\t'] = '\\t',
['\n'] = '\\n',
['\r'] = '\\r',
}

local function escape_byte_long(c1, c2)
return string_format('\\%03d%s', c1:byte(), c2)
end
local function escape_byte_short(c)
return string_format('\\%d', c:byte())
end
local function quote_string(s, quote)
s = s:gsub('[\\\t\n\r]', escapes)
s = s:gsub(quote, '\\%1')
s = s:gsub('([^\32-\126])([0-9])', escape_byte_long)
s = s:gsub('[^\32-\126]', escape_byte_short)
return s
end

local function format_string(s, quote)
return string_format('%s%s%s', quote, quote_string(s, quote), quote)
end

local function write_string(s, write, quote)
write(quote); write(quote_string(s, quote)); write(quote)
end

local keywords = {}
for i,k in ipairs{
'and', 'break', 'do', 'else', 'elseif', 'end',
'false', 'for', 'function', 'goto', 'if', 'in',
'local', 'nil', 'not', 'or', 'repeat', 'return',
'then', 'true', 'until', 'while',
} do
keywords[k] = true
end

local function is_identifier(v)
return type(v) == 'string' and not keywords[v]
and v:find('^[a-zA-Z_][a-zA-Z_0-9]*$') ~= nil
end

local hasinf = math_huge == math_huge - 1
local function format_number(v)
if v ~= v then
return '0/0' --NaN
elseif hasinf and v == math_huge then
return '1/0' --writing 'math.huge' would not make it portable, just wrong
elseif hasinf and v == -math_huge then
return '-1/0'
elseif v == math_floor(v) and v >= -2^31 and v <= 2^31-1 then
return string_format('%d', v) --printing with %d is faster
else
return string_format('%0.17g', v)
end
end

local function write_number(v, write)
write(format_number(v))
end

local function is_dumpable(f)
return type(f) == 'function' and debug.getinfo(f, 'Su').what ~= 'C'
end

local function format_function(f)
return string_format('loadstring(%s)', format_string(string_dump(f, true)))
end

local function write_function(f, write, quote)
write'loadstring('; write_string(string_dump(f, true), write, quote); write')'
end

local ffi, int64, uint64
local function is_int64(v)
if type(v) ~= 'cdata' then return false end
if not int64 then
ffi = require'ffi'
int64 = ffi.new'int64_t'
uint64 = ffi.new'uint64_t'
end
return ffi.istype(v, int64) or ffi.istype(v, uint64)
end

local function format_int64(v)
return tostring(v)
end

local function write_int64(v, write)
write(format_int64(v))
end

local function format(v, quote)
quote = quote or "'"
if v == nil or type(v) == 'boolean' then
return tostring(v)
elseif type(v) == 'number' then
return format_number(v)
elseif type(v) == 'string' then
return format_string(v, quote)
elseif is_dumpable(v) then
return format_function(v)
elseif is_int64(v) then
return format_int64(v)
else
error('unserializable', 0)
end
end

local function is_serializable(v)
return type(v) == 'nil' or type(v) == 'boolean' or type(v) == 'string'
or type(v) == 'number' or is_dumpable(v) or is_int64(v)
end

local function pf_write(v, write, quote)
quote = quote or "'"
if v == nil or type(v) == 'boolean' then
write(tostring(v))
elseif type(v) == 'number' then
write_number(v, write)
elseif type(v) == 'string' then
write_string(v, write, quote)
elseif is_dumpable(v) then
write_function(v, write, quote)
elseif is_int64(v) then
write_int64(v, write)
else
error('unserializable', 0)
end
end

local function pretty(v, write, indent, parents, quote, onerror, depth, wwrapper)
if is_serializable(v) then
pf_write(v, write, quote)
elseif getmetatable(v) and getmetatable(v).__pwrite then
wwrapper = wwrapper or function(v)
pretty(v, write, nil, parents, quote, onerror, -1, wwrapper)
end
getmetatable(v).__pwrite(v, write, wwrapper)
elseif type(v) == 'table' then
if parents then
if parents[v] then
write(onerror and onerror('cycle', v, depth) or 'nil --[[cycle]]')
return
end
parents[v] = true
end

write'{'
local maxn = 0; while rawget(v, maxn+1) ~= nil do maxn = maxn+1 end

local first = true
for k,v in pairs(v) do
if not (maxn > 0 and type(k) == 'number' and k == math.floor(k) and k >= 1 and k <= maxn) then
if first then first = false else write',' end
if indent then write'\n'; write(indent:rep(depth)) end
if is_identifier(k) then
write(k); write'='
else
write'['; pretty(k, write, indent, parents, quote, onerror, depth + 1, wwrapper); write']='
end
pretty(v, write, indent, parents, quote, onerror, depth + 1, wwrapper)
end
end

for k,v in ipairs(v) do
if first then first = false else write',' end
if indent then write'\n'; write(indent:rep(depth)) end
pretty(v, write, indent, parents, quote, onerror, depth + 1, wwrapper)
end

if indent then write'\n'; write(indent:rep(depth-1)) end
write'}'

if parents then parents[v] = nil end
else
write(onerror and onerror('unserializable', v, depth) or
string.format('nil --[[unserializable %s]]', type(v)))
end
end

local function to_sink(write, v, indent, parents, quote, onerror, depth)
return pretty(v, write, indent, parents, quote, onerror, depth or 1)
end

local function to_string(v, indent, parents, quote, onerror, depth)
local buf = {}
pretty(v, function(s) buf[#buf+1] = s end, indent, parents, quote, onerror, depth or 1)
return table.concat(buf)
end

local function to_file(file, v, indent, parents, quote, onerror, depth)
local f = assert(io.open(file, 'wb'))
f:write'return '
pretty(v, function(s) f:write(s) end, indent, parents, quote, onerror, depth or 1)
f:close()
end

local function pp(...)
local t = {}
for i=1,select('#',...) do
t[i] = to_string(select(i,...), ' ', {})
end
print(unpack(t))
return ...
end

return setmetatable({
write = to_sink,
format = to_string,
save = to_file,
print = pp,
pp = pp, --old API
}, {__call = function(self, ...)
return pp(...)
end})
14 changes: 10 additions & 4 deletions src/program/lisper/dev-env/lisp.fib
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[4660] 00:00:00:00:aa:01 rloc fd80:1::2 1 50
[4660] 00:00:00:00:aa:02 rloc fd80:2::2 1 50
[4660] 00:00:00:00:aa:03 rloc fd80:3::2 1 50
[4660] FF:FF:FF:FF:FF:FF rloc fd80:1::2 1 50, fd80:2::2 1 50, fd80:3::2 1 50
{ "instance-id": 1, "eid-prefix": "000000-00bb01/48", "rlocs": [{ "rloc": "fd80:8::2", "priority": 1, "weight": 50 }] }

{ "instance-id": 1, "eid-prefix": "000000-00bb02/48", "rlocs": [{ "rloc": "fd80:8::2", "priority": 1, "weight": 50 }] }

{ "instance-id": 1, "eid-prefix": "000000-00bb03/48", "rlocs": [{ "rloc": "fd80:8::2", "priority": 1, "weight": 50 }] }

{ "instance-id": 4660, "eid-prefix": "ffffff-ffffff/48", "rles": [
{ "rle": "fd80:8::2", "priority": 1, "weight": 50 }
]}

54 changes: 30 additions & 24 deletions src/program/lisper/dev-env/lisp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ io.stderr:setvbuf'no'
--LISP controller mock-up program for testing.

local function assert(v, ...)
if v then return v, ... end
error(tostring((...)), 2)
if v then return v, ... end
error(tostring((...)), 2)
end

local ffi = require("ffi")
Expand All @@ -18,33 +18,39 @@ local PUNT_SOCK = "/var/tmp/lispers.net-itr"
S.signal('pipe', 'ign') --I 💔 Linux

::retry::
sock = sock or assert(S.socket("unix", "stream, nonblock"))
sock = sock or assert(S.socket("unix", "dgram, nonblock"))
local sa = S.t.sockaddr_un(CONTROL_SOCK)
local ok, err = sock:connect(sa)
if not ok then
if err.CONNREFUSED or err.AGAIN then
S.sleep(1)
print'retrying...'
goto retry
end
assert(nil, err)
if err.CONNREFUSED or err.AGAIN then
S.sleep(1)
print'retrying...'
goto retry
end
assert(nil, err)
end
print'connected'

while true do
if assert(S.select({writefds = {sock}}, 0)).count == 1 then

local f = assert(io.open'lisp.fib')
local data = assert(f:read'*a')
f:close()

print'sending...'
if not S.write(sock, data, #data) then
print'write error'
sock:close()
sock = nil
goto retry
end
end
S.sleep(1)
if assert(S.select({writefds = {sock}}, 0)).count == 1 then

local f = assert(io.open'lisp.fib')
local s = f:read'*a'
f:close()
local t = {}
for s in s:gmatch'(.-)[\r?\n][\r?\n]' do
table.insert(t, s)
end

print'sending...'
for i,s in ipairs(t) do
if not S.write(sock, s, #s) then
print'write error'
sock:close()
sock = nil
goto retry
end
end
end
S.sleep(1)
end
34 changes: 22 additions & 12 deletions src/program/lisper/dev-env/lisper.conf
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
{
"control_sock" : "/var/tmp/lisp-ipc-map-cache",
"punt_sock" : "/var/tmp/lispers.net-itr",
"arp_timeout" : 60, // seconds

"interfaces": [
//{ "name": "eth0", "mac": "00:00:00:00:01:04", "pci": "01:00.0" },
//{ "name": "eth1", "mac": "00:00:00:00:01:05", "pci": "02:00.0" },
{ "name": "eth0", "mac": "00:00:00:00:01:05", "ip": "fd80:1::1", "gateway": "fd80:1::1" }
{ "name": "e0", "mac": "00:00:00:00:01:04" }
],
"vlans": [
{ "name": "vlan1", "interface": "eth0", "id": 1 },
{ "name": "vlan2", "interface": "eth0", "id": 2, "mac": "00:00:00:00:01:05",
"ip": "fd80:2::1", "gateway": "fd80:1::1" }

//"vlans": [
// { "name": "vlan1", "interface": "eth0", "id": 1 },
// { "name": "vlan2", "interface": "eth0", "id": 2, "mac": "00:00:00:00:01:05" }
//],

"exits": [
{ "name": "e0", "ip": "fd80:4::2", "interface": "e0", "next_hop": "fd80:4::1" }
],

"lispers": [
{ "ip": "fd:80:1::1", "interface": "eth0" },
{ "ip": "fd:80:2::1", "interface": "eth1_1" }
{ "ip": "fd80:8::2", "exit": "e0" }
],

"local_networks": [
{ "iid": 1, "type": "L2TPv3", "ip": "fd80:1::2", "interface": "eth0", "cookie": "" },
{ "iid": 2, "type": "L2TPv3", "ip": "fd80:2::2", "interface": "vlan1", "cookie": "" },
{ "iid": 2, "interface": "eth1" }
{ "iid": 1, "type": "L2TPv3", "ip": "fd80:1::2", "exit": "e0",
"session_id": 4660, "cookie": "0" },
{ "iid": 1, "type": "L2TPv3", "ip": "fd80:2::2", "exit": "e0",
"session_id": 4660, "cookie": "0" },
{ "iid": 1, "type": "L2TPv3", "ip": "fd80:3::2", "exit": "e0",
"session_id": 4660, "cookie": "0" }

//{ "iid": 2, "interface": "eth1" }
]
}
Loading

0 comments on commit 722c9e2

Please sign in to comment.