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

bim

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