From fe082039dd08201c2afb545049c19a201516f8f5 Mon Sep 17 00:00:00 2001 From: Xavier Perret <xa.perret@outlook.com> Date: Sat, 29 Oct 2022 16:29:54 +0200 Subject: [PATCH] bim --- app/sender/sender.go | 74 ++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/app/sender/sender.go b/app/sender/sender.go index ad815d3..4a993e1 100644 --- a/app/sender/sender.go +++ b/app/sender/sender.go @@ -3,7 +3,7 @@ package sender import ( "encoding/json" "fmt" - "net" + manage_connection "node/manage-connection" . "node/types" "os" "strconv" @@ -13,11 +13,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) { fmt.Println() fmt.Println("Trying to connect to ", address) - conn, err := net.Dial("tcp", address) - if err != nil { - fmt.Println("Error while connecting to the neighbour", err) - os.Exit(1) - } + conn := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, address) mess := Message{ MessageType: "rate", @@ -25,7 +21,7 @@ func SendVoteToNeighbour(config Config, trans Transaction, address string) { } fmt.Println("Sending message to neighbour", mess) encoder := json.NewEncoder(conn) - err = encoder.Encode(mess) + err := encoder.Encode(mess) if err != nil { fmt.Println("Error while encoding the transaction", err) os.Exit(1) @@ -47,11 +43,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) { ackToSend.AmountOfCorrectNode = ack.AmountOfCorrectNode ackToSend.TotalNodes = ack.TotalNodes - conn, err := net.Dial("tcp", address) - if err != nil { - fmt.Println("Error while connecting to the neighbour", err) - os.Exit(1) - } + conn := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, address) mess := Message{ MessageType: "AckResponse", @@ -59,7 +51,7 @@ func SendAckToNeighbour(config Config, ack AckTransaction, address string) { } fmt.Println("Sending message to neighbour", mess) encoder := json.NewEncoder(conn) - err = encoder.Encode(mess) + err := encoder.Encode(mess) if err != nil { fmt.Println("Error while encoding the transaction", err) os.Exit(1) @@ -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) { fmt.Println() ipAddr := destinationIp + ":" + destinationPort fmt.Println("Trying to connect to ", destinationIp, ":", destinationPort) - conn, err := net.Dial("tcp", ipAddr) - if err != nil { - fmt.Println("Error while connecting to the neighbour", err) - os.Exit(1) - } + conn := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, ipAddr) mess := Message{ MessageType: "transaction", @@ -99,7 +97,7 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp } fmt.Println("Sending message to neighbour", mess) encoder := json.NewEncoder(conn) - err = encoder.Encode(mess) + err := encoder.Encode(mess) if err != nil { fmt.Println("Error while encoding the transaction", err) os.Exit(1) @@ -109,37 +107,33 @@ func SendTransactionToNeighbour(config Config, trans Transaction, destinationIp fmt.Println("MessageBody successfully sent to neighbour") } -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) - } -} - -func SendFakeTransactionToNeighbour(trans Transaction, address string) { +// SendVoteRequestToNode is a function to send a vote request to a node, used strictly by client +func SendVoteRequestToNode(clientConfig Config, transIDInt int64, address string) { fmt.Println() fmt.Println("Trying to connect to ", address) - conn, err := net.Dial("tcp", address) - if err != nil { - fmt.Println("Error while connecting to the neighbour", err) - os.Exit(1) - } + conn := manage_connection.CreateConnection(address) mess := Message{ - MessageType: "fake", - MessageBody: trans, + MessageType: "voteRequest", + MessageBody: transIDInt, } - fmt.Println("Sending message to neighbour", mess) + fmt.Println("Sending vote request to node", mess) encoder := json.NewEncoder(conn) - err = encoder.Encode(mess) + err := encoder.Encode(mess) if err != nil { fmt.Println("Error while encoding the transaction", err) os.Exit(1) } 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") } -- GitLab