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
|
;; Copyright 2006-2011, Matthew Welland.
;;
;; This program is made available under the GNU GPL version 2.0 or
;; greater. See the accompanying file COPYING for details.
;;
;; This program is distributed WITHOUT ANY WARRANTY; without even the
;; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.
(declare (unit server))
;; procstr is the name of the procedure to be called as a string
(define (server:autoremote procstr params)
(handle-exceptions
exn
(begin
(debug:print 1 "Remote failed for " proc " " params)
(apply (eval (string->symbol proc)) params))
(if *runremote*
(apply (eval (string->symbol (conc "remote:" procstr))) params)
(eval (string->symbol procstr) params))))
(define (server:start db)
(debug:print 0 "Attempting to start the server ...")
(let* ((rpc:listener (server:find-free-port-and-open (rpc:default-server-port)))
(th1 (make-thread
(cute (rpc:make-server rpc:listener) "rpc:server")
'rpc:server)))
(db:set-var db "SERVER" (conc (get-host-name) ":" (rpc:default-server-port)))
(rpc:publish-procedure!
'remote:run
(lambda (procstr . params)
(server:autoremote procstr params)))
(set! *rpc:listener* rpc:listener*)
(thread-start! rpc:server)))
(define (server:find-free-port-and-open port)
(handle-exceptions
exn
(begin
(print "Failed to bind to port " (rpc:default-server-port) ", trying next port")
(server:find-free-port-and-open (+ port 1)))
(rpc:default-server-port port)
(tcp-listen (rpc:default-server-port))))
(define (server:client-setup db)
(let* ((hostinfo (db:get-var db "SERVER"))
(hostdat (if hostinfo (string-split hostinfo ":")))
(host (if hostinfo (car hostdat)))
(port (if (and hostinfo (> (length hostdat) 1))(cadr hostdat) #f)))
(set! *runremote* (vector host port))))
|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
|
>
|
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
;; Copyright 2006-2011, Matthew Welland.
;;
;; This program is made available under the GNU GPL version 2.0 or
;; greater. See the accompanying file COPYING for details.
;;
;; This program is distributed WITHOUT ANY WARRANTY; without even the
;; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.
(require-extension (srfi 18) extras tcp rpc)
(import (prefix rpc rpc:))
(use sqlite3 srfi-1 posix regex regex-case srfi-69 hostinfo)
(import (prefix sqlite3 sqlite3:))
(declare (unit server))
(declare (uses common))
(declare (uses db))
(include "common_records.scm")
(include "db_records.scm")
;; procstr is the name of the procedure to be called as a string
(define (server:autoremote procstr params)
(handle-exceptions
exn
(begin
(debug:print 1 "Remote failed for " proc " " params)
(apply (eval (string->symbol proc)) params))
(if *runremote*
(apply (eval (string->symbol (conc "remote:" procstr))) params)
(eval (string->symbol procstr) params))))
(define (server:start db)
(debug:print 0 "Attempting to start the server ...")
(let* ((rpc:listener (server:find-free-port-and-open (rpc:default-server-port)))
(th1 (make-thread
(cute (rpc:make-server rpc:listener) "rpc:server")
'rpc:server))
(hostname (get-host-name))
(ipaddr (hostname->ip hostname))
(ipaddrstr (string-intersperse (map number->string (u8vector->list ipaddr)) "."))
(ipaddrstr:port (conc ipaddrstr ":" (rpc:default-server-port))))
(db:set-var db "SERVER" ipaddrstr:port)
(rpc:publish-procedure!
'remote:run
(lambda (procstr . params)
(server:autoremote procstr params)))
;;======================================================================
;; db specials here
;;======================================================================
;; ** set-tests-state-status
(rpc:publish-procedure!
'rdb:set-tests-state-status
(lambda (run-id testnames currstate currstatus newstate newstatus)
(db:set-tests-state-status db run-id testnames currstate currstatus newstate newstatus)))
(rpc:publish-procedure!
'rdb:teststep-set-status!
(lambda (run-id test-name teststep-name state-in status-in item-path comment logfile)
(db:teststep-set-status! db run-id test-name teststep-name state-in status-in item-path comment logfile)))
(rpc:publish-procedure!
'rdb:test-update-meta-info
(lambda (run-id testname itemdat minutes cpuload diskfree tmpfree)
(db:test-update-meta-info db run-id testname item-path minutes cpuload diskfree tmpfree)))
(rpc:publish-procedure!
'rdb:test-set-state-status-by-run-id-testname
(lambda (run-id test-name item-path status state)
(db:test-set-state-status-by-run-id-testname db run-id test-name item-path status state)))
(rpc:publish-procedure!
'rdb:csv->test-data
(lambda (test-id csvdata)
(db:csv->data db test-id csvdata)))
(rpc:publish-procedure!
'rdb:roll-up-pass-fail-counts
(lambda (run-id test-name item-path status)
(db:roll-up-pass-fail-counts db run-id test-name item-path status)))
(rpc:publish-procedure!
'rdb:test-set-comment
(lambda (run-id test-name item-path comment)
(db:test-set-comment db run-id test-name item-path comment)))
(rpc:publish-procedure!
'rpc:test-set-log!
(lambda (run-id test-name item-path logf)
(db:test-set-log! db run-id test-name item-path logf)))
(set! *rpc:listener* rpc:listener)
(on-exit (lambda ()
(sqlite3:execute db "DELETE FROM metadat WHERE var='SERVER' and val=?;" host:port)
(sqlite3:finalize! db)))
(thread-start! th1)
(thread-join! th1))) ;; rpc:server)))
(define (server:find-free-port-and-open port)
(handle-exceptions
exn
(begin
(print "Failed to bind to port " (rpc:default-server-port) ", trying next port")
(server:find-free-port-and-open (+ port 1)))
(rpc:default-server-port port)
(tcp-listen (rpc:default-server-port))))
(define (server:client-setup db)
(let* ((hostinfo (db:get-var db "SERVER"))
(hostdat (if hostinfo (string-split hostinfo ":")))
(host (if hostinfo (car hostdat)))
(port (if (and hostinfo (> (length hostdat) 1))(cadr hostdat) #f)))
(if (and port
(string->number port))
(debug:print 2 "INFO: Setting up to connect to host " host ":" port))
(set! *runremote* (if port (vector host (string->number port)) #f))))
|