Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
perso-distributed-systems
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xavier.perret
perso-distributed-systems
Commits
8ebb6ef3
Commit
8ebb6ef3
authored
2 years ago
by
Xavier Perret
Browse files
Options
Downloads
Patches
Plain Diff
changed interaction with user
parent
c2d5c814
No related branches found
No related tags found
1 merge request
!2
added file to separate client function from the server
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/command-line/userinput.go
+122
-38
122 additions, 38 deletions
app/command-line/userinput.go
with
122 additions
and
38 deletions
app/command-line/userinput.go
+
122
−
38
View file @
8ebb6ef3
...
@@ -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
Server
UserInputLoop
(
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
(
"
\n
Please 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(c
lientC
onfig)
//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
(
c
lientC
onfig
,
newTrans
,
c
lientC
onfig
.
Neighbours
[
neighbourIDInt
]
.
Address
,
strconv
.
Itoa
(
c
lientC
onfig
.
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
(
"
\n
Please enter the index of the transaction you want to rate:"
)
fmt
.
Print
(
"
\n
Please 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
:=
c
lientC
onfig
.
Neighbours
[
neighbourIDInt
]
.
Address
+
":"
+
strconv
.
Itoa
(
c
lientC
onfig
.
Neighbours
[
neighbourIDInt
]
.
Port
)
fmt
.
Println
(
"Sending rate demand to "
,
address
)
fmt
.
Println
(
"Sending rate demand to "
,
address
)
database
=
ObjectStorageAPI
.
ReadDatabaseFromBlobStorage
(
objectStorage
)
Sender
.
SendVote
ToNeighbour
(
config
,
database
[
transIDInt
]
,
address
)
Sender
.
SendVote
RequestToNode
(
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
(
"
\n
Please enter the index of the transaction you want to overwrite by faking:
"
)
fmt
.
Print
ln
(
"The node will receive the request and change the transaction with a fake one
"
)
fmt
.
Print
(
"
\n
Please 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
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment