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

changed interaction with user

parent c2d5c814
No related branches found
No related tags found
1 merge request!2added file to separate client function from the server
...@@ -57,18 +57,79 @@ func userCreatedTransaction(config Config) Transaction { ...@@ -57,18 +57,79 @@ func userCreatedTransaction(config Config) Transaction {
return trans return trans
} }
func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { func ServerUserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
var database Database var database Database
for true { for true {
var operation string var operation string
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")
fmt.Println("1. Create a transaction") fmt.Println("1. Fabricate a fake transaction")
fmt.Println("2. Rate a transaction (from the client)") fmt.Println("2. Print all transactions")
fmt.Println("3. Fabricate a fake transaction") fmt.Println("4. Exit")
fmt.Println("4. Print all transactions") fmt.Print("Your choice: ")
fmt.Println("6. Exit") _, err := fmt.Scanln(&operation)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
switch operation {
case "1":
fmt.Println("You chose to fabricate a fake transaction")
fmt.Println("You chose to fake a transaction")
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)
}
tmpFakeTrans := userCreatedTransaction(config)
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 "2":
fmt.Println("You chose to print all transactions")
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
break
case "3":
fmt.Println("You chose to ask for all transactions of a given node")
break
case "4":
fmt.Println("You chose to exit")
return
default:
fmt.Println("You chose an invalid option")
break
}
}
}
func ClientUserInputLoop(clientConfig Config, isAlsoServer bool) {
for true {
var operation string
fmt.Println()
fmt.Println()
fmt.Println("Please enter the operation you want to do")
fmt.Println("1. Create a transaction and ask node to spread it")
fmt.Println("2. Ask node to rate a transaction")
fmt.Println("3. Ask node to fake a transaction")
fmt.Println("4. Ask node to list all transactions")
fmt.Println("5. Exit")
fmt.Print("Your choice: ") fmt.Print("Your choice: ")
_, err := fmt.Scanln(&operation) _, err := fmt.Scanln(&operation)
if err != nil { if err != nil {
...@@ -80,13 +141,11 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { ...@@ -80,13 +141,11 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
fmt.Println("You chose to create a transaction") fmt.Println("You chose to create a transaction")
if isAlsoServer { if isAlsoServer {
fmt.Println("Not yet implemented!") fmt.Println("Not yet implemented!")
//newTrans := userCreatedTransaction(config) //newTrans := userCreatedTransaction(clientConfig)
//createTransaction(newTrans) //createTransaction(newTrans)
} else { } else {
newTrans := userCreatedTransaction(config) newTrans := userCreatedTransaction(clientConfig)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) utilities.PrintNeighbors(clientConfig.Neighbours)
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
...@@ -100,13 +159,14 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { ...@@ -100,13 +159,14 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
fmt.Println("error :", err.Error()) fmt.Println("error :", err.Error())
os.Exit(1) os.Exit(1)
} }
Sender.SendTransactionToNeighbour(config, newTrans, config.Neighbours[neighbourIDInt].Address, strconv.Itoa(config.Neighbours[neighbourIDInt].Port)) Sender.SendTransactionToNeighbour(clientConfig, newTrans, clientConfig.Neighbours[neighbourIDInt].Address, strconv.Itoa(clientConfig.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")
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) fmt.Println("We will ask you for an index of a transaction to rate")
utilities.PrintingDatabaseToConsole(database) fmt.Println("The given node will send a request to all its neighbours to rate the transaction")
fmt.Println("The node will then print the result of the rating")
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)
...@@ -119,8 +179,9 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { ...@@ -119,8 +179,9 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
fmt.Println("error :", err.Error()) fmt.Println("error :", err.Error())
os.Exit(1) os.Exit(1)
} }
utilities.PrintNeighbors(config.Neighbours)
fmt.Println("Please enter the ID of the neighbour you want to send the transaction to") utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("Please enter the ID of the neighbour you want to send the rate request transaction to")
var neighbourID string var neighbourID string
_, err = fmt.Scanln(&neighbourID) _, err = fmt.Scanln(&neighbourID)
if err != nil { if err != nil {
...@@ -132,47 +193,70 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { ...@@ -132,47 +193,70 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
fmt.Println("error :", err.Error()) fmt.Println("error :", err.Error())
os.Exit(1) os.Exit(1)
} }
address := config.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(config.Neighbours[neighbourIDInt].Port) address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address) fmt.Println("Sending rate demand to ", address)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
Sender.SendVoteToNeighbour(config, database[transIDInt], address) Sender.SendVoteRequestToNode(clientConfig, transIDInt, address)
break break
case "3": case "3":
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")
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) fmt.Println("We will ask you for an index of a transaction to fake")
utilities.PrintingDatabaseToConsole(database) fmt.Println("and the node you want to ask to fake it")
fmt.Print("\nPlease enter the index of the transaction you want to overwrite by faking:") fmt.Println("The node will receive the request and change the transaction with a fake one")
fmt.Print("\nPlease enter the index of the transaction you want to fake:")
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)
} }
tmpFakeTrans := userCreatedTransaction(config) utilities.PrintNeighbors(clientConfig.Neighbours)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) fmt.Println("Please enter the ID of the neighbour you want to send the rate request transaction to")
database = ObjectStorageAPI.FakeTransaction(database[transIDInt], tmpFakeTrans, database) var neighbourID string
ObjectStorageAPI.WriteDatabaseToBlobStorage(database, objectStorage) _, err = fmt.Scanln(&neighbourID)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) if err != nil {
fmt.Println("Database after faking:") fmt.Println("error :", err.Error())
utilities.PrintingDatabaseToConsole(database) os.Exit(1)
case "4": }
fmt.Println("You chose to print all transactions") neighbourIDInt, err := strconv.ParseInt(neighbourID, 10, 64)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) if err != nil {
utilities.PrintingDatabaseToConsole(database) fmt.Println("error :", err.Error())
os.Exit(1)
}
address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
Sender.SendFakeRequestToNode(clientConfig, transIDInt, address)
break break
case "5": case "4":
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")
utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("We will ask you for the ID of the neighbour you want to ask for the transactions")
fmt.Println("The node will then print locally its transactions")
fmt.Println("Please enter the ID of the neighbour you want to send the print request transaction to")
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)
}
address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
Sender.SendListTransactionsRequestToNode(clientConfig, address)
break break
case "6": case "5":
fmt.Println("You chose to exit") fmt.Println("You chose to exit")
return return
default: default:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment