add getmessage and nummessages

This commit is contained in:
erinn 2021-01-07 11:38:58 -08:00
parent 159b7daf43
commit 838df3ed90
1 changed files with 30 additions and 11 deletions

41
lib.go
View File

@ -69,7 +69,7 @@ func StartCwtch(dir_c *C.char, len C.int) {
event.NewGetValMessageFromPeer, event.NewGetValMessageFromPeer,
event.PeerStateChange, event.PeerStateChange,
} }
newApp.LoadProfiles("awesome") newApp.LoadProfiles("be gay do crime")
newApp.LaunchPeers() newApp.LaunchPeers()
application = newApp application = newApp
log.Infof("Providing Handle %v", id) log.Infof("Providing Handle %v", id)
@ -78,8 +78,8 @@ func StartCwtch(dir_c *C.char, len C.int) {
//export ACNEvents //export ACNEvents
func ACNEvents() *C.char { func ACNEvents() *C.char {
select { select {
case event := <- acnQueue.OutChan(): case myevent := <- acnQueue.OutChan():
return C.CString(fmt.Sprintf("%v", event)) return C.CString(fmt.Sprintf("%v", myevent))
default: default:
return C.CString("") return C.CString("")
} }
@ -108,20 +108,20 @@ func GetContacts(onion_ptr *C.char, onion_len C.int) *C.char {
onion := C.GoStringN(onion_ptr, onion_len) onion := C.GoStringN(onion_ptr, onion_len)
log.Infof("Get Contacts for %v", onion) log.Infof("Get Contacts for %v", onion)
peer := application.GetPeer(onion) mypeer := application.GetPeer(onion)
contactEventsQueue = event.NewQueue() contactEventsQueue = event.NewQueue()
application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue) application.GetEventBus(onion).Subscribe(event.PeerStateChange, contactEventsQueue)
var contacts []Contact var contacts []Contact
for _,contact := range peer.GetContacts() { for _,contact := range mypeer.GetContacts() {
contactInfo := peer.GetContact(contact) contactInfo := mypeer.GetContact(contact)
log.Infof("contactInfo %v", contactInfo) log.Infof("contactInfo %v", contactInfo)
contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State}) contacts = append(contacts, Contact{Name: contactInfo.Name, Onion: contactInfo.Onion, Status: contactInfo.State})
} }
byte,_ := json.Marshal(contacts) bytes,_ := json.Marshal(contacts)
return C.CString(string(byte)) return C.CString(string(bytes))
} }
@ -138,13 +138,32 @@ func SelectProfile(onion_ptr *C.char, onion_len C.int) *C.char {
//export ContactEvents //export ContactEvents
func ContactEvents() *C.char { func ContactEvents() *C.char {
select { select {
case event := <- contactEventsQueue.OutChan(): case myevent := <- contactEventsQueue.OutChan():
return C.CString(fmt.Sprintf("%v", event)) return C.CString(fmt.Sprintf("%v", myevent))
default: default:
return C.CString("") 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))
}
func main() { func main() {
} }