Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
perso-distributed-systems
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xavier.perret
perso-distributed-systems
Commits
fe082039
Commit
fe082039
authored
2 years ago
by
Xavier Perret
Browse files
Options
Downloads
Patches
Plain Diff
bim
parent
5a63f762
No related branches found
No related tags found
1 merge request
!2
added file to separate client function from the server
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/sender/sender.go
+34
-40
34 additions, 40 deletions
app/sender/sender.go
with
34 additions
and
40 deletions
app/sender/sender.go
+
34
−
40
View file @
fe082039
...
@@ -3,7 +3,7 @@ package sender
...
@@ -3,7 +3,7 @@ package sender
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"net
"
manage_connection
"node/manage-connection
"
.
"node/types"
.
"node/types"
"os"
"os"
"strconv"
"strconv"
...
@@ -13,11 +13,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) {
...
@@ -13,11 +13,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) {
fmt
.
Println
()
fmt
.
Println
()
fmt
.
Println
(
"Trying to connect to "
,
address
)
fmt
.
Println
(
"Trying to connect to "
,
address
)
conn
,
err
:=
net
.
Dial
(
"tcp"
,
address
)
conn
:=
manage_connection
.
CreateConnectionWithSpecifiedLocalAddress
(
config
.
Address
,
address
)
if
err
!=
nil
{
fmt
.
Println
(
"Error while connecting to the neighbour"
,
err
)
os
.
Exit
(
1
)
}
mess
:=
Message
{
mess
:=
Message
{
MessageType
:
"rate"
,
MessageType
:
"rate"
,
...
@@ -25,7 +21,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) {
...
@@ -25,7 +21,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) {
}
}
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
encoder
:=
json
.
NewEncoder
(
conn
)
encoder
:=
json
.
NewEncoder
(
conn
)
err
=
encoder
.
Encode
(
mess
)
err
:
=
encoder
.
Encode
(
mess
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
...
@@ -47,11 +43,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) {
...
@@ -47,11 +43,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) {
ackToSend
.
AmountOfCorrectNode
=
ack
.
AmountOfCorrectNode
ackToSend
.
AmountOfCorrectNode
=
ack
.
AmountOfCorrectNode
ackToSend
.
TotalNodes
=
ack
.
TotalNodes
ackToSend
.
TotalNodes
=
ack
.
TotalNodes
conn
,
err
:=
net
.
Dial
(
"tcp"
,
address
)
conn
:=
manage_connection
.
CreateConnectionWithSpecifiedLocalAddress
(
config
.
Address
,
address
)
if
err
!=
nil
{
fmt
.
Println
(
"Error while connecting to the neighbour"
,
err
)
os
.
Exit
(
1
)
}
mess
:=
Message
{
mess
:=
Message
{
MessageType
:
"AckResponse"
,
MessageType
:
"AckResponse"
,
...
@@ -59,7 +51,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) {
...
@@ -59,7 +51,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) {
}
}
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
encoder
:=
json
.
NewEncoder
(
conn
)
encoder
:=
json
.
NewEncoder
(
conn
)
err
=
encoder
.
Encode
(
mess
)
err
:
=
encoder
.
Encode
(
mess
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
...
@@ -81,17 +73,23 @@ func SendVoteToAllNeighbours(config Config, trans Transaction, parentIp string)
...
@@ -81,17 +73,23 @@ func SendVoteToAllNeighbours(config Config, trans Transaction, parentIp string)
}
}
}
}
// sendTransactionToNeighbour is a function to send a transaction to a neighbour
// SendTransactionToAllNeighbours is a function to send a transaction to all neighbours
func
SendTransactionToAllNeighbours
(
config
Config
,
trans
Transaction
)
{
for
neighbour
:=
range
config
.
Neighbours
{
fmt
.
Println
(
"Sending transaction to neighbour "
,
config
.
Neighbours
[
neighbour
]
.
ID
)
ip
:=
config
.
Neighbours
[
neighbour
]
.
Address
port
:=
strconv
.
Itoa
(
config
.
Neighbours
[
neighbour
]
.
Port
)
SendTransactionToNeighbour
(
config
,
trans
,
ip
,
port
)
}
}
// SendTransactionToNeighbour is a function to send a transaction to a neighbour, used both by server/client
func
SendTransactionToNeighbour
(
config
Config
,
trans
Transaction
,
destinationIp
string
,
destinationPort
string
)
{
func
SendTransactionToNeighbour
(
config
Config
,
trans
Transaction
,
destinationIp
string
,
destinationPort
string
)
{
fmt
.
Println
()
fmt
.
Println
()
ipAddr
:=
destinationIp
+
":"
+
destinationPort
ipAddr
:=
destinationIp
+
":"
+
destinationPort
fmt
.
Println
(
"Trying to connect to "
,
destinationIp
,
":"
,
destinationPort
)
fmt
.
Println
(
"Trying to connect to "
,
destinationIp
,
":"
,
destinationPort
)
conn
,
err
:=
net
.
Dial
(
"tcp"
,
ipAddr
)
conn
:=
manage_connection
.
CreateConnectionWithSpecifiedLocalAddress
(
config
.
Address
,
ipAddr
)
if
err
!=
nil
{
fmt
.
Println
(
"Error while connecting to the neighbour"
,
err
)
os
.
Exit
(
1
)
}
mess
:=
Message
{
mess
:=
Message
{
MessageType
:
"transaction"
,
MessageType
:
"transaction"
,
...
@@ -99,7 +97,7 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp
...
@@ -99,7 +97,7 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp
}
}
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
fmt
.
Println
(
"Sending message to neighbour"
,
mess
)
encoder
:=
json
.
NewEncoder
(
conn
)
encoder
:=
json
.
NewEncoder
(
conn
)
err
=
encoder
.
Encode
(
mess
)
err
:
=
encoder
.
Encode
(
mess
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
...
@@ -109,37 +107,33 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp
...
@@ -109,37 +107,33 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp
fmt
.
Println
(
"MessageBody successfully sent to neighbour"
)
fmt
.
Println
(
"MessageBody successfully sent to neighbour"
)
}
}
func
SendTransactionToAllNeighbours
(
config
Config
,
trans
Transaction
)
{
// SendVoteRequestToNode is a function to send a vote request to a node, used strictly by client
for
neighbour
:=
range
config
.
Neighbours
{
func
SendVoteRequestToNode
(
clientConfig
Config
,
transIDInt
int64
,
address
string
)
{
fmt
.
Println
(
"Sending transaction to neighbour "
,
config
.
Neighbours
[
neighbour
]
.
ID
)
ip
:=
config
.
Neighbours
[
neighbour
]
.
Address
port
:=
strconv
.
Itoa
(
config
.
Neighbours
[
neighbour
]
.
Port
)
SendTransactionToNeighbour
(
config
,
trans
,
ip
,
port
)
}
}
func
SendFakeTransactionToNeighbour
(
trans
Transaction
,
address
string
)
{
fmt
.
Println
()
fmt
.
Println
()
fmt
.
Println
(
"Trying to connect to "
,
address
)
fmt
.
Println
(
"Trying to connect to "
,
address
)
conn
,
err
:=
net
.
Dial
(
"tcp"
,
address
)
conn
:=
manage_connection
.
CreateConnection
(
address
)
if
err
!=
nil
{
fmt
.
Println
(
"Error while connecting to the neighbour"
,
err
)
os
.
Exit
(
1
)
}
mess
:=
Message
{
mess
:=
Message
{
MessageType
:
"
fake
"
,
MessageType
:
"
voteRequest
"
,
MessageBody
:
trans
,
MessageBody
:
trans
IDInt
,
}
}
fmt
.
Println
(
"Sending
message to neighbour
"
,
mess
)
fmt
.
Println
(
"Sending
vote request to node
"
,
mess
)
encoder
:=
json
.
NewEncoder
(
conn
)
encoder
:=
json
.
NewEncoder
(
conn
)
err
=
encoder
.
Encode
(
mess
)
err
:
=
encoder
.
Encode
(
mess
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
fmt
.
Println
(
"Error while encoding the transaction"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
}
}
conn
.
Close
()
conn
.
Close
()
}
// SendFakeRequestToNode is a function to send a fake request to a node, used strictly by client
func
SendFakeRequestToNode
(
clientConfig
Config
,
transIDInt
int64
,
address
string
)
{
}
// SendListTransactionsRequestToNode is a function to ask a node to locally list its transaction in its cli, used strictly by client
func
SendListTransactionsRequestToNode
(
clientConfig
Config
,
address
string
)
{
fmt
.
Println
(
"MessageBody successfully sent to neighbour"
)
}
}
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