Skip to content
Snippets Groups Projects
Select Git revision
  • 474a41a781afb2666ae138eb5cb4cbced947a552
  • master default protected
2 results

sender.go

Blame
  • sender.go 5.22 KiB
    package sender
    
    import (
    	"encoding/json"
    	"fmt"
    	manage_connection "node/manage-connection"
    	. "node/types"
    	"os"
    	"strconv"
    )
    
    func SendVoteToNeighbour(config Config, trans Transaction, address string) {
    	fmt.Println()
    	fmt.Println("Trying to connect to ", address)
    
    	conn := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, address)
    
    	mess := Message{
    		MessageType: "rate",
    		MessageBody: trans,
    	}
    	fmt.Println("Sending message to neighbour", mess)
    	encoder := json.NewEncoder(conn)
    	err := encoder.Encode(mess)
    	if err != nil {
    		fmt.Println("Error while encoding the transaction", err)
    		os.Exit(1)
    	}
    
    	err = conn.Close()
    	if err != nil {
    		fmt.Println("Error while closing the process-manage-connection", err)
    	}
    
    	fmt.Println("MessageBody successfully sent to neighbour")
    }
    
    func SendAckToNeighbour(config Config, ack AckTransaction, address string) {
    	fmt.Println("*******************STARTING TO SEND ACK*******************")
    	fmt.Println("Trying to connect to ", address)
    	var ackToSend AckTransaction
    	ackToSend.Id = ack.Id
    	ackToSend.AmountOfCorrectNode = ack.AmountOfCorrectNode
    	ackToSend.TotalNodes = ack.TotalNodes
    
    	conn := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, address)
    
    	mess := Message{
    		MessageType: "AckResponse",
    		MessageBody: ackToSend,
    	}
    	fmt.Println("Sending message to neighbour", mess)
    	encoder := json.NewEncoder(conn)
    	err := encoder.Encode(mess)
    	if err != nil {
    		fmt.Println("Error while encoding the transaction", err)
    		os.Exit(1)
    	}
    	conn.Close()
    
    	fmt.Println("MessageBody successfully sent to neighbour")
    	fmt.Println("*******************FINISHED ACK*******************")
    }
    
    func SendVoteToAllNeighbours(config Config, trans Transaction, parentIp string) {
    	for neighbour := range config.Neighbours {
    		ip := config.Neighbours[neighbour].Address
    		port := strconv.Itoa(config.Neighbours[neighbour].Port)
    		address := ip + ":" + port
    		if parentIp != ip {
    			go SendVoteToNeighbour(config, trans, address)
    		}
    	}
    }
    
    // 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 := manage_connection.CreateConnectionWithSpecifiedLocalAddress(config.Address, ipAddr)
    
    	mess := Message{
    		MessageType: "transaction",
    		MessageBody: trans,
    	}
    	fmt.Println("Sending message to neighbour", mess)
    	encoder := json.NewEncoder(conn)
    	err := encoder.Encode(mess)
    	if err != nil {
    		fmt.Println("Error while encoding the transaction", err)
    		os.Exit(1)
    	}
    	conn.Close()
    
    	fmt.Println("MessageBody successfully sent to neighbour")
    }
    
    // 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 := manage_connection.CreateConnection(address)
    
    	trans := Transaction{
    		Id:       strconv.FormatInt(transIDInt, 10),
    		Sender:   "",
    		Receiver: "",
    		Amount:   "",
    	}
    	mess := Message{
    		MessageType: "voteRequest",
    		MessageBody: trans,
    	}
    	fmt.Println("Sending vote request to node", mess)
    	encoder := json.NewEncoder(conn)
    	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) {
    	fmt.Println()
    	fmt.Println("Trying to connect to ", address)
    
    	conn := manage_connection.CreateConnection(address)
    
    	trans := Transaction{
    		Id:       strconv.FormatInt(transIDInt, 10),
    		Sender:   "",
    		Receiver: "",
    		Amount:   "",
    	}
    	mess := Message{
    		MessageType: "fakeRequest",
    		MessageBody: trans,
    	}
    
    	fmt.Println("Sending vote request to node", mess)
    	encoder := json.NewEncoder(conn)
    	err := encoder.Encode(mess)
    	if err != nil {
    		fmt.Println("Error while encoding the transaction", err)
    		os.Exit(1)
    	}
    	conn.Close()
    }
    
    // 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()
    	fmt.Println("Trying to connect to ", address)
    
    	conn := manage_connection.CreateConnection(address)
    
    	mess := Message{
    		MessageType: "listTransactionsRequest",
    		MessageBody: "",
    	}
    	fmt.Println("Sending vote request to node", mess)
    	encoder := json.NewEncoder(conn)
    	err := encoder.Encode(mess)
    	if err != nil {
    		fmt.Println("Error while encoding the transaction", err)
    		os.Exit(1)
    	}
    	conn.Close()
    }