(import tcp-server format (chicken random) (chicken tcp) (chicken io) (chicken string) (prefix sqlite3 sqlite3:) sql-de-lite srfi-18 simple-exceptions mailbox s11n)
(let* ((work-mailbox (make-mailbox))
(notify-mailbox (make-mailbox))
(th1 (make-thread (lambda ()
((make-tcp-server
(tcp-listen myport)
(lambda ()
(let* ((db (sqlite3:open-database "test.db"))
(rec-data (deserialize)))
(sqlite3:set-busy-handler! db (sqlite3:make-busy-timeout 136000))
;;(exec (sql db "INSERT INTO entries (received,send) VALUES (?,?);") "something" (conc "Server One Response: " "something else"))
(sqlite3:execute db "INSERT INTO entries (received,send) VALUES (?,?);" "something" (conc "Server One Response: " "something else"))
(mailbox-send! work-mailbox rec-data)
(format (current-error-port) (conc rec-data))
(write-line (conc "Response to: " (conc rec-data)))
;;(close-database db)
)))
#t))
"receive"))
(th2 (make-thread (lambda ()
(print "Jeff is here")
(let loop ((entries 0))
(thread-sleep! 0.01)
(print "Preparding to send entries" entries)
(handle-exceptions exn (begin (print "Had an issue: " (message exn))(thread-sleep! 10))
(define-values (i o) (tcp-connect "localhost" yourport))
(serialize (list "localhost:6505" mymessage (random-bytes) (random-bytes) (random-bytes) (random-bytes)) o)
(print (read-line i))
(close-input-port i)
(close-output-port o))
(loop (+ entries 1)))) "send"))
(th3 (make-thread (lambda ()
(print "In mailbox thread")
(let loop2 ((entries2 0))
(print "Processing: " (mailbox-receive! work-mailbox))
(thread-sleep! (* 10 (pseudo-random-real)))
(mailbox-send! notify-mailbox (list 'ack))
(loop2 1))) "processing"))
(th4 (make-thread (lambda ()
(print "In notify-mailbox thread")
(let loop3 ((entries3 0))
(print "Notifying: " (mailbox-receive! notify-mailbox))
(handle-exceptions exn (begin (print "Had an issue: " (message exn))(thread-sleep! 10))
(define-values (i o) (tcp-connect "localhost" yourport))
(serialize (list 'ack mymessage) o)
(print (read-line i))
(close-input-port i)
(close-output-port o))
;;(thread-sleep! 1)
(loop3 1))) "notify"))
)
(thread-start! th1)
(thread-start! th2)
(thread-start! th3)
(thread-start! th4)
(thread-join! th2)
)
(print "Done here")