diff --git a/e2eClient/README.md b/e2eClient/README.md index 3f62bc1d0b0b9ef8860260c1aedc2d95bbe3b856..e06f796aa7c33d3d18beb5398f8ea4cea5db2144 100644 --- a/e2eClient/README.md +++ b/e2eClient/README.md @@ -1 +1,27 @@ # 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 diff --git a/e2eClient/main.go b/e2eClient/main.go index 719a50df723c34b8ff7acc1b34c329d2c507ae07..aba473fa6606dc8a854b4d146d935c13a27e6e2a 100644 --- a/e2eClient/main.go +++ b/e2eClient/main.go @@ -91,16 +91,13 @@ func main() { } } - err = ioutil.WriteFile(myContactPath, identity.GetContact().Marshal(), fs.ModePerm) - if err != nil { - jww.FATAL.Panicf("Failed to write contact to file at %s: %+v", myContactPath, err) - } + writeContact(myContactPath, identity.GetContact()) // Create an E2E client // Pass in auth object which controls auth callbacks for this client params := xxdk.GetDefaultE2EParams() 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) if err != nil { jww.FATAL.Panicf("Unable to Login: %+v", err) @@ -143,6 +140,13 @@ func main() { // Wait until connected or crash on timeout 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-------------------------------------------------- if recipientContactPath != "" { @@ -194,13 +198,6 @@ func main() { 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----------------------------------------------- // Wait until the user terminates the program diff --git a/e2eClient/utils.go b/e2eClient/utils.go index 71346e5874140f36a6cf413490b5e09cc2ca13e2..6d97aea3088e5b5038787ec714556ddf02f1b28c 100644 --- a/e2eClient/utils.go +++ b/e2eClient/utils.go @@ -2,6 +2,8 @@ package main import ( jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/crypto/contact" + "gitlab.com/xx_network/primitives/utils" "io/ioutil" "log" "os" @@ -36,3 +38,13 @@ func initLog(threshold uint, logPath string) { 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) + } +}