Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xxdk Examples
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
elixxir
xxdk Examples
Commits
0aa414ee
Commit
0aa414ee
authored
2 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Have connect server send message back to client; Improve code quality
parent
983040f8
No related branches found
No related tags found
1 merge request
!5
Connections readme
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
connectClient/README.md
+11
-3
11 additions, 3 deletions
connectClient/README.md
connectClient/main.go
+33
-22
33 additions, 22 deletions
connectClient/main.go
connectServer/listener.go
+3
-3
3 additions, 3 deletions
connectServer/listener.go
connectServer/main.go
+31
-15
31 additions, 15 deletions
connectServer/main.go
with
78 additions
and
43 deletions
connectClient/README.md
+
11
−
3
View file @
0aa414ee
...
...
@@ -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:
"
\x
9bTM6
\x
eeh,
\x
7fT
\x
f1
\x
e9
\x
1f
\x
d4
\x
07X
\x
98T
\x
db
\x
7fy
\x
d7
\x
0e
\x
84p
\x
04:
\x
e2m
\x
95E9J
\x
02"
Topology:
"
\x
dd4
\x
04ʧ
\x
dd
\x
dd<
\x
86
\x
85
\x
00{
\x
03
\x
db
\x
d0rC
\x
cc
\x
e5<
\x
c6>
\x
f1~
\x
17
\x
e2
\x
cc
\x
cd
`
\x
fcBm
\x
02
" Topology:"
\x
dax
\x
a2
\x
e1f
\x
033!S
\x
9a1cX
\x
adKƗ
\x
90
\x
97c
\x
cc
$|
j
\x
9e
\x
c1Z
\x
9b
\x
c6@5%
\x
02
" Topology:"
\x
d53
\x
c0
\x
1a
\x
9fm
\x
90-~D%kE
\x
1e+
\x
fc0d:R
\x
e20
\x
aa2
\x
a5
\\
N
\x
19
\x
b0e
\x
c5_
\x
02
" Topology:"
\x
b59M
\x
f4w
\x
c6
\x
07
\x
ec
\x
d61A
\x
02
\x
f9
\x
f3
\x
14
\x
9d
\x
f5
\x
d8F0
\x
84a
\x
ea:
\x
cc
\x
10
\x
95&8du
\x
14
\x
02
" Timestamps:1657227203 Timestamps:1657227203246307109 Timestamps:1657227209216187342 Timestamps:1657227212216189662 Timestamps:0 Timestamps:0 Timestamps:0 ResourceQueueTimeoutMillis:3906340864 AddressSpaceSize:18 EccSignature:{Nonce:"
\x
c3Ͼ
\x
c8T
\x
e1
\x
adE
\x
81
\x
94r
\t\x
14,
\x
fa
\u
008a
\x
fc8
\x
93h
\x
ac
\x
f4e
\x
e4Y
\x
eb
\x
a5v
\x
a9
\x
82
" Signature:"
E
$\
xffjŲ
\x
fa/
\x
e6U@
\x
1d
\x
edp
\x
c4
\x
d7ջ
\x
88
\x
e1
\x
ea?7
\x
8f
\x
837
\x
c2 ?
\x
99
\x
e5
\x
cc
\x
d7
\x
cd
}
\x
adw
`
fPĸ
\x
06
\x
1bm
\x
84,
\x
02f
\x
ce
\x
e4
\x
08
\x
96
\x
84K
\x
0c
\x
88wy
\x
fds
\n
"
}}}
```
This diff is collapsed.
Click to expand it.
connectClient/main.go
+
33
−
22
View file @
0aa414ee
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
connectServer/listener.go
+
3
−
3
View file @
0aa414ee
...
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
connectServer/main.go
+
31
−
15
View file @
0aa414ee
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment