Skip to content
Snippets Groups Projects
Commit 6e81b432 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Add basic listener and minor refactoring

parent 5ddf09b6
No related branches found
No related tags found
No related merge requests found
...@@ -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))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment