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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
6e81b432
Commit
6e81b432
authored
Oct 3, 2020
by
Richard T. Carback III
Browse files
Options
Downloads
Patches
Plain Diff
Add basic listener and minor refactoring
parent
5ddf09b6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmd/root.go
+52
-15
52 additions, 15 deletions
cmd/root.go
with
52 additions
and
15 deletions
cmd/root.go
+
52
−
15
View file @
6e81b432
...
@@ -15,7 +15,9 @@ import (
...
@@ -15,7 +15,9 @@ import (
jww
"github.com/spf13/jwalterweatherman"
jww
"github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/spf13/viper"
"gitlab.com/elixxir/client/api"
"gitlab.com/elixxir/client/api"
"gitlab.com/elixxir/client/interfaces/message"
"gitlab.com/elixxir/client/interfaces/params"
"gitlab.com/elixxir/client/interfaces/params"
"gitlab.com/elixxir/client/switchboard"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/id"
"io/ioutil"
"io/ioutil"
"os"
"os"
...
@@ -126,6 +128,13 @@ var rootCmd = &cobra.Command{
...
@@ -126,6 +128,13 @@ var rootCmd = &cobra.Command{
user
:=
client
.
GetUser
()
user
:=
client
.
GetUser
()
jww
.
INFO
.
Printf
(
"%s"
,
user
.
ID
)
jww
.
INFO
.
Printf
(
"%s"
,
user
.
ID
)
// Set up reception handler
swboard
:=
client
.
GetSwitchboard
()
recvCh
:=
make
(
chan
message
.
Receive
,
10
)
listenerID
:=
swboard
.
RegisterChannel
(
"raw"
,
switchboard
.
AnyUser
(),
message
.
Raw
,
recvCh
)
jww
.
INFO
.
Printf
(
"Message ListenerID: %v"
,
listenerID
)
err
:=
client
.
StartNetworkFollower
()
err
:=
client
.
StartNetworkFollower
()
if
err
!=
nil
{
if
err
!=
nil
{
jww
.
FATAL
.
Panicf
(
"%+v"
,
err
)
jww
.
FATAL
.
Panicf
(
"%+v"
,
err
)
...
@@ -134,23 +143,12 @@ var rootCmd = &cobra.Command{
...
@@ -134,23 +143,12 @@ var rootCmd = &cobra.Command{
// Wait until connected or crash on timeout
// Wait until connected or crash on timeout
connected
:=
make
(
chan
bool
,
10
)
connected
:=
make
(
chan
bool
,
10
)
client
.
GetHealth
()
.
AddChannel
(
connected
)
client
.
GetHealth
()
.
AddChannel
(
connected
)
waitTimeout
:=
time
.
Duration
(
viper
.
GetUint
(
"waitTimeout"
))
waitUntilConnected
(
connected
)
timeoutTimer
:=
time
.
NewTimer
(
waitTimeout
*
time
.
Second
)
isConnected
:=
false
for
!
isConnected
{
select
{
case
isConnected
=
<-
connected
:
jww
.
INFO
.
Printf
(
"health status: %v
\n
"
,
isConnected
)
break
case
<-
timeoutTimer
.
C
:
jww
.
FATAL
.
Panic
(
"timeout on connection"
)
}
}
// Send Messages
// Send Messages
msgBody
:=
viper
.
GetString
(
"message"
)
msgBody
:=
viper
.
GetString
(
"message"
)
recipientID
:=
getUIDFromString
(
viper
.
GetString
(
"destid"
))
//recipientID := getUIDFromString(viper.GetString("destid"))
recipientID
:=
user
.
ID
msg
:=
client
.
NewCMIXMessage
(
recipientID
,
[]
byte
(
msgBody
))
msg
:=
client
.
NewCMIXMessage
(
recipientID
,
[]
byte
(
msgBody
))
params
:=
params
.
GetDefaultCMIX
()
params
:=
params
.
GetDefaultCMIX
()
...
@@ -170,7 +168,8 @@ var rootCmd = &cobra.Command{
...
@@ -170,7 +168,8 @@ var rootCmd = &cobra.Command{
// Wait until message timeout or we receive enough then exit
// Wait until message timeout or we receive enough then exit
// TODO: Actually check for how many messages we've received
// TODO: Actually check for how many messages we've received
receiveCnt
:=
viper
.
GetUint
(
"receiveCount"
)
receiveCnt
:=
viper
.
GetUint
(
"receiveCount"
)
timeoutTimer
=
time
.
NewTimer
(
waitTimeout
*
time
.
Second
)
waitTimeout
:=
time
.
Duration
(
viper
.
GetUint
(
"waitTimeout"
))
timeoutTimer
:=
time
.
NewTimer
(
waitTimeout
*
time
.
Second
)
done
:=
false
done
:=
false
for
!
done
{
for
!
done
{
select
{
select
{
...
@@ -178,12 +177,50 @@ var rootCmd = &cobra.Command{
...
@@ -178,12 +177,50 @@ var rootCmd = &cobra.Command{
fmt
.
Println
(
"Timed out!"
)
fmt
.
Println
(
"Timed out!"
)
done
=
true
done
=
true
break
break
case
m
:=
<-
recvCh
:
fmt
.
Printf
(
"Message received: %v"
,
m
)
break
}
}
}
}
fmt
.
Printf
(
"Received %d"
,
receiveCnt
)
fmt
.
Printf
(
"Received %d"
,
receiveCnt
)
},
},
}
}
func
waitUntilConnected
(
connected
chan
bool
)
{
waitTimeout
:=
time
.
Duration
(
viper
.
GetUint
(
"waitTimeout"
))
timeoutTimer
:=
time
.
NewTimer
(
waitTimeout
*
time
.
Second
)
isConnected
:=
false
//Wait until we connect or panic if we can't by a timeout
for
!
isConnected
{
select
{
case
isConnected
=
<-
connected
:
jww
.
INFO
.
Printf
(
"health status: %v
\n
"
,
isConnected
)
break
case
<-
timeoutTimer
.
C
:
jww
.
FATAL
.
Panic
(
"timeout on connection"
)
}
}
// Now start a thread to empty this channel and update us
// on connection changes for debugging purposes.
go
func
()
{
prev
:=
true
for
{
select
{
case
isConnected
=
<-
connected
:
if
isConnected
!=
prev
{
prev
=
isConnected
jww
.
INFO
.
Printf
(
"health status changed: %v
\n
"
,
isConnected
)
}
break
}
}
}()
}
func
getUIDFromString
(
idStr
string
)
*
id
.
ID
{
func
getUIDFromString
(
idStr
string
)
*
id
.
ID
{
idBytes
,
err
:=
hex
.
DecodeString
(
fmt
.
Sprintf
(
"%0*d%s"
,
idBytes
,
err
:=
hex
.
DecodeString
(
fmt
.
Sprintf
(
"%0*d%s"
,
66
-
len
(
idStr
),
0
,
idStr
))
66
-
len
(
idStr
),
0
,
idStr
))
...
...
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