Fixed hubmgr so that it rereads the hublist when changed.
[doldaconnect.git] / lib / guile / hubmgr
CommitLineData
6b081952 1#!/usr/bin/guile -s
2!#
3
3af4536f
FT
4; Dolda Connect - Modular multiuser Direct Connect-style client
5; Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
6;
7; This program is free software; you can redistribute it and/or modify
8; it under the terms of the GNU General Public License as published by
9; the Free Software Foundation; either version 2 of the License, or
10; (at your option) any later version.
11;
12; This program is distributed in the hope that it will be useful,
13; but WITHOUT ANY WARRANTY; without even the implied warranty of
14; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15; GNU General Public License for more details.
16;
17; You should have received a copy of the GNU General Public License
18; along with this program; if not, write to the Free Software
19; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
6b081952 21(use-modules (dolcon ui) (dolcon util))
f4473d02 22(use-modules (ice-9 format) (ice-9 rdelim) (ice-9 getopt-long))
6b081952 23
24(define max-hubs 6)
25(define hub-list '())
26(define hl-file (string-append (getenv "HOME") "/.hublist"))
27(define hublist '())
28(define connlist '())
29(define statelist '())
f4473d02 30(define logdest #t)
f9661e7e 31(define hl-mtime 0)
6b081952 32
33(define (logf . args)
34 (let ((fmt (car args)) (args (cdr args)))
f4473d02
FT
35 (if logdest
36 (apply format (cons* logdest (string-append fmt "~%") args)))))
6b081952 37
38(define (list-delta l1 l2)
39 (let ((r1 '()) (r2 '()))
40 (for-each (lambda (o1)
41 (catch 'found
42 (lambda ()
43 (for-each (lambda (o2)
44 (if (equal? o1 o2) (throw 'found o2)))
45 l2)
46 (set! r2 (cons o1 r2)))
47 (lambda (sig ret)
48 (set! r1 (cons (cons o1 ret) r1))
49 (set! l2 (delq ret l2)))))
50 l1)
51 (list r1 r2 l2)))
52
53(define (read-hl)
91328d65
FT
54 (letrec ((read-lines (lambda (lines p)
55 (let ((line (read-line p)))
56 (if (eof-object? line)
57 (begin (close-port p)
58 lines)
59 (read-lines (let ((lexed (dc-lexsexpr line)))
60 (if (> (length lexed) 0)
61 (append lines (list lexed))
62 lines)) p))))))
63 (catch 'system-error
64 (lambda () (read-lines '() (open-input-file hl-file)))
65 (lambda (key . args) '()))))
6b081952 66
67(define (cklist)
68 (set! statelist (let ((nl '()) (ct (current-time)))
69 (for-each (lambda (o)
70 (if (< ct (+ (cadr o) (caddr o)))
71 (set! nl (cons o nl))))
72 statelist)
73 nl))
f9661e7e
FT
74 (catch 'system-error
75 (lambda ()
76 (let ((mtime (stat:mtime (stat hl-file))))
77 (if (> mtime hl-mtime)
78 (let* ((delta (list-delta hublist (read-hl)))
79 (same (car delta))
80 (del (cadr delta))
81 (new (caddr delta)))
82 (display same) (newline)
83 (display del) (newline)
84 (display new) (newline)
85 (for-each (lambda (o)
86 (let ((el (assq o connlist)))
87 (if el
88 (begin (if (not (eq? (cdr el) 'pend))
89 (dc-qcmd (list "dcnct" (cdr el))))
90 (set! connlist (delq el connlist))))))
91 del)
92 (set! hublist (append (map (lambda (o) (car o)) same) new))
93 (set! hl-mtime mtime)))))
94 (lambda (key . args) '()))
6b081952 95 (for-each (lambda (o)
96 (if (and (not (assq o connlist))
97 (not (assq o statelist)))
98 (begin (logf "connecting to ~a" (cadr o))
99 (set! connlist (cons (cons o 'pend) connlist))
ae958036 100 (dc-qcmd (cons* "cnct" o)
6b081952 101 (let ((hub o))
102 (lambda (resp)
103 (let ((er (dc-extract resp)) (ir (dc-intresp resp)))
104 (if (= (cdr (assq 'code er)) 200)
105 (begin (set-cdr! (assq hub connlist) (car ir))
106 (logf "~a state syn (~a)" (cadr hub) (car ir)))
107 (begin (set! connlist (delq (assq hub connlist) connlist))
4024acee 108 (set! statelist (cons (list hub (current-time) 10) statelist))
6b081952 109 (logf "~a failed (~a)" (cadr hub) (cdr (assq 'code er))))))))))))
110 hublist))
111
112(define (hubmgr-main args)
ae958036
FT
113 (let ((opts (getopt-long args '((nodaemon (single-char #\d) (value #f))
114 (server (single-char #\s) (value #t))
115 (quiet (single-char #\q) (value #f))))))
116 (if (option-ref opts 'quiet #f) (set! logdest #f))
6b081952 117 (set! hublist (read-hl))
f9661e7e 118 (set! hl-mtime (stat:mtime (stat hl-file)))
6b081952 119 (logf "read ~a hubs" (length hublist))
ae958036
FT
120 (dc-c&l (not (option-ref opts 'quiet #f)) (option-ref opts 'server #f) #t)
121 (dc-ecmd-assert 200 "notify" "fn:act" "on" "msg" "on")
f4473d02 122 (dc-ecmd-assert 200 "register" "hubmgr")
ae958036 123 (dc-util-handle 'fn 'msg)
6b081952 124 (dc-fnproc-reg 'state (lambda (fn)
2a156d4f 125 (if (and (eq? (cdr (assq 'state fn)) 'dead)
126 (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist)))
6b081952 127 (begin (logf "~a died" (cdr (assq 'id fn)))
128 (dc-qcmd (list "dcnct" (cdr (assq 'id fn))))))
129 (cklist)))
130 (dc-fnproc-reg 'dstr (lambda (fn)
131 (let ((clf (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist))))
132 (if clf
133 (let ((hlf (cdr clf)))
134 (logf "~a disappeared" (cadr hlf))
135 (set! connlist (delq (assq hlf connlist) connlist))
136 (set! statelist (cons (list hlf (current-time) 10) statelist)))))
137 (cklist)))
ae958036
FT
138 (dc-msgproc-reg (lambda (sender msg)
139 (if (equal? (car msg) "exit")
140 (throw 'quit 0))))
6b081952 141 (dc-loop-reg ".periodic" #f cklist)
142
ae958036
FT
143 (if (not (option-ref opts 'nodaemon #f))
144 (begin (logf "daemonizing...")
145 (if (= (primitive-fork) 0)
146 (set! logdest #f)
147 (primitive-exit 0))))
f4473d02 148
ae958036 149 (for-each (lambda (sig) (sigaction sig (lambda (sig) (throw 'quit 0)))) (list SIGINT SIGTERM SIGHUP))
6b081952 150 (cklist)
ae958036
FT
151 (catch 'quit dc-loop
152 (lambda (sig ret)
153 (catch 'quit
154 (lambda ()
155 (for-each (lambda (o)
156 (if (not (eq? (cdr o) 'pend))
157 (dc-ecmd "dcnct" (cdr o))))
158 connlist)
159 )
160 (lambda (sig ret) ret))
161 ret))))
6b081952 162
163(setlocale LC_ALL "")
164(hubmgr-main (command-line))