Skip to content
Snippets Groups Projects
Commit a66425cb authored by Jonah Husson's avatar Jonah Husson
Browse files

Fixes from latest comments

parent 68478757
No related branches found
No related tags found
1 merge request!4Add example E2e client
# xxdk E2E client example # xxdk E2E client example
This mini-repository contains example logic for running an e2e client.
This is provided by the xx network team as a springboard to help consumers
better understand our API and how it may be used.
`main.go` contains the crux of the logic. We avoid complicating our example by
avoiding the usage of CLI flags for basic variables you may change in the code.
This file initiates an xxdk E2E client, using the authentication callbacks in
`auth.go`. With that established, it registers a generic message listener
and establishes authentication with a partner. Finally, it sends a test message
and listens for incoming messages until stopped by the user.
`utils.go` contains utility functions for running the program. In this case,
we provide a tool initializing a log and one which writes a contact to a file.
`listener.go` contains logic for handling the reception of a message via the
e2e client. In this example, it is very basic. We invite consumers
to use this as a basis to implement more complex message listeners.
## Build Instructions
In these instructions we will go over building a connection client using our
example. In order to build a client which successfully sends a message through
the connection, we must first go over how to build and run a connection server.
### Building a Client
...@@ -91,16 +91,13 @@ func main() { ...@@ -91,16 +91,13 @@ func main() {
} }
} }
err = ioutil.WriteFile(myContactPath, identity.GetContact().Marshal(), fs.ModePerm) writeContact(myContactPath, identity.GetContact())
if err != nil {
jww.FATAL.Panicf("Failed to write contact to file at %s: %+v", myContactPath, err)
}
// Create an E2E client // Create an E2E client
// Pass in auth object which controls auth callbacks for this client // Pass in auth object which controls auth callbacks for this client
params := xxdk.GetDefaultE2EParams() params := xxdk.GetDefaultE2EParams()
jww.INFO.Printf("Using E2E parameters: %+v", params) jww.INFO.Printf("Using E2E parameters: %+v", params)
confirmChan := make(chan contact.Contact) confirmChan := make(chan contact.Contact, 5)
xxdkClient, err := xxdk.Login(baseClient, &auth{confirmChan: confirmChan}, identity, params) xxdkClient, err := xxdk.Login(baseClient, &auth{confirmChan: confirmChan}, identity, params)
if err != nil { if err != nil {
jww.FATAL.Panicf("Unable to Login: %+v", err) jww.FATAL.Panicf("Unable to Login: %+v", err)
...@@ -143,6 +140,13 @@ func main() { ...@@ -143,6 +140,13 @@ func main() {
// Wait until connected or crash on timeout // Wait until connected or crash on timeout
waitUntilConnected(connected) waitUntilConnected(connected)
// Register a listener for messages--------------------------------------------------
// Listen for all types of messages using catalog.NoType
// Listen for messages from all users using id.ZeroUser
// User-defined behavior for message reception goes in the listener
_ = e2eClient.RegisterListener(&id.ZeroUser, catalog.NoType, listener{name: "e2e Message Listener"})
// Connect with the recipient-------------------------------------------------- // Connect with the recipient--------------------------------------------------
if recipientContactPath != "" { if recipientContactPath != "" {
...@@ -194,13 +198,6 @@ func main() { ...@@ -194,13 +198,6 @@ func main() {
jww.INFO.Printf("Message %v sent in RoundIDs: %+v at %v", messageID, roundIDs, timeSent) jww.INFO.Printf("Message %v sent in RoundIDs: %+v at %v", messageID, roundIDs, timeSent)
} }
// Register a listener for messages--------------------------------------------------
// Listen for all types of messages using catalog.NoType
// Listen for messages from all users using id.ZeroUser
// User-defined behavior for message reception goes in the listener
_ = e2eClient.RegisterListener(&id.ZeroUser, catalog.NoType, listener{name: "e2e Message Listener"})
// Keep app running to receive messages----------------------------------------------- // Keep app running to receive messages-----------------------------------------------
// Wait until the user terminates the program // Wait until the user terminates the program
......
...@@ -2,6 +2,8 @@ package main ...@@ -2,6 +2,8 @@ package main
import ( import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/crypto/contact"
"gitlab.com/xx_network/primitives/utils"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
...@@ -36,3 +38,13 @@ func initLog(threshold uint, logPath string) { ...@@ -36,3 +38,13 @@ func initLog(threshold uint, logPath string) {
jww.SetLogThreshold(jww.LevelInfo) jww.SetLogThreshold(jww.LevelInfo)
} }
} }
func writeContact(outfilePath string, c contact.Contact) {
err := utils.WriteFileDef(outfilePath, c.Marshal())
if err != nil {
jww.ERROR.Printf("could not write contact file: %+v", err)
} else {
jww.INFO.Printf("contact written to %s successfully", outfilePath)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment