Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
d4cd52ca
Commit
d4cd52ca
authored
6 years ago
by
Bernardo Cardoso
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
bindings/client.go
+13
-9
13 additions, 9 deletions
bindings/client.go
cmd/root.go
+1
-2
1 addition, 2 deletions
cmd/root.go
cmd/udb.go
+22
-17
22 additions, 17 deletions
cmd/udb.go
with
36 additions
and
28 deletions
bindings/client.go
+
13
−
9
View file @
d4cd52ca
...
...
@@ -191,8 +191,11 @@ func SetRateLimiting(limit int) {
api
.
SetRateLimiting
(
uint32
(
limit
))
}
func
RegisterForUserDiscovery
(
emailAddress
string
)
error
{
return
api
.
RegisterForUserDiscovery
(
emailAddress
)
func
RegisterForUserDiscovery
(
emailAddress
string
,
callback
func
(
error
))
{
go
func
()
{
err
:=
api
.
RegisterForUserDiscovery
(
emailAddress
)
callback
(
err
)
}()
}
type
SearchResult
struct
{
...
...
@@ -200,13 +203,14 @@ type SearchResult struct {
PublicKey
[]
byte
}
func
SearchForUser
(
emailAddress
string
)
(
*
SearchResult
,
error
)
{
func
SearchForUser
(
emailAddress
string
,
callback
func
(
SearchResult
,
error
))
{
go
func
()
{
searchedUser
,
key
,
err
:=
api
.
SearchForUser
(
emailAddress
)
if
err
!=
nil
{
return
nil
,
err
}
else
{
return
&
SearchResult
{
ResultID
:
searchedUser
.
Bytes
(),
PublicKey
:
key
},
nil
}
callback
(
SearchResult
{
ResultID
:
searchedUser
.
Bytes
(),
PublicKey
:
key
},
err
)
}
()
}
// Parses a passed message. Allows a message to be aprsed using the interal parser
...
...
This diff is collapsed.
Click to expand it.
cmd/root.go
+
1
−
2
View file @
d4cd52ca
...
...
@@ -297,8 +297,7 @@ var rootCmd = &cobra.Command{
// Handle sending to UDB
if
*
recipientId
==
*
bots
.
UdbID
{
grp
:=
user
.
TheSession
.
GetGroup
()
fmt
.
Println
(
parseUdbMessage
(
message
,
grp
))
parseUdbMessage
(
message
)
}
else
{
// Handle sending to any other destination
wireOut
:=
bindings
.
FormatTextMessage
(
message
)
...
...
This diff is collapsed.
Click to expand it.
cmd/udb.go
+
22
−
17
View file @
d4cd52ca
...
...
@@ -10,12 +10,30 @@ import (
"fmt"
jww
"github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/bindings"
"gitlab.com/elixxir/crypto/
cyclic
"
"gitlab.com/elixxir/crypto/
large
"
"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
func
parseUdbMessage
(
msg
string
,
grp
*
cyclic
.
Group
)
string
{
func
parseUdbMessage
(
msg
string
)
{
// Split the message on spaces
args
:=
strings
.
Fields
(
msg
)
if
len
(
args
)
<
3
{
...
...
@@ -27,23 +45,10 @@ func parseUdbMessage(msg string, grp *cyclic.Group) string {
keyword
:=
args
[
0
]
// Case-insensitive match the keyword to a command
if
strings
.
EqualFold
(
keyword
,
"SEARCH"
)
{
result
,
err
:=
bindings
.
SearchForUser
(
args
[
2
])
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
)
}
bindings
.
SearchForUser
(
args
[
2
],
handleSearchResults
)
}
else
if
strings
.
EqualFold
(
keyword
,
"REGISTER"
)
{
err
:=
bindings
.
RegisterForUserDiscovery
(
args
[
2
])
if
err
!=
nil
{
return
fmt
.
Sprintf
(
"UDB registration failed: %v"
,
err
.
Error
())
}
else
{
return
"UDB registration successful."
}
bindings
.
RegisterForUserDiscovery
(
args
[
2
],
handleRegisterResult
)
}
else
{
jww
.
ERROR
.
Printf
(
"UDB command not recognized!"
)
}
return
""
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment