1. Tim van der Linden
  2. json-rpc

Commits

Limburgse Natuurgidsen  committed bb98aa9

Working basic thread message dispatcher.

  • Participants
  • Parent commits 3bfe4c6
  • Branches default

Comments (0)

Files changed (1)

File json-rpc-client.scm

View file
  • Ignore whitespace
 ; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 ; POSSIBILITY OF SUCH DAMAGE.
 
-;;; --> Symbol hack -> Make method a symbol for notification, string for no notification
-;;; --> Create worker thread with read-json loop for catching input (dispatcher)
-;;; --> Dispatch notifications from server and responses to different mailboxes
-
 (module json-rpc-client
   (json-rpc-server)
 
 	((not (output-port? output)) (server-setup-arguments-error "output port" "ouput-port" (get-type output)))
 	((not (is-valid-version? version)) (server-setup-arguments-error "version" "2.0" version))
 	(else
-	 (thread-start! (listener-thread input (current-thread))) ;Start the looping listener thread
+	 (thread-start! (listener-thread input (current-thread))) ; Start the looping listener thread
 	 (lambda (method . params)
 	   (cond ((not (is-valid-method? method)) (server-setup-data-error "method" "can only be a string or a symbol."))
 		 ((not (are-valid-params? params)) (server-setup-data-error "params" "can only be a vector or an alist."))
 						    (if (null? params)
 							'()
 							(cons 'params (build-params params)))
-						    (if (symbol? method) ;Hack to make this request a notification or not
+						    (if (symbol? method) ; Hack to make this request a notification or not
 							'()
-							(cons 'id "1")))) ;ID is hardcoded - can't handle more then one request at a time...or can we?
+							(cons 'id "1")))) ; ID is hardcoded - can't handle more then one request at a time...or can we?
 				output)))))))
 
 ; Helper for building a vector or alist from the parameters if present
 
 ; Check if the params are a list as defined in the spec
 (define (are-valid-params? params)
-  (list? params)) ;Assumptions? Don't know if this check is enough (check for null (is also a list) or list)
+  (list? params)) ; Assumptions? Don't know if this check is enough (check for null (is also a list) or list)
 
 ; Check if the version is correctly formatted as defined in the spec
 (define (is-valid-version? version)
   (write-json request output))
 
 ; Create the looping listener thread which will dispatch messages to various thread-mailboxes
-;; Keep reading the JSON sent back from the server, if the reponse contains an  ID and this ID is 
-;; equal to the sender, dispatch a message to the current threads mailbox. 
-;; Otherwise send the message to the public threads mailbox. 
+;; Keep reading the JSON sent back from the server, if the reponse contains an ID then this is a 
+;; repsonse to the request sent so dispatch a message to the current threads mailbox. 
+;; Otherwise send the message to the public threads mailbox (?). 
 (define (listener-thread input current-thread) 
   (make-thread (lambda () 
 		 (loop
-		  (if 
-		   (alist-ref 'id (read-json input #f))
-		   (thread-send current-thread "There is a response"))))
+		  (let 
+		      ; Note that we have to set Medea to not consume trailing whitespace for otherwise Medea will swallow
+		      ; the first character of the *next* json string waiting in the port and thus render that string useless.
+		      ((data (read-json input consume-trailing-whitespace: #f)))
+		    (if 
+		     (alist-ref 'id data)
+		     (thread-send current-thread data)))))
 	       'listener))
 
 )