Skip to content
Snippets Groups Projects
Commit fe082039 authored by Xavier Perret's avatar Xavier Perret
Browse files

bim

parent 5a63f762
No related branches found
No related tags found
1 merge request!2added file to separate client function from the server
...@@ -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: transIDInt,
} }
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")
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment