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
|
(declare (uses debugprint))
(declare (uses commonmod))
(module tcp-transportmod
*
(import scheme
chicken
data-structures
extras
matchable)
(import (prefix sqlite3 sqlite3:)
posix typed-records srfi-18 srfi-1
srfi-69
stack
files
ports
commonmod
;; debugprint
)
;;======================================================================
;; client
;;======================================================================
;; (define keep-age-param (make-parameter 10)) ;; qif file age, if over move to attic
(defstruct tt
(area #f)
(conns (make-hash-table)) ;; dbfname -> conn
)
(define (tt:make-remote areapath)
(make-tt area: areapath))
(define (tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname)
;; NOTE: areapath is passed in and in tt struct. We'll use passed in value for now.
(let* ((conn (hash-table-ref/default (tt-conns runremote) dbfname #f)))
(if conn
;; have connection, call the server
(let* ((res (tt:send-receive runremote conn cmd rid params)))
(cond
((member res '(busy starting))
(thread-sleep! 1)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname))
(else
res)))
;; no conn yet, find and or start and find a server
(let* ((server (tt:find-server areapath dbfname)))
(if server
(let* ((conn (tt:server-connect server)))
(hash-table-set! (tt-conns runremote) dbfname conn)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname))
;; no server, try to start one
(begin
(tt:start-server areapath dbfname)
(thread-sleep! 1)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname)))))))
|
>
>
>
>
|
|
>
|
>
>
|
>
|
|
<
|
|
|
|
>
|
>
>
>
|
|
>
>
>
>
>
>
>
>
|
|
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
|
(declare (uses debugprint))
(declare (uses commonmod))
(module tcp-transportmod
*
(import scheme
(prefix sqlite3 sqlite3:)
chicken
data-structures
directory-utils
extras
files
hostinfo
matchable
md5
message-digest
ports
posix
srfi-1
srfi-18
srfi-4
srfi-69
stack
typed-records
commonmod
debugprint
)
;;======================================================================
;; client
;;======================================================================
;; (define keep-age-param (make-parameter 10)) ;; qif file age, if over move to attic
(defstruct tt
;; all
(areapath #f)
;; client related
(conns (make-hash-table)) ;; dbfname -> conn
;; server related
(cleanup-proc #f)
)
(defstruct tt-conn
host
port
dbfname
)
(define (tt:make-remote areapath)
(make-tt area: areapath))
(define (tt:client-connect-to-server ttdat)
#f)
(define (tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname)
;; NOTE: areapath is passed in and in tt struct. We'll use passed in value for now.
(let* ((conn (hash-table-ref/default (tt-conns runremote) dbfname #f)))
(if conn
;; have connection, call the server
(let* ((res (tt:send-receive runremote conn cmd rid params)))
(cond
((member res '(busy starting))
(thread-sleep! 1)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname))
(else
res)))
;; no conn yet, find and or start and find a server
(let* ((server (tt:find-server areapath dbfname)))
(if server
(let* ((conn (tt:client-connect-to-server server)))
(hash-table-set! (tt-conns runremote) dbfname conn)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname))
;; no server, try to start one
(begin
(tt:start-server areapath dbfname)
(thread-sleep! 1)
(tt:handler runremote cmd rid params attemptnum area-dat areapath readonly-mode dbfname)))))))
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
;;======================================================================
;; server
;;======================================================================
(define (tt:sync-dbs ttdat)
#f)
(define (tt:start-server ttdat)
#f)
(define (tt:server-connect ttdat)
#f)
(define (tt:find-server ttdat)
#f)
(define (tt:shutdown-server ttdat)
#f)
)
|
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
;;======================================================================
;; server
;;======================================================================
(define (tt:sync-dbs ttdat)
#f)
;; start the listener and start responding to requests
;;
(define (tt:start-server ttdat)
#f)
(define (tt:shutdown-server ttdat)
(let* ((cleanproc (tt-cleanup-proc ttdat)))
(if cleanproc (cleanproc))
;; close up ports here
#f))
;; return servid
;; side-effects:
;; ttdat-cleanup-proc is populated with function to remove the serverinfo file
(define (tt:create-server-registration-file ttdat dbfname)
(let* ((areapath (tt-areapath ttdat))
(servdir (tt:get-servinfo-dir areapath))
(conn (hash-table-ref/default (tt-conns ttdat) dbfname #f)))
(assert conn "FATAL: tt:create-server-registration-file called with no conn, dbfname="dbfname)
(let* ((host (tt-conn-host conn))
(port (tt-conn-port conn))
(servinf (conc servdir"/"host":"port"-"(current-process-id)":"dbfname))
(serv-id (tt:mk-signature areapath))
(clean-proc (lambda ()
(delete-file* servinf))))
(tt-cleanup-proc-set! ttdat clean-proc)
(with-output-to-file servinf
(lambda ()
(print "SERVER STARTED: "host":"port" AT "(current-seconds)" server-id: "serv-id" pid: "(current-process-id))))
serv-id)))
;; find valid server
;; get servers listed, last part of name must match :<dbfname>
;; if more than one, wait one second and look again
;; future: ping oldest, if alive remove other :<dbfname> files
;;
(define (tt:find-server ttdat dbfname)
(let* ((areapath (tt-areapath ttdat))
(servdir (tt:get-servinfo-dir areapath))
(sfiles (glob (conc servdir"/*:"dbfname))))
sfiles))
;; Given an area path, start a server process ### NOTE ### > file 2>&1
;; if the target-host is set
;; try running on that host
;; incidental: rotate logs in logs/ dir.
;;
(define (tt:server-process-run areapath testsuite mtexe #!key (profile-mode "")) ;; areapath is *toppath* for a given testsuite area
(let* ((logfile (conc areapath "/logs/server.log")) ;; -" curr-pid "-" target-host ".log"))
(cmdln (conc
mtexe
" -server - ";; (or target-host "-")
" -m testsuite:" testsuite
" " profile-mode
))) ;; (conc " >> " logfile " 2>&1 &")))))
;; we want the remote server to start in *toppath* so push there
(push-directory areapath)
(debug:print 0 *default-log-port* "INFO: Trying to start server in tcp mode (" cmdln ") ...")
(debug:print 0 *default-log-port* "INFO: starting server at " (common:human-time))
(system (conc "nbfake " cmdln))
(pop-directory)))
;;======================================================================
;; utils
;;======================================================================
;; Generate a unique signature for this server
(define (tt:mk-signature areapath)
(message-digest-string (md5-primitive)
(with-output-to-string
(lambda ()
(write (list areapath
(current-process-id)
(argv)))))))
(define (tt:get-best-guess-address hostname)
(let ((res #f))
(for-each
(lambda (adr)
(if (not (eq? (u8vector-ref adr 0) 127))
(set! res adr)))
;; NOTE: This can fail when there is no mention of the host in /etc/hosts. FIXME
(vector->list (hostinfo-addresses (hostname->hostinfo hostname))))
(string-intersperse
(map number->string
(u8vector->list
(if res res (hostname->ip hostname)))) ".")))
(define (tt:get-servinfo-dir areapath)
(let* ((spath (conc areapath"/.servinfo")))
(if (not (file-exists? spath))
(create-directory spath #t))
spath))
)
|