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
|
;;(use srfi-18 extras tcp s11n srfi-1 posix regex srfi-69 hostinfo md5
;; message-digest matchable spiffy uri-common intarweb http-client
;; spiffy-request-vars uri-common intarweb directory-utils)
(declare (unit clientmod))
(declare (uses servermod))
(declare (uses artifacts))
(module clientmod
*
(import scheme
posix
data-structures
srfi-18
typed-records
artifacts
servermod
)
(defstruct con ;; client connection
(hdir #f)
(obj-to-str #f)
(host #f)
(pid #f)
(sdat #f) ;; server artifact data
)
(define (client:find-server areapath)
(let* ((sdir (conc areapath"/.server"))
(sarfs (glob (conc sdir"/*.artifact")))) ;; server artifacts
(if (null? sarfs)
(begin
(server:launch areapath)
(thread-sleep! 1)
(client:find-server areapath))
(let* ((sarf (car sarfs))
(sdat (read-artifact->alist sarf))
(hdir (alist-ref 'd sdat)))
(make-con hdir: hdir sdat: sdat)))))
(define (client:send-receive con cmd params)
(let* ((obj->string (con-obj-to-str con))
(arf `((c . ,cmd)
(p . ,(obj->string params))
(h . ,(con-host con))
(i . ,(con-pid con))))
(hdir (con-hdir con))
(uuid (write-alist->artifact hdir arf ptype: 'Q)))
;; wait for a response here
#f
))
)
|
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
|
>
>
>
>
>
>
>
>
|
>
>
<
>
>
>
>
>
|
|
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
|
;;(use srfi-18 extras tcp s11n srfi-1 posix regex srfi-69 hostinfo md5
;; message-digest matchable spiffy uri-common intarweb http-client
;; spiffy-request-vars uri-common intarweb directory-utils)
(declare (unit clientmod))
(declare (uses servermod))
(declare (uses artifacts))
(declare (uses debugprint))
(module clientmod
*
(import scheme
chicken
posix
data-structures
srfi-18
typed-records
artifacts
servermod
debugprint
)
(defstruct con ;; client connection
(hdir #f) ;; this is the directory sdir/serverhost.serverpid
(sdir #f)
(obj-to-str #f)
(str-to-obj #f)
(host #f)
(pid #f)
(sdat #f) ;; server artifact data
(areapath #f)
)
(define *my-client-signature* #f)
(define (client:find-server areapath)
(let* ((sdir (conc areapath"/.server"))
(sarfs (glob (conc sdir"/*.artifact")))) ;; server artifacts
(if (null? sarfs)
(begin
(server:launch areapath)
(thread-sleep! 1)
(client:find-server areapath))
(let* ((sarf (car sarfs))
(sdat (read-artifact->alist sarf))
(hdir (alist-ref 'd sdat)))
(make-con hdir: hdir sdir: sdir sdat: sdat)))))
;; move this into artifacts
;; find the artifact with key set to val
;;
(define (client:find-artifact arfs key val)
(let loop ((rem arfs))
(if (null? rem) ;; didn't find a match
#f
(let* ((arf (car rem))
(adat (read-artifact->alist arf))
(val-found (alist-ref key adat)))
(if (equal? val-found val)
(cons (cons 'path arf) adat) ;; return the artifact as alist anotated with 'path
(loop (cdr rem)))))))
(define (client:send-receive con cmd params)
(let* ((obj->string (con-obj-to-str con))
(string->obj (con-str-to-obj con))
(arf `((c . ,cmd)
(p . ,(obj->string params))
(h . ,(con-host con)) ;; tells server where to put response
(i . ,(con-pid con))));; and is where this client looks
(hdir (con-hdir con))
(sdir (con-sdir con))
(uuid (write-alist->artifact hdir arf ptype: 'Q))
(respdir (conc sdir"/"(con-host con)"."(con-pid con)"/responses")))
(let loop ((start (current-milliseconds)))
(let* ((arfs (glob (conc respdir"/*.artifact")))
(res (client:find-artifact arfs 'P uuid)))
(if res ;; we found our response!
(let ((arf (alist-ref 'path res))
(rstr (alist-ref 'r res)))
(delete-file arf) ;; done with it, future - move to archive area
(string->obj rstr))
(begin ;; no response yet, look again in 500ms
(thread-sleep! 0.5)
(loop (current-milliseconds))))))))
;; client:get-signature
(define (client:get-signature)
(if *my-client-signature* *my-client-signature*
(let ((sig (conc (get-host-name) " " (current-process-id))))
(set! *my-client-signature* sig)
*my-client-signature*)))
)
|