Skip to content
Snippets Groups Projects
Commit d4cd52ca authored by Bernardo Cardoso's avatar Bernardo Cardoso
Browse files

Make UDB calls non blocking by using goroutine and callbacks

parent 22799f1d
No related branches found
No related tags found
No related merge requests found
...@@ -191,8 +191,11 @@ func SetRateLimiting(limit int) { ...@@ -191,8 +191,11 @@ func SetRateLimiting(limit int) {
api.SetRateLimiting(uint32(limit)) api.SetRateLimiting(uint32(limit))
} }
func RegisterForUserDiscovery(emailAddress string) error { func RegisterForUserDiscovery(emailAddress string, callback func(error)) {
return api.RegisterForUserDiscovery(emailAddress) go func() {
err := api.RegisterForUserDiscovery(emailAddress)
callback(err)
}()
} }
type SearchResult struct { type SearchResult struct {
...@@ -200,13 +203,14 @@ type SearchResult struct { ...@@ -200,13 +203,14 @@ type SearchResult struct {
PublicKey []byte PublicKey []byte
} }
func SearchForUser(emailAddress string) (*SearchResult, error) { func SearchForUser(emailAddress string, callback func(SearchResult, error)) {
go func() {
searchedUser, key, err := api.SearchForUser(emailAddress) searchedUser, key, err := api.SearchForUser(emailAddress)
if err != nil { callback(SearchResult{
return nil, err ResultID: searchedUser.Bytes(),
} else { PublicKey: key},
return &SearchResult{ResultID: searchedUser.Bytes(), PublicKey: key}, nil err)
} }()
} }
// Parses a passed message. Allows a message to be aprsed using the interal parser // Parses a passed message. Allows a message to be aprsed using the interal parser
......
...@@ -297,8 +297,7 @@ var rootCmd = &cobra.Command{ ...@@ -297,8 +297,7 @@ var rootCmd = &cobra.Command{
// Handle sending to UDB // Handle sending to UDB
if *recipientId == *bots.UdbID { if *recipientId == *bots.UdbID {
grp := user.TheSession.GetGroup() parseUdbMessage(message)
fmt.Println(parseUdbMessage(message, grp))
} else { } else {
// Handle sending to any other destination // Handle sending to any other destination
wireOut := bindings.FormatTextMessage(message) wireOut := bindings.FormatTextMessage(message)
......
...@@ -10,12 +10,30 @@ import ( ...@@ -10,12 +10,30 @@ import (
"fmt" "fmt"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/bindings" "gitlab.com/elixxir/client/bindings"
"gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/large"
"strings" "strings"
) )
func handleSearchResults(result bindings.SearchResult, err error) {
if err != nil {
fmt.Printf("UDB search failed: %v\n", err.Error())
} else {
userIdText := large.NewIntFromBytes(result.ResultID).Text(10)
fmt.Printf("UDB search successful. Returned user %v, "+
"public key %q\n", userIdText, result.PublicKey)
}
}
func handleRegisterResult(err error) {
if err != nil {
fmt.Printf("UDB registration failed: %v\n", err.Error())
} else {
fmt.Printf("UDB registration successful.\n")
}
}
// Determines what UDB send function to call based on the text in the message // Determines what UDB send function to call based on the text in the message
func parseUdbMessage(msg string, grp *cyclic.Group) string { func parseUdbMessage(msg string) {
// Split the message on spaces // Split the message on spaces
args := strings.Fields(msg) args := strings.Fields(msg)
if len(args) < 3 { if len(args) < 3 {
...@@ -27,23 +45,10 @@ func parseUdbMessage(msg string, grp *cyclic.Group) string { ...@@ -27,23 +45,10 @@ func parseUdbMessage(msg string, grp *cyclic.Group) string {
keyword := args[0] keyword := args[0]
// Case-insensitive match the keyword to a command // Case-insensitive match the keyword to a command
if strings.EqualFold(keyword, "SEARCH") { if strings.EqualFold(keyword, "SEARCH") {
result, err := bindings.SearchForUser(args[2]) bindings.SearchForUser(args[2], handleSearchResults)
if err != nil {
return fmt.Sprintf("UDB search failed: %v", err.Error())
} else {
userIdText := grp.NewIntFromBytes(result.ResultID).Text(10)
return fmt.Sprintf("UDB search successful. Returned user %v, "+
"public key %q", userIdText, result.PublicKey)
}
} else if strings.EqualFold(keyword, "REGISTER") { } else if strings.EqualFold(keyword, "REGISTER") {
err := bindings.RegisterForUserDiscovery(args[2]) bindings.RegisterForUserDiscovery(args[2], handleRegisterResult)
if err != nil {
return fmt.Sprintf("UDB registration failed: %v", err.Error())
} else {
return "UDB registration successful."
}
} else { } else {
jww.ERROR.Printf("UDB command not recognized!") jww.ERROR.Printf("UDB command not recognized!")
} }
return ""
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment