move callbacks into python class, simplify demobot
This commit is contained in:
parent
260386bcac
commit
05020f126e
4 changed files with 103 additions and 72 deletions
|
|
@ -16,15 +16,15 @@ import (
|
|||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
typedef void (*on_event_handler_ptr) (char*);
|
||||
typedef void (*on_message_handler_ptr) (char*);
|
||||
typedef void (*on_event_handler_ptr) (char*, void*);
|
||||
typedef void (*on_message_handler_ptr) (char*, void*);
|
||||
|
||||
static inline void call_c_on_event_handler(on_event_handler_ptr ptr, char* jsonStr) {
|
||||
(ptr)(jsonStr);
|
||||
static inline void call_c_on_event_handler(on_event_handler_ptr ptr, char* jsonStr, void* pobj) {
|
||||
(ptr)(jsonStr, pobj);
|
||||
}
|
||||
|
||||
static inline void call_c_on_message_handler(on_message_handler_ptr ptr, char* jsonStr) {
|
||||
(ptr)(jsonStr);
|
||||
static inline void call_c_on_message_handler(on_message_handler_ptr ptr, char* jsonStr, void* pobj) {
|
||||
(ptr)(jsonStr, pobj);
|
||||
}
|
||||
|
||||
*/
|
||||
|
|
@ -35,28 +35,32 @@ matrix client with c callback
|
|||
*/
|
||||
type CBClient struct {
|
||||
*mxclient.MXClient
|
||||
on_event_handler C.on_event_handler_ptr
|
||||
on_message_handler C.on_message_handler_ptr
|
||||
on_event_handler C.on_event_handler_ptr
|
||||
on_event_handler_pobj unsafe.Pointer
|
||||
on_message_handler C.on_message_handler_ptr
|
||||
on_message_handler_pobj unsafe.Pointer
|
||||
}
|
||||
|
||||
func (cli *CBClient) OnEvent(s string) {
|
||||
cStr := C.CString(s)
|
||||
defer C.free(unsafe.Pointer(cStr))
|
||||
C.call_c_on_event_handler(cli.on_event_handler, cStr)
|
||||
C.call_c_on_event_handler(cli.on_event_handler, cStr, cli.on_event_handler_pobj)
|
||||
}
|
||||
|
||||
func (cli *CBClient) OnMessage(s string) {
|
||||
cStr := C.CString(s)
|
||||
defer C.free(unsafe.Pointer(cStr))
|
||||
C.call_c_on_message_handler(cli.on_message_handler, cStr)
|
||||
C.call_c_on_message_handler(cli.on_message_handler, cStr, cli.on_message_handler_pobj)
|
||||
}
|
||||
|
||||
func (cli *CBClient) Set_on_event_handler(fn C.on_event_handler_ptr) {
|
||||
func (cli *CBClient) Set_on_event_handler(fn C.on_event_handler_ptr, pobj unsafe.Pointer) {
|
||||
cli.on_event_handler = fn
|
||||
cli.on_event_handler_pobj = pobj
|
||||
}
|
||||
|
||||
func (cli *CBClient) Set_on_message_handler(fn C.on_message_handler_ptr) {
|
||||
func (cli *CBClient) Set_on_message_handler(fn C.on_message_handler_ptr, pobj unsafe.Pointer) {
|
||||
cli.on_message_handler = fn
|
||||
cli.on_message_handler_pobj = pobj
|
||||
}
|
||||
|
||||
// NewCClient creates a new Matrix Client ready for syncing
|
||||
|
|
@ -65,7 +69,7 @@ func NewCBClient(homeserverURL string, userID id.UserID, accessToken string) (*C
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CBClient{client, nil, nil}, nil
|
||||
return &CBClient{client, nil, nil, nil, nil}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -179,7 +183,7 @@ func apiv0_createclient(storage_path *C.char, url *C.char, userID *C.char, acces
|
|||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
client := &CBClient{mxclient, nil, nil}
|
||||
client := &CBClient{mxclient, nil, nil, nil, nil}
|
||||
cclients = append(cclients, client)
|
||||
return C.CString(fmt.Sprintf("{ \"id:\"SUCESS. ID=%d\n", len(cclients)))
|
||||
}
|
||||
|
|
@ -190,7 +194,7 @@ func apiv0_createclient_pass(mxpassfile_path *C.char, storage_path *C.char, url
|
|||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
client := &CBClient{mxclient, nil, nil}
|
||||
client := &CBClient{mxclient, nil, nil, nil, nil}
|
||||
mxclient.OnEvent = client.OnEvent
|
||||
mxclient.OnMessage = client.OnMessage
|
||||
cclients = append(cclients, client)
|
||||
|
|
@ -203,24 +207,24 @@ func apiv0_createclient_pass(mxpassfile_path *C.char, storage_path *C.char, url
|
|||
}
|
||||
|
||||
//export apiv0_set_on_event_handler
|
||||
func apiv0_set_on_event_handler(cid C.int, fn C.on_event_handler_ptr) *C.char {
|
||||
func apiv0_set_on_event_handler(cid C.int, fn C.on_event_handler_ptr, pobj unsafe.Pointer) *C.char {
|
||||
cli, err := getClient(int(cid))
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
||||
cli.Set_on_event_handler(fn)
|
||||
cli.Set_on_event_handler(fn, pobj)
|
||||
return C.CString("SUCCESS.")
|
||||
}
|
||||
|
||||
//export apiv0_set_on_message_handler
|
||||
func apiv0_set_on_message_handler(cid C.int, fn C.on_message_handler_ptr) *C.char {
|
||||
func apiv0_set_on_message_handler(cid C.int, fn C.on_message_handler_ptr, pobj unsafe.Pointer) *C.char {
|
||||
cli, err := getClient(int(cid))
|
||||
if err != nil {
|
||||
return C.CString(fmt.Sprintf("ERR: %v", err))
|
||||
}
|
||||
|
||||
cli.Set_on_message_handler(fn)
|
||||
cli.Set_on_message_handler(fn, pobj)
|
||||
return C.CString("SUCCESS.")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue