From 8ebb6ef32862f32ef0784e7a7a94231e141644ca Mon Sep 17 00:00:00 2001 From: Xavier Perret <xa.perret@outlook.com> Date: Sat, 29 Oct 2022 16:09:57 +0200 Subject: [PATCH] changed interaction with user --- app/command-line/userinput.go | 160 ++++++++++++++++++++++++++-------- 1 file changed, 122 insertions(+), 38 deletions(-) diff --git a/app/command-line/userinput.go b/app/command-line/userinput.go index fcb4adc..306a6ca 100644 --- a/app/command-line/userinput.go +++ b/app/command-line/userinput.go @@ -57,18 +57,79 @@ func userCreatedTransaction(config Config) Transaction { return trans } -func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { +func ServerUserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { var database Database 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") - fmt.Println("2. Rate a transaction (from the client)") - fmt.Println("3. Fabricate a fake transaction") - fmt.Println("4. Print all transactions") - fmt.Println("6. Exit") + fmt.Println("1. Fabricate a fake transaction") + fmt.Println("2. Print all transactions") + fmt.Println("4. Exit") + fmt.Print("Your choice: ") + _, 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: ") _, err := fmt.Scanln(&operation) if err != nil { @@ -80,13 +141,11 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { fmt.Println("You chose to create a transaction") if isAlsoServer { fmt.Println("Not yet implemented!") - //newTrans := userCreatedTransaction(config) + //newTrans := userCreatedTransaction(clientConfig) //createTransaction(newTrans) } else { - newTrans := userCreatedTransaction(config) - database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) - database = ObjectStorageAPI.AddTransactionToBlobStorage(newTrans, database, objectStorage) - utilities.PrintNeighbors(config.Neighbours) + newTrans := userCreatedTransaction(clientConfig) + utilities.PrintNeighbors(clientConfig.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 @@ -100,13 +159,14 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { fmt.Println("error :", err.Error()) 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 case "2": fmt.Println("You chose to rate a transaction") - database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) - utilities.PrintingDatabaseToConsole(database) + fmt.Println("We will ask you for an index of a transaction to rate") + 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:") var transID string _, err := fmt.Scanln(&transID) @@ -119,8 +179,9 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { fmt.Println("error :", err.Error()) 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 _, err = fmt.Scanln(&neighbourID) if err != nil { @@ -132,47 +193,70 @@ func UserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) { fmt.Println("error :", err.Error()) 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) - database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) - Sender.SendVoteToNeighbour(config, database[transIDInt], address) + + Sender.SendVoteRequestToNode(clientConfig, transIDInt, address) break case "3": - 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:") - + fmt.Println("We will ask you for an index of a transaction to fake") + fmt.Println("and the node you want to ask to fake it") + 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 _, 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 "4": - fmt.Println("You chose to print all transactions") - database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage) - utilities.PrintingDatabaseToConsole(database) + 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 + _, 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.SendFakeRequestToNode(clientConfig, transIDInt, address) break - case "5": + case "4": 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 - case "6": + case "5": fmt.Println("You chose to exit") return default: -- GitLab