From a66425cb90a6129990a306bf1c655d50ae491073 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Tue, 12 Jul 2022 15:31:55 -0400
Subject: [PATCH] Fixes from latest comments

---
 e2eClient/README.md | 26 ++++++++++++++++++++++++++
 e2eClient/main.go   | 21 +++++++++------------
 e2eClient/utils.go  | 12 ++++++++++++
 3 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/e2eClient/README.md b/e2eClient/README.md
index 3f62bc1..e06f796 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 719a50d..aba473f 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 71346e5..6d97aea 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)
+	}
+}
-- 
GitLab