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