Merge branch 'trunk' of git.openprivacy.ca:flutter/libcwtch-go into trunk

This commit is contained in:
Dan Ballard 2021-01-09 16:37:28 -08:00
commit 8e13a87a78
1 changed files with 31 additions and 13 deletions

44
lib.go
View File

@ -1,5 +1,5 @@
package cwtchh
//package main
//package cwtch
package main
import "C"
import (
@ -71,7 +71,6 @@ func StartCwtch(dir_c *C.char, len C.int) {
event.PeerStateChange,
}
newApp.LoadProfiles("be gay do crime")
//newApp.LoadProfiles("awesome")
newApp.LaunchPeers()
application = newApp
log.Infof("Providing Handle %v", id)
@ -80,8 +79,8 @@ func StartCwtch(dir_c *C.char, len C.int) {
//export ACNEvents
func ACNEvents() *C.char {
select {
case event := <- acnQueue.OutChan():
return C.CString(fmt.Sprintf("%v", event))
case myevent := <- acnQueue.OutChan():
return C.CString(fmt.Sprintf("%v", myevent))
default:
return C.CString("")
}
@ -110,20 +109,20 @@ func GetContacts(onion_ptr *C.char, onion_len C.int) *C.char {
onion := C.GoStringN(onion_ptr, onion_len)
log.Infof("Get Contacts for %v", onion)
peer := application.GetPeer(onion)
mypeer := application.GetPeer(onion)
contactEventsQueue = event.NewQueue()
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
var contacts []Contact
for _,contact := range peer.GetContacts() {
contactInfo := peer.GetContact(contact)
for _,contact := range mypeer.GetContacts() {
contactInfo := mypeer.GetContact(contact)
log.Infof("contactInfo %v", contactInfo)
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State})
}
byte,_ := json.Marshal(contacts)
return C.CString(string(byte))
bytes,_ := json.Marshal(contacts)
return C.CString(string(bytes))
}
@ -140,12 +139,31 @@ func SelectProfile(onion_ptr *C.char, onion_len C.int) *C.char {
//export ContactEvents
func ContactEvents() *C.char {
select {
case event := <- contactEventsQueue.OutChan():
return C.CString(fmt.Sprintf("%v", event))
case myevent := <- contactEventsQueue.OutChan():
return C.CString(fmt.Sprintf("%v", myevent))
default:
return C.CString("")
}
}
//export NumMessages
func NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n C.int) {
profile := C.GoStringN(profile_ptr, profile_len)
handle := C.GoStringN(handle_ptr, handle_len)
n = C.int(len(application.GetPeer(profile).GetContact(handle).Timeline.Messages))
log.Infof("NumMessagse(%s, %s) = %d", profile, handle, n)
return
}
//export GetMessage
func GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char {
profile := C.GoStringN(profile_ptr, profile_len)
handle := C.GoStringN(handle_ptr, handle_len)
message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index]
bytes,_ := json.Marshal(message)
log.Infof("GetMessage(%s, %s, %d) = %s", profile, handle, message_index, string(bytes))
return C.CString(string(bytes))
}
// Leave as is, needed by ffi
//func main() {}
func main() {}