Skip to content
Snippets Groups Projects
Commit 0aa414ee authored by Josh Brooks's avatar Josh Brooks
Browse files

Have connect server send message back to client; Improve code quality

parent 983040f8
No related branches found
No related tags found
1 merge request!5Connections readme
......@@ -76,6 +76,14 @@ INFO 2022/07/07 12:59:12.088046 Message received: {XxMessage WjdMwCH+... [73 102
By default, the client sends a single message to the server, with the server
registering a `receive.Listener` which listens to messages of type
`catalog.NoType` from the client. The server does not send a message back in
this example.
`catalog.NoType` from the client.
Verification that the server is able to send messages back to the client may
also be done. This can be done by checking the client's log for the string
`Message received`.
```bash
grep "Message received" client.log
INFO 2022/07/07 13:53:34.242752 Message received: {NoType S5yr+Zo1... [73 102 32 116 104 105 115 32 109 101 115 115 97 103 101 32 105 115 32 115 101 110 116 32 115 117 99 99 101 115 115 102 117 108 108 121 44 32 119 101 39 108 108 32 104 97 118 101 32 101 115 116 97 98 108 105 115 104 101 100 32 99 111 110 116 97 99 116 32 119 105 116 104 32 116 104 101 32 99 108 105 101 110 116 46] aT1Z9OLBN+WLMSk9UjN/2Jxe6xtmU9RadnqXU9mf8kgD q9G7VmURis3jNwj8FHxgpS3sukSdL/8+MlLaqyGbJwYD [0 0 0 0 0 1 159 92] 2022-07-07 13:53:31.078117153 -0700 PDT true {58349730 QUEUED 0xc000bc04c0 map[PENDING:1969-12-31 16:00:01.657227203 -0800 PST PRECOMPUTING:2022-07-07 13:53:23.246307109 -0700 PDT STANDBY:2022-07-07 13:53:29.216187342 -0700 PDT QUEUED:2022-07-07 13:53:32.216189662 -0700 PDT] [] 1000 18 187093087 ID:58349730 UpdateID:187093087 State:3 BatchSize:1000 Topology:"\x9bTM6\xeeh,\x7fT\xf1\xe9\x1f\xd4\x07X\x98T\xdb\x7fy\xd7\x0e\x84p\x04:\xe2m\x95E9J\x02" Topology:"\xdd4\x04ʧ\xdd\xdd<\x86\x85\x00{\x03\xdb\xd0rC\xcc\xe5<\xc6>\xf1~\x17\xe2\xcc\xcd`\xfcBm\x02" Topology:"\xdax\xa2\xe1f\x033!S\x9a1cX\xadKƗ\x90\x97c\xcc$|j\x9e\xc1Z\x9b\xc6@5%\x02" Topology:"\xd53\xc0\x1a\x9fm\x90-~D%kE\x1e+\xfc0d:R\xe20\xaa2\xa5\\N\x19\xb0e\xc5_\x02" Topology:"\xb59M\xf4w\xc6\x07\xec\xd61A\x02\xf9\xf3\x14\x9d\xf5\xd8F0\x84a\xea:\xcc\x10\x95&8du\x14\x02" Timestamps:1657227203 Timestamps:1657227203246307109 Timestamps:1657227209216187342 Timestamps:1657227212216189662 Timestamps:0 Timestamps:0 Timestamps:0 ResourceQueueTimeoutMillis:3906340864 AddressSpaceSize:18 EccSignature:{Nonce:"\xc3Ͼ\xc8T\xe1\xadE\x81\x94r\t\x14,\xfa\u008a\xfc8\x93h\xac\xf4e\xe4Y \xeb\xa5v\xa9\x82" Signature:"E$\xffjŲ\xfa/޿\xe6U@\x1d\xedp\xc4\xd7ջ\x88\xe1\xea?7\x8f\x837\xc2 ?\x99\xe5\xcc\xd7\xcd}\xadw`fPĸ\x06\x1bm\x84,\x02f\xce\xe4\x08\x96\x84K\x0c\x88wy\xfds\n"}}}
```
......@@ -21,7 +21,7 @@ func main() {
// Logging
initLog(1, "client.log")
// Create a new client object-------------------------------------------------------
// Create a new client object----------------------------------------------
// Path to the server contact file
serverContactPath := "connectServer.xxc"
......@@ -29,9 +29,9 @@ func main() {
// You would ideally use a configuration tool to acquire these parameters
statePath := "statePath"
statePass := "password"
// The following connects to mainnet. For historical reasons it is called a json file
// but it is actually a marshalled file with a cryptographic signature attached.
// This may change in the future.
// The following connects to mainnet. For historical reasons it is called a
// json file but it is actually a marshalled file with a cryptographic
// signature attached. This may change in the future.
ndfURL := "https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/mainnet.json"
certificatePath := "../mainnet.crt"
ndfPath := "ndf.json"
......@@ -53,7 +53,8 @@ func main() {
jww.FATAL.Panicf("Failed to read certificate: %v", err)
}
ndfJSON, err = xxdk.DownloadAndVerifySignedNdfWithUrl(ndfURL, string(cert))
ndfJSON, err = xxdk.DownloadAndVerifySignedNdfWithUrl(
ndfURL, string(cert))
if err != nil {
jww.FATAL.Panicf("Failed to download NDF: %+v", err)
}
......@@ -66,10 +67,11 @@ func main() {
}
}
// Login to your client session-----------------------------------------------------
// Login to your client session--------------------------------------------
// Login with the same sessionPath and sessionPass used to call NewClient()
baseClient, err := xxdk.LoadCmix(statePath, []byte(statePass), xxdk.GetDefaultCMixParams())
baseClient, err := xxdk.LoadCmix(statePath, []byte(statePass),
xxdk.GetDefaultCMixParams())
if err != nil {
jww.FATAL.Panicf("Failed to load state: %+v", err)
}
......@@ -83,22 +85,25 @@ func main() {
if err != nil {
jww.FATAL.Panicf("Failed to generate reception identity: %+v", err)
}
err = xxdk.StoreReceptionIdentity(identityStorageKey, identity, baseClient)
err = xxdk.StoreReceptionIdentity(
identityStorageKey, identity, baseClient)
if err != nil {
jww.FATAL.Panicf("Failed to store new reception identity: %+v", err)
}
}
// Create an E2E client
// The connect packages handles AuthCallbacks, xxdk.DefaultAuthCallbacks is fine here
// The `connect` packages handles AuthCallbacks,
// `xxdk.DefaultAuthCallbacks` is fine here
params := xxdk.GetDefaultE2EParams()
jww.INFO.Printf("Using E2E parameters: %+v", params)
e2eClient, err := xxdk.Login(baseClient, xxdk.DefaultAuthCallbacks{}, identity, params)
e2eClient, err := xxdk.Login(baseClient, xxdk.DefaultAuthCallbacks{},
identity, params)
if err != nil {
jww.FATAL.Panicf("Unable to Login: %+v", err)
}
// Start network threads------------------------------------------------------------
// Start network threads----------------------------------------------------
// Set networkFollowerTimeout to a value of your choice (seconds)
networkFollowerTimeout := 5 * time.Second
......@@ -126,7 +131,8 @@ func main() {
// Create a tracker channel to be notified of network changes
connected := make(chan bool, 10)
// Provide a callback that will be signalled when network health status changes
// Provide a callback that will be signalled when network
// health status changes
e2eClient.GetCmix().AddHealthCallback(
func(isConnected bool) {
connected <- isConnected
......@@ -134,7 +140,7 @@ func main() {
// Wait until connected or crash on timeout
waitUntilConnected(connected)
// Connect with the server--------------------------------------------------
// Connect with the server-------------------------------------------------
// Recipient's contact (read from a Client CLI-generated contact file)
contactData, err := ioutil.ReadFile(serverContactPath)
......@@ -143,8 +149,9 @@ func main() {
}
// Imported "gitlab.com/elixxir/crypto/contact"
// which provides an `Unmarshal` function to convert the byte slice ([]byte) output
// of `ioutil.ReadFile()` to the `Contact` type expected by `RequestAuthenticatedChannel()`
// which provides an `Unmarshal` function to convert the
// byte slice ([]byte) output of `ioutil.ReadFile()` to the `Contact` type
// expected by `RequestAuthenticatedChannel()`
recipientContact, err := contact.Unmarshal(contactData)
if err != nil {
jww.FATAL.Panicf("Failed to get contact data: %+v", err)
......@@ -156,9 +163,10 @@ func main() {
if err != nil {
jww.FATAL.Panicf("Failed to create connection object: %+v", err)
}
jww.INFO.Printf("Connect with %s successfully established!", recipientContact.ID)
jww.INFO.Printf("Connect with %s successfully established!",
recipientContact.ID)
// Register a listener for messages--------------------------------------------------
// Register a listener for messages----------------------------------------
// Listen for all types of messages using catalog.NoType
// User-defined behavior for message reception goes in the listener
......@@ -169,17 +177,20 @@ func main() {
jww.FATAL.Panicf("Could not register message listener: %+v", err)
}
// Send a message to the server----------------------------------------------------
// Send a message to the server--------------------------------------------
// Test message
msgBody := "If this message is sent successfully, we'll have established contact with the server."
roundIDs, messageID, timeSent, err := handler.SendE2E(catalog.XxMessage, []byte(msgBody), params.Base)
msgBody := "If this message is sent successfully, we'll have established " +
"contact with the server."
roundIDs, messageID, timeSent, err := handler.SendE2E(
catalog.XxMessage, []byte(msgBody), params.Base)
if err != nil {
jww.FATAL.Panicf("Failed to send message: %+v", err)
}
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)
// Keep app running to receive messages-----------------------------------------------
// Keep app running to receive messages------------------------------------
// Wait until the user terminates the program
c := make(chan os.Signal)
......
......@@ -10,8 +10,8 @@ type listener struct {
name string
}
// Hear will be called whenever a message matching the RegisterListener call is
// received.
// Hear will be called whenever a message matching the
// RegisterListener call is received.
//
// User-defined message handling logic goes here.
func (l *listener) Hear(item receive.Message) {
......
......@@ -20,7 +20,7 @@ func main() {
// Logging
initLog(1, "server.log")
// Create a new client object-------------------------------------------------------
// Create a new client object----------------------------------------------
// Set the output contact file path
contactFilePath := "connectServer.xxc"
......@@ -28,9 +28,9 @@ func main() {
// You would ideally use a configuration tool to acquire these parameters
statePath := "statePath"
statePass := "password"
// The following connects to mainnet. For historical reasons it is called a json file
// but it is actually a marshalled file with a cryptographic signature attached.
// This may change in the future.
// The following connects to mainnet. For historical reasons it is called
// a json file but it is actually a marshalled file with a cryptographic
// signature attached. This may change in the future.
ndfURL := "https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/mainnet.json"
certificatePath := "../mainnet.crt"
ndfPath := "ndf.json"
......@@ -52,7 +52,8 @@ func main() {
jww.FATAL.Panicf("Failed to read certificate: %v", err)
}
ndfJSON, err = xxdk.DownloadAndVerifySignedNdfWithUrl(ndfURL, string(cert))
ndfJSON, err = xxdk.DownloadAndVerifySignedNdfWithUrl(
ndfURL, string(cert))
if err != nil {
jww.FATAL.Panicf("Failed to download NDF: %+v", err)
}
......@@ -65,10 +66,11 @@ func main() {
}
}
// Load client state and identity-----------------------------------------------------
// Load client state and identity------------------------------------------
// Load with the same sessionPath and sessionPass used to call NewClient()
baseClient, err := xxdk.LoadCmix(statePath, []byte(statePass), xxdk.GetDefaultCMixParams())
baseClient, err := xxdk.LoadCmix(statePath, []byte(statePass),
xxdk.GetDefaultCMixParams())
if err != nil {
jww.FATAL.Panicf("Failed to load state: %+v", err)
}
......@@ -88,12 +90,13 @@ func main() {
}
}
// Save contact file----------------------------------------------------------------
// Save contact file-------------------------------------------------------
// Save the contact file so that client can connect to this server
writeContact(contactFilePath, identity.GetContact())
// Handle incoming connections------------------------------------------------------
// Handle incoming connections---------------------------------------------
e2eParams := xxdk.GetDefaultE2EParams()
// Create callback for incoming connections
cb := func(connection connect.Connection) {
......@@ -104,12 +107,24 @@ func main() {
if err != nil {
jww.FATAL.Panicf("Failed to register listener: %+v", err)
}
msgBody := "If this message is sent successfully, we'll have " +
"established contact with the client."
roundIDs, messageID, timeSent, err := connection.SendE2E(catalog.NoType,
[]byte(msgBody), e2eParams.Base)
if err != nil {
jww.FATAL.Panicf("Failed to send message: %+v", err)
}
jww.INFO.Printf("Message %v sent in RoundIDs: %+v at %v", messageID,
roundIDs, timeSent)
// Start connection server----------------------------------------------------------
}
// Start the connection server, which will allow clients to start connections with you
e2eParams := xxdk.GetDefaultE2EParams()
// Start connection server-------------------------------------------------
// Start the connection server, which will allow clients to start
//connections with you
connectionListParams := connect.DefaultConnectionListParams()
connectServer, err := connect.StartServer(
identity, cb, baseClient, e2eParams, connectionListParams)
......@@ -117,7 +132,7 @@ func main() {
jww.FATAL.Panicf("Unable to start connection server: %+v", err)
}
// Start network threads------------------------------------------------------------
// Start network threads---------------------------------------------------
// Set networkFollowerTimeout to a value of your choice (seconds)
networkFollowerTimeout := 5 * time.Second
......@@ -145,7 +160,8 @@ func main() {
// Create a tracker channel to be notified of network changes
connected := make(chan bool, 10)
// Provide a callback that will be signalled when network health status changes
// Provide a callback that will be signalled when network health
// status changes
connectServer.E2e.GetCmix().AddHealthCallback(
func(isConnected bool) {
connected <- isConnected
......@@ -153,7 +169,7 @@ func main() {
// Wait until connected or crash on timeout
waitUntilConnected(connected)
// Keep app running to receive messages-----------------------------------------------
// Keep app running to receive messages------------------------------------
// Wait until the user terminates the program
c := make(chan os.Signal)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment