Made hubmgr daemonizable.
[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)
6b081952 31
32(define (logf . args)
33 (let ((fmt (car args)) (args (cdr args)))
f4473d02
FT
34 (if logdest
35 (apply format (cons* logdest (string-append fmt "~%") args)))))
6b081952 36
37(define (list-delta l1 l2)
38 (let ((r1 '()) (r2 '()))
39 (for-each (lambda (o1)
40 (catch 'found
41 (lambda ()
42 (for-each (lambda (o2)
43 (if (equal? o1 o2) (throw 'found o2)))
44 l2)
45 (set! r2 (cons o1 r2)))
46 (lambda (sig ret)
47 (set! r1 (cons (cons o1 ret) r1))
48 (set! l2 (delq ret l2)))))
49 l1)
50 (list r1 r2 l2)))
51
52(define (read-hl)
53 (catch 'system-error
54 (lambda ()
55 (let ((p (open-input-file hl-file)))
56 (catch 'eof
57 (lambda ()
58 (let ((lines '()))
59 (while #t
60 (let ((line (read-line p)))
61 (if (eof-object? line)
62 (throw 'eof lines)
63 (let ((lexed (dc-lexsexpr line)))
64 (if (> (length lexed) 0)
65 (set! lines (append lines (list lexed))))))))))
66 (lambda (s a) (close-port p) a))))
67 (lambda (key . args)
68 '())))
69
70(define (cklist)
71 (set! statelist (let ((nl '()) (ct (current-time)))
72 (for-each (lambda (o)
73 (if (< ct (+ (cadr o) (caddr o)))
74 (set! nl (cons o nl))))
75 statelist)
76 nl))
77 (for-each (lambda (o)
78 (if (and (not (assq o connlist))
79 (not (assq o statelist)))
80 (begin (logf "connecting to ~a" (cadr o))
81 (set! connlist (cons (cons o 'pend) connlist))
82 (dc-qcmd (list* "cnct" o)
83 (let ((hub o))
84 (lambda (resp)
85 (let ((er (dc-extract resp)) (ir (dc-intresp resp)))
86 (if (= (cdr (assq 'code er)) 200)
87 (begin (set-cdr! (assq hub connlist) (car ir))
88 (logf "~a state syn (~a)" (cadr hub) (car ir)))
89 (begin (set! connlist (delq (assq hub connlist) connlist))
4024acee 90 (set! statelist (cons (list hub (current-time) 10) statelist))
6b081952 91 (logf "~a failed (~a)" (cadr hub) (cdr (assq 'code er))))))))))))
92 hublist))
93
94(define (hubmgr-main args)
f4473d02
FT
95 (let ((opts (getopt-long args '((daemon (single-char #\d) (value #f))
96 (server (single-char #\s) (value #t))))))
97
6b081952 98 (set! hublist (read-hl))
99 (logf "read ~a hubs" (length hublist))
f4473d02 100 (dc-c&l #t (option-ref opts 'server #f) #t)
6b081952 101 (dc-ecmd-assert 200 "notify" "fn:act" "on")
f4473d02 102 (dc-ecmd-assert 200 "register" "hubmgr")
6b081952 103 (dc-handle-fn)
104 (dc-fnproc-reg 'state (lambda (fn)
2a156d4f 105 (if (and (eq? (cdr (assq 'state fn)) 'dead)
106 (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist)))
6b081952 107 (begin (logf "~a died" (cdr (assq 'id fn)))
108 (dc-qcmd (list "dcnct" (cdr (assq 'id fn))))))
109 (cklist)))
110 (dc-fnproc-reg 'dstr (lambda (fn)
111 (let ((clf (assq (cdr (assq 'id fn)) (map (lambda (o) (cons (cdr o) (car o))) connlist))))
112 (if clf
113 (let ((hlf (cdr clf)))
114 (logf "~a disappeared" (cadr hlf))
115 (set! connlist (delq (assq hlf connlist) connlist))
116 (set! statelist (cons (list hlf (current-time) 10) statelist)))))
117 (cklist)))
118 (dc-loop-reg ".periodic" #f cklist)
119
f4473d02
FT
120 (if (and (option-ref opts 'daemon #f)
121 (not (= (primitive-fork) 0)))
122 (primitive-exit 0)
123 (set! logdest #f))
124
6b081952 125 (cklist)
126 (dc-loop)))
127
128(setlocale LC_ALL "")
129(hubmgr-main (command-line))