chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

Using callbacks from inside the C api

Open lighth7015 opened this issue 5 years ago • 3 comments

Hi,

I am attempting to implement a deferred calling mechanism for an application I'm going to be writing-

Essentially, the flow goes like this-

  1. C code initializes and runs scheme connection script
  2. The connection script registers a list of callbacks with an intern'd C function
  3. The intern'd C function records the list of callbacks and stores them in a hash map for use later on.
  4. A while later, the application decides it wants to dispatch an event to the the connection script, so it invokes g_dispatch_event, which looks up the correct event and retrieves a pointer to its sexp.

However, upon invoking the sexp with the correct ctx, nothing happens. I know I'm doing something wrong, but I'm unsure what.

This is the snippet of code in question-

void g_dispatch_event(g_client_context client, kh_list_key_t state)
{	
	sexp_gc_var2(code, cref);
	khash_t(dispatcher)* dispatcher =client->dispatcher;
	
	sexp callback = kh_value(dispatcher, 
		kh_get(dispatcher, dispatcher, state));
	
	g_write_log("Address for event handler \"%s\" is %lp.", 
		state, callback );
	
	sexp_eval(client->ctx, callback, NULL);
}

Thanks in advance for the assistance!

lighth7015 avatar Mar 03 '19 08:03 lighth7015

What is callback? If it's a Scheme procedure you want to use sexp_apply:

sexp_apply(client->ctx, callback, SEXP_NULL);

ashinn avatar Mar 03 '19 15:03 ashinn

Yes, it is a scheme procedure; however sexp_apply doesn't even appear to be doing anything. Perhaps I'm passing in the callbacks incorrectly?

This is my scheme code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 																;;
;; Connection error messages											;;
;; 																;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (service-connect-failure)
	(dialog 90 0))

(define (service-connection-fail)
	(dialog 91 0))

(define (network-account-failure)
	(dialog 92 0))

(define (general-connect-failure)
	(dialog 92 0))

(define (service-connect-upgrade)
	(dialog 93 1 "Upgrade message"))

(define (service-connection-lost)
	(dialog 1 1 "Connection lost."))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 																;;
;; Connection error messages											;;
;; 																;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (service-connect-success)
	(dialog 30 2 "Connecting to the Network."))

(define (service-connection-init)
	(dialog 30 2 "Negotiating the Connection."))

(define (network-account-welcome)
	(dialog 30 2 "Connected to the Network."))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 																;;
;; Handle connection												;;
;; 																;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (service-connect-init)
	(dialog 12 0)
	(begin-connect 120
		(list ;; List of status resposnes the connection manager 
			 ;; can invoke.
			 
			 ;; Unable to resolve iv.intervue.net
			 "Can't Find Host"		service-connect-failure
			 
			 ;; For some odd reason, there aren't any SRV records
			 "Can't Auto-Discover"		service-connect-failure
			 
			 ;; The host isn't accepting TCP/IP connections.
			 "Connection Refused"		service-connect-failure
			 
			 ;; "No Route to Host"; give up connection attempt.
			 "Server Unreachable"		service-connection-fail
			 
			 ;; "No Route to Host"; give up connection attempt.
			 "Initializing Connection"	service-connection-init
			 
			 ;; Server hung up unexpectedly (or doesn't support our
			 ;; client configuration.)
			 "Service Unavailable"		service-connection-fail
			 
			 ;; TCP connection succeeded, so update the stat dialog
			 "Connection Ok"		service-connect-success
			 
			 ;; Negotiating security and protocol with the network
			 "Negotiate Connection"		service-connection-init
			 
			 ;; Okay, we're in like flynn!
			 "Account Auth Success"		network-account-welcome
			 
			 ;; The Global Catalog service didn't accept our 
			 ;; username/password combination.
			 "Account Auth Failure"		network-account-failure)
		
		general-connect-failure
	))

lighth7015 avatar Mar 03 '19 19:03 lighth7015

sexp_apply returns the result of its computation, including any errors. You need to inspect this result and print them out if an exception is returned.

ashinn avatar Mar 04 '19 14:03 ashinn