File: netsync.lua

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (98 lines) | stat: -rw-r--r-- 2,904 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

function mtn2(...)
  return mtn("--db=test2.db", "--keydir=keys2", unpack(arg))
end

function mtn3(...)
  return mtn("--db=test3.db", "--keydir=keys3", unpack(arg))
end

netsync = {}
netsync.internal = {}

function netsync.setup()
  check(copy("test.db", "test2.db"))
  check(copy("keys", "keys2"))
  check(copy("test.db", "test3.db"))
  check(copy("keys", "keys3"))
  check(getstd("common/netsync-hooks.lua", "netsync.lua"))
  math.randomseed(os.time())
end

function netsync.setup_with_notes()
  netsync.setup()
  check(getstd("common/netsync-hooks_with_notes.lua", "netsync.lua"))
end

function netsync.internal.client(srv, oper, pat, n, res)
  if n == nil then n = 2 end
  args = {"--rcfile=netsync.lua", "--keydir=keys"..n,
          "--db=test"..n..".db", oper, srv.address}
  if type(pat) == "string" then
    table.insert(args, pat)
  elseif type(pat) == "table" then
    for k, v in pairs(pat) do
      table.insert(args, v)
    end
  elseif pat ~= nil then
    err("Bad pattern type "..type(pat))
  end
  check(mtn(unpack(args)), res, false, false)
end
function netsync.internal.pull(srv, pat, n, res) srv:client("pull", pat, n, res) end
function netsync.internal.push(srv, pat, n, res) srv:client("push", pat, n, res) end
function netsync.internal.sync(srv, pat, n, res) srv:client("sync", pat, n, res) end

function netsync.start(pat, n, min)
  if pat == "" or pat == nil then pat = "{*}" end
  local args = {}
  local fn = mtn
  local addr = "localhost:" .. math.random(20000, 50000)
  table.insert(args, "--dump=_MTN/server_dump")
  table.insert(args, "--bind="..addr)
  if min then
    fn = minhooks_mtn
  else
    table.insert(args, "--rcfile=netsync.lua")
  end
  if n ~= nil then
    table.insert(args, "--keydir=keys"..n)
    table.insert(args, "--db=test"..n..".db")
  end
  table.insert(args, "serve")
  if type(pat) == "string" then
    table.insert(args, pat)
  elseif type(pat) == "table" then
    for k, v in pairs(pat) do
      table.insert(args, v)
    end
  else
    err("Bad pattern type "..type(pat))
  end
  local out = bg(fn(unpack(args)), false, false, false)
  out.address = addr
  local mt = getmetatable(out)
  mt.client = netsync.internal.client
  mt.pull = netsync.internal.pull
  mt.push = netsync.internal.push
  mt.sync = netsync.internal.sync
  local mt_wait = mt.wait
  mt.check = function(obj) return not mt_wait(obj, 0) end
  mt.wait = nil -- using this would hang; don't allow it
  -- wait for "beginning service..."
  while fsize(out.prefix .. "stderr") == 0 do
    sleep(1)
    check(out:check())
  end
  mt.stop = mt.finish
  return out
end

function netsync.internal.run(oper, pat)
  local srv = netsync.start(pat)
  srv:client(oper, pat)
  srv:finish()
end
function netsync.pull(pat) netsync.internal.run("pull", pat) end
function netsync.push(pat) netsync.internal.run("push", pat) end
function netsync.sync(pat) netsync.internal.run("sync", pat) end