73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
-
+
-
+
|
pktsdir
mtrah
(mutex (make-mutex))
)
;; make it a global? Well, it is local to area module
(define *area-conndat* (make-area))
(define *area-info* (make-area))
(define *pktspec*
`((server (hostname . h)
(port . p)
(pid . i)
)
(data (hostname . h) ;; sender hostname
(port . p) ;; sender port
(ip . i) ;; sender ip
(ip . a) ;; sender ip
(hostkey . k) ;; sending host key - store info at server under this key
(servkey . s) ;; server key - this needs to match at server end or reject the msg
(format . f) ;; sb=serialized-base64, t=text, sx=sexpr, j=json
(data . d) ;; base64 encoded slln data
)))
(define (server:get-mtrah)
|
103
104
105
106
107
108
109
110
111
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
|
103
104
105
106
107
108
109
110
111
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
|
-
+
+
-
+
+
-
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
+
-
+
+
-
+
-
-
-
+
+
+
|
;; look for other servers
;; contact other servers and compile list of servers
;; there are two types of server
;; main servers - dashboards, runners and dedicated servers - need pkt
;; passive servers - test executers, step calls, list-runs - no pkt
;;
(define (server:start-nmsg #!optional (force-server-type #f))
(mutex-lock! (area-mutex *area-conndat*))
(mutex-lock! (area-mutex *area-info*))
(let* ((server-type (or force-server-type
(if (args:any? "-run" "-server")
'main
'passive)))
(port-num (portlogger:open-run-close portlogger:find-port))
(best-ip (server:get-my-best-address))
(area-conn (nmsg:start-server port-num))
;; (pktspec (area-pktspec *area-conndat*))
;; (pktspec (area-pktspec *area-info*))
(mtdir (or (server:get-mtrah)
(begin
(print "ERROR: megatest.config not found and MT_RUN_AREA_HOME is not set.")
#f)))
(pktdir (conc mtdir
"/.server-pkts")))
(if (not mtdir)
#f
(begin
(if (not (directory? pktdir))(create-directory pktdir))
;; server is started, now create pkt if needed
(print "Starting server in " server-type " mode")
(if (eq? server-type 'main)
(begin
(area-pktid-set! *area-conndat*
(area-pktid-set! *area-info*
(write-alist->pkt
pktdir
`((hostname . ,(get-host-name))
(ip . ,best-ip)
(port . ,port-num)
(pid . ,(current-process-id)))
pktspec: *pktspec*
ptype: 'server))
(area-pktfile-set! *area-conndat* (conc pktdir "/" (area-pktid *area-conndat*) ".pkt"))))
(area-pktfile-set! *area-info* (conc pktdir "/" (area-pktid *area-info*) ".pkt"))))
;; set all the area info in the
(area-pktsdir-set! *area-conndat* pktdir)
(area-mtrah-set! *area-conndat* mtdir)
(area-conn-set! *area-conndat* area-conn)
(area-port-set! *area-conndat* port-num)
(mutex-unlock! (area-mutex *area-conndat*))
#t))))
(area-pktsdir-set! *area-info* pktdir)
(area-mtrah-set! *area-info* mtdir)
(area-conn-set! *area-info* area-conn)
(area-port-set! *area-info* port-num)
(mutex-unlock! (area-mutex *area-info*))
area-conn))))
;; Call this to start the actual server
;;
;; start_server
;;
;; mode: '
;; handler: proc which takes pktrecieved as argument
;;
(define (server:launch mode handler)
(define (server:launch mode proc)
(let* ((start-time (current-seconds))
(rep (server:start-nmsg mode))
(last-msg (current-seconds))
(th1 (make-thread
(lambda ()
(let loop ()
(let ((pktdat (nn-recv rep)))
(let ((pktdat (nmsg:recv rep)))
(set! last-msg (current-seconds))
(print "received: " pktdat)
(if (not (eof-object? pktdat))
(begin
(proc pktdat)
(loop))))))
"message handler"))
(th2 (make-thread
(lambda ()
(let loop ()
(thread-sleep! 10)
(if (> (- (current-seconds) last-msg) 60) ;; timeout after 60 seconds
(begin
(print "Waited for 60 seconds and no messages, exiting now.")
(exit))
(loop)))))))
(thread-start! th1)
(thread-start! th2)
(thread-join th1)))
(thread-join! th1)))
(define (server:shutdown)
(let ((conn (area-conn *area-conndat*))
(pktf (area-pktfile *area-conndat*))
(port (area-port *area-conndat*)))
(let ((conn (area-conn *area-info*))
(pktf (area-pktfile *area-info*))
(port (area-port *area-info*)))
(if conn
(begin
(if pktf (delete-file* pktf))
(server:send-all "imshuttingdown")
(nmsg:close conn)
(portlogger:open-run-close portlogger:release-port port)))))
|
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
+
-
-
-
-
+
+
+
+
+
+
-
+
-
-
+
+
-
+
-
+
-
+
+
|
(D . "1549427032.0"))
;; srvpkt is the info for the server we wish to send the message to
;;
(define (server:send servpkt data dtype)
(let* ((port (alist-ref 'port servpkt))
(host (alist-ref 'hostname servpkt))
(ip (alist-ref 'ip servpkt))
(hkey (alist-ref 'Z servpkt))
(addr (conc host ":" port))
(myport (area-port *area-conndat*))
(myhost (area-myaddr *area-conndat*))
(mykey (area-pktid *area-conndat*))
(addr (conc (or ip host) ":" port)) ;; fall back to host if ip not provided
(myport (area-port *area-info*))
(myhost (area-myaddr *area-info*))
(mykey (area-pktid *area-info*))
(msg (alist->pkt `((hostname . ,myhost)
(port . ,myport)
(servkey . ,hkey) ;; server looks at this to ensure message is for them
(hostkey . ,mykey)
(format . ,dtype) ;; formating of the message
(data . ,data))
*pktspec*)))
(if (and port host)
(begin
(print "sending " msg " to " addr)
(nmsg:open-send-receive addr msg)
(nmsg:open-send-receive addr msg))
#f)))
;; is the server alive?
;;
(define (server:ping servpkt)
(let* ((start-time (current-milliseconds))
(res (server:send servpkt "ping" "t")))
(cons (- (current-milliseconds)
(equal? res "got ping")))))
(cons (- (current-milliseconds) start-time)
(equal? res "got ping"))))
;; look up all pkts and get the server id (the hash), port, host/ip
;; store this info in the global struct *area-conndat*
;; store this info in the global struct *area-info*
;;
(define (server:get-all)
;; readll all pkts
;; foreach pkt; if it isn't me ping the server; if alive, add to hosts hash, else rm the pkt
(let ((all-pkts (server:get-all-server-pkts *area-conn*)))
(let ((all-pkts (server:get-all-server-pkts *area-info*)))
(for-each
(lambda (servpkt)
(server:ping servpkt))
(let* ((res (server:ping servpkt)))
(print "Got " res " from server " servpkt)))
all-pkts)))
;; send out an "I'm about to exit notice to all known servers"
;;
(define (server:imminent-death)
'())
|