Megatest

Diff
Login

Differences From Artifact [42b648b50c]:

To Artifact [e4b8345913]:


122
123
124
125
126
127
128
129

130
131
132
133
134
135
136
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136







-
+








;; This is the basic setup command. Must always be
;; called before connecting to a db using connect.
;;
;; find or become the captain
;; setup and return a ulex object
;;
(define (find-or-setup-captain udata)
(define (find-or-setup-captain udata #!optional (tries 0))
  ;; see if we already have a captain and if the lease is ok
  (if (and (udat-captain-address udata)
	   (udat-captain-port    udata)
	   (< (current-seconds) (udat-captain-lease udata)))
      udata
      (let* ((cpkts (get-all-captain-pkts udata)) ;; read captain pkts
	     (captn (get-winning-pkt cpkts)))
147
148
149
150
151
152
153

154
155
156
157







158
159
160
161
162
163
164
147
148
149
150
151
152
153
154




155
156
157
158
159
160
161
162
163
164
165
166
167
168







+
-
-
-
-
+
+
+
+
+
+
+







	      (udat-captain-lease-set!   udata (+ (current-seconds) 10))
	      (let-values (((success pingtime)(ping udata (conc ipaddr ":" port))))
		(if success
		    udata
		    (begin
		      (print "Found unreachable captain at " ipaddr ":" port ", removing pkt")
		      (remove-captain-pkt udata captn)
		      (if (< tries 20)
		      (find-or-setup-captain udata))))
	      (begin
		(setup-as-captain udata)  ;; this saves the thread to captain-thread and starts the thread
		(find-or-setup-captain udata)))))))
			  (find-or-setup-captain udata (+ tries 1))
			  #f)))))
	    (begin
	      (setup-as-captain udata)  ;; this saves the thread to captain-thread and starts the thread
	      (if (< tries 20)
		  (find-or-setup-captain udata (+ tries 1))
		  #f))))))

;; connect to a specific dbfile
;;   - if already connected - return the dbowner host-port
;;   - ask the captain who to talk to for this db
;;   - put the entry in the dbowners hash as dbfile => host-port
;;
(define (connect udata dbfname dbtype)
367
368
369
370
371
372
373
374

375
376
377
378
379
380
381
371
372
373
374
375
376
377

378
379
380
381
382
383
384
385







-
+







      (get-host-name))                                          ;; no interfaces?
     ((eq? (length all-my-addresses) 1)
      (car all-my-addresses))                      ;; only one to choose from, just go with it
     
     (else
      (car (sort all-my-addresses ip-pref-less?)))
     ;; (else 
     ;;  (ip->string (car (filter (lambda (x)                      ;; take any but 127.
     ;;  (ip->strin g(car (filter (lambda (x)                      ;; take any but 127.
     ;;    			 (not (eq? (u8vector-ref x 0) 127)))
     ;;    		       all-my-addresses))))

     )))

(define (get-all-ips-sorted)
  (sort (get-all-ips) ip-pref-less?))
538
539
540
541
542
543
544
545

546
547
548
549
550
551
552
553








554
555
556
557
558





559

560


561
562
563
564
565






566
567
568
569
570
571
572
542
543
544
545
546
547
548

549
550
551
552





553
554
555
556
557
558
559
560

561
562
563
564
565
566
567
568
569

570
571
572
573





574
575
576
577
578
579
580
581
582
583
584
585
586







-
+



-
-
-
-
-
+
+
+
+
+
+
+
+
-




+
+
+
+
+
-
+

+
+
-
-
-
-
-
+
+
+
+
+
+







;; create a tcp listener and return a populated udat struct with
;; my port, address, hostname, pid etc.
;; return #f if fail to find a port to allocate.
;;
;;  if udata-in is #f create the record
;;  if there is already a serv-listener return the udata
;;
(define (start-server-find-port udata-in #!optional (port 4242))
(define (start-server-find-port udata-in #!optional (port 4242)(tries 0))
  (let ((udata (or udata-in (make-udat))))
    (if (udat-serv-listener udata) ;; TODO - add check that the listener is alive and ready?
	udata
	(handle-exceptions
	    exn
	  (if (< port 65535)
	      (start-server-find-port udata (+ port 1))
	      #f)
	(let ((res (connect-server udata port)))
	  (or res
	      (begin
		;; (print "Could not connect to " port)
		(if (and (< port 65535)
			 (< tries 5000)) ;; make this number bigger when things are working
		    (start-server-find-port udata (+ port 1)(+ tries 1))
		    #f)))))))
	  (connect-server udata port)))))

(define (connect-server udata port)
  ;; (tcp-listener-socket LISTENER)(socket-name so)
  ;; sockaddr-address, sockaddr-port, sockaddr->string
  (let* ((tlsn (handle-exceptions
		   exn
		   (begin
		     ;; (print "unable to connect to " port ", exn=" exn)
		     #f)
  (let* ((tlsn (tcp-listen port 1000 #f)) ;; (tcp-listen TCPPORT [BACKLOG [HOST]])
		 (tcp-listen port 1000 #f))) ;; (tcp-listen TCPPORT [BACKLOG [HOST]])
	 (addr (get-my-best-address))) ;; (hostinfo-addresses (host-information (current-hostname)))
    (if tlsn
	(begin
    (udat-my-address-set!    udata addr)
    (udat-my-port-set!       udata port)
    (udat-my-hostname-set!   udata (get-host-name))
    (udat-serv-listener-set! udata tlsn)
    udata))
	  (udat-my-address-set!    udata addr)
	  (udat-my-port-set!       udata port)
	  (udat-my-hostname-set!   udata (get-host-name))
	  (udat-serv-listener-set! udata tlsn)
	  udata)
	#f)))

(define (get-peer-dat udata host-port #!optional (hostname #f)(pid #f))
  (let* ((pdat (or (udat-get-peer udata host-port)
		   (handle-exceptions ;; ERROR - MAKE THIS EXCEPTION HANDLER MORE SPECIFIC
		    exn
		    #f
		    (let ((npdat (make-peer addr-port: host-port)))