Skip to content
Snippets Groups Projects
Select Git revision
  • c8011a9c645111ee795fe5d96d0b38b828f1982c
  • main default protected
2 results

infra

Blame
  • Forked from Dojo Project (HES-SO) / Projects / Backend / DojoBackend
    Source project has a limited visibility.
    utilities.go 4.40 KiB
    package utilities
    
    import (
    	"fmt"
    	"math/rand"
    	"net"
    	. "node/types"
    	"strconv"
    	"time"
    )
    
    func PrintTransaction(trans Transaction) {
    	fmt.Println("--------------------")
    	fmt.Println("Printing transaction")
    	fmt.Println("The id is ", trans.Id)
    	fmt.Println("The sender is ", trans.Sender)
    	fmt.Println("The receiver is ", trans.Receiver)
    	fmt.Println("The amount is ", trans.Amount)
    	fmt.Println("--------------------")
    }
    
    func ListAllTransactions(db Database) {
    	for i, transaction := range db {
    		fmt.Println("***********INDEX N=,", i, "****************")
    		PrintTransaction(transaction)
    		fmt.Println("***********************************")
    	}
    }
    
    func PrintNeighbors(neighbors []Neighbors) {
    	fmt.Println("Printing neighbors")
    	for i, neighbor := range neighbors {
    		fmt.Println("***********INDEX N=,", i, "****************")
    		fmt.Println("The id is ", neighbor.ID)
    		fmt.Println("The address is ", neighbor.Address)
    		fmt.Println("The port is ", neighbor.Port)
    		fmt.Println("***********************************")
    	}
    }
    
    func PrintConfig(config Config) {
    	fmt.Println("Printing config")
    	fmt.Println("The id is ", config.ID)
    	fmt.Println("The address is ", config.Address)
    	fmt.Println("The port is ", config.Port)
    	PrintNeighbors(config.Neighbours)
    }
    
    func PrintingDatabaseToConsole(database Database) {
    	for i, trans := range database {
    		fmt.Println("--------------------")
    		fmt.Printf("\nListing transactions number %d in database\n", i)
    		fmt.Printf("Transaction id: %s\n", trans.Id)
    		fmt.Printf("Transaction sender: %s\n", trans.Sender)
    		fmt.Printf("Transaction receiver: %s\n", trans.Receiver)
    		fmt.Printf("Transaction amount: %s\n", trans.Amount)
    		fmt.Println("--------------------")
    	}
    }
    
    func CompareTransactions(trans1 Transaction, trans2 Transaction, ack AckTransaction) AckTransaction {
    	newAck := AckTransaction{
    		Id:                  ack.Id,
    		AmountOfCorrectNode: ack.AmountOfCorrectNode,
    		TotalNodes:          ack.TotalNodes,
    	}
    
    	if trans1.Id != trans2.Id {
    		return newAck
    	}
    
    	areBothTransactionEquals := true
    	if trans1.Receiver != trans2.Receiver {
    		areBothTransactionEquals = false
    	}
    	if trans1.Sender != trans2.Sender {
    		areBothTransactionEquals = false
    	}
    	if trans1.Amount != trans2.Amount {
    		areBothTransactionEquals = false
    	}
    
    	if areBothTransactionEquals {
    		newAck.AmountOfCorrectNode += 1
    	}
    	newAck.TotalNodes += 1
    
    	return newAck
    }
    
    func PrintConnection(connection net.Conn, connectionNumber int) {
    	fmt.Println("*****************************************")
    	fmt.Println("Processing client request number ", connectionNumber)
    	fmt.Println("The process-manage-connection is ", connection)
    	fmt.Println("The remote address is ", connection.RemoteAddr())
    	fmt.Println("The local address is ", connection.LocalAddr())
    	fmt.Println("*****************************************")
    }
    
    // TranslateMessageToAckTransaction translates a message to an ack transaction
    // the geniuses who made unmarshable take int as float64
    func TranslateMessageToAckTransaction(mess Message) AckTransaction {
    	var transactionId string
    	var amountOfCorrectNode int
    	var totalNode int
    
    	var body map[string]interface{} = mess.MessageBody.(map[string]interface{})
    	transactionId = body["id"].(string)
    	amountOfCorrectNode = int(body["amountOfCorrectNode"].(float64))
    	totalNode = int(body["totalNodes"].(float64))
    
    	return AckTransaction{
    		Id:                  transactionId,
    		AmountOfCorrectNode: amountOfCorrectNode,
    		TotalNodes:          totalNode,
    	}
    }
    
    func TranslateMessageToTransaction(mess Message) Transaction {
    	var newTrans Transaction
    
    	var body map[string]interface{} = mess.MessageBody.(map[string]interface{})
    	newTrans.Id = body["id"].(string)
    	newTrans.Sender = body["sender"].(string)
    	newTrans.Receiver = body["receiver"].(string)
    	newTrans.Amount = body["amount"].(string)
    
    	return newTrans
    
    }
    
    func RandomString() string {
    	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    	return strconv.Itoa(r.Int())
    }
    
    func PrintTransactionDoneMessage() {
    	fmt.Println("***********************************")
    	fmt.Println("All transactions have been received")
    	fmt.Println("***********************************")
    }
    
    func PrintAckTransaction(ack AckTransaction) {
    	fmt.Println("***********************************")
    	fmt.Println("Printing ack transaction")
    	fmt.Println("The id is ", ack.Id)
    	fmt.Println("The amount of correct node is ", ack.AmountOfCorrectNode)
    	fmt.Println("The total nodes is ", ack.TotalNodes)
    	fmt.Println("***********************************")
    }