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

improved userinput

parent a9c6b071
No related branches found
No related tags found
1 merge request!2added file to separate client function from the server
......@@ -2,7 +2,11 @@ package command_line
import (
"fmt"
"math/rand"
ObjectStorageAPI "node/object-storage"
Sender "node/sender"
. "node/types"
"node/utilities"
"os"
"strconv"
"time"
......@@ -53,12 +57,10 @@ func userCreatedTransaction(config Config) Transaction {
return trans
}
func UserInputLoop(config Config, isAlsoServer bool) {
func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
var database Database
for true {
var operation string
if !ServerReady {
continue
}
fmt.Println()
fmt.Println()
fmt.Println("Please enter the operation you want to do")
......@@ -82,8 +84,9 @@ func UserInputLoop(config Config, isAlsoServer bool) {
//createTransaction(newTrans)
} else {
newTrans := userCreatedTransaction(config)
addTransactionToDb(newTrans)
printAllNeighbours(config)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
database = ObjectStorageAPI.AddTransactionToBlobStorage(newTrans, database, objectStorage)
utilities.PrintNeighbors(config.Neighbours)
fmt.Println("TRANSACTION READY TO BE SENT")
fmt.Println("Please enter the ID of the neighbour you want to send the transaction to")
var neighbourID string
......@@ -97,12 +100,13 @@ func UserInputLoop(config Config, isAlsoServer bool) {
fmt.Println("error :", err.Error())
os.Exit(1)
}
sendTransactionToNeighbour(config, newTrans, config.Neighbours[neighbourIDInt].Address, strconv.Itoa(config.Neighbours[neighbourIDInt].Port))
Sender.SendTransactionToNeighbour(config, newTrans, config.Neighbours[neighbourIDInt].Address, strconv.Itoa(config.Neighbours[neighbourIDInt].Port))
}
break
case "2":
fmt.Println("You chose to rate a transaction")
listAllTransactions()
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
fmt.Print("\nPlease enter the index of the transaction you want to rate:")
var transID string
_, err := fmt.Scanln(&transID)
......@@ -115,7 +119,7 @@ func UserInputLoop(config Config, isAlsoServer bool) {
fmt.Println("error :", err.Error())
os.Exit(1)
}
printAllNeighbours(config)
utilities.PrintNeighbors(config.Neighbours)
fmt.Println("Please enter the ID of the neighbour you want to send the transaction to")
var neighbourID string
_, err = fmt.Scanln(&neighbourID)
......@@ -130,53 +134,40 @@ func UserInputLoop(config Config, isAlsoServer bool) {
}
address := config.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(config.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
sendVoteToNeighbour(config, DB[transIDInt], address)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
Sender.SendVoteToNeighbour(config, database[transIDInt], address)
break
case "3":
fmt.Println("You chose to fabricate a fake transaction")
fmt.Println("You chose to fake a transaction")
listAllTransactions()
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
fmt.Print("\nPlease enter the index of the transaction you want to overwrite by faking:")
var transID string
_, err := fmt.Scanln(&transID)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
transIDInt, err := strconv.ParseInt(transID, 10, 64)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
printAllNeighbours(config)
fmt.Println("Please enter the ID of the neighbour you to ask to fake the given transaction")
var neighbourID string
_, err = fmt.Scanln(&neighbourID)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
neighbourIDInt, err := strconv.ParseInt(neighbourID, 10, 64)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
tmpFakeTrans := userCreatedTransaction(config)
transToFake := DB[transIDInt]
fakeTrans := Transaction{
Id: transToFake.Id,
Sender: tmpFakeTrans.Sender,
Receiver: tmpFakeTrans.Receiver,
Amount: tmpFakeTrans.Amount,
}
ip := config.Neighbours[neighbourIDInt].Address
port := strconv.Itoa(config.Neighbours[neighbourIDInt].Port)
address := ip + ":" + port
sendFakeTransactionToNeighbour(fakeTrans, address)
break
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
database = ObjectStorageAPI.FakeTransaction(database[transIDInt], tmpFakeTrans, database)
ObjectStorageAPI.WriteDatabaseToBlobStorage(database, objectStorage)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
fmt.Println("Database after faking:")
utilities.PrintingDatabaseToConsole(database)
case "4":
fmt.Println("You chose to print all transactions")
listAllTransactions()
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
break
case "5":
fmt.Println("You chose to ask for all transactions of a given node")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment