Skip to content
Snippets Groups Projects
Commit 7d58b3e3 authored by julien.debray's avatar julien.debray
Browse files

Ajout fake et list

parent 5b1c497c
No related branches found
No related tags found
No related merge requests found
......@@ -6,13 +6,14 @@ import (
"math/rand"
"net"
"os"
"strconv"
"time"
"gopkg.in/yaml.v3"
)
const (
SERVER_HOST = "127.0.0.1"
SERVER_HOST = "4.233.112.40"
SERVER_PORT = "9876"
SERVER_TYPE = "tcp"
)
......@@ -26,9 +27,9 @@ type Transaction struct {
type Data struct {
Sender int `yaml:"sender"` // = sender's node ID or 0 if sender is not a node
Create bool `yaml:"create"` // create transaction or verify
voteOK int `yaml:"voteOK"` // counter for verify
voteKO int `yaml:"voteKO"` // counter for verify
Action int `yaml:"create"` // action to do
voteOK int `yaml:"voteOK"` // counter for vote
voteKO int `yaml:"voteKO"` // counter for vote
Transaction struct { // transaction
ID int `yaml:"id"`
Sender string `yaml:"sender"`
......@@ -38,17 +39,9 @@ type Data struct {
}
func main() {
if os.Args[1] != "0" && os.Args[1] != "1" {
fmt.Print("Use: go run client.go x with x = 0 / 1\n")
os.Exit(1)
}
var create bool
if os.Args[1] == "0" {
create = false
} else {
create = true
}
data := createData(create)
action, _ := strconv.Atoi(os.Args[1])
// action -> 0: vote / 1: create / 2: fake / 3: list
data := createData(action)
str, _ := yaml.Marshal(data)
connection, err := net.Dial(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
......@@ -60,23 +53,23 @@ func main() {
func createTransaction() Transaction {
var transaction Transaction
rand2 := rand.New(rand.NewSource(time.Now().UnixNano()))
transaction.id = rand2.Intn(1000)
transaction.id = rand2.Intn(10000)
transaction.sender = "A"
transaction.receiver = "B"
transaction.amount = 100
return transaction
}
func createData(create bool) Data {
func createData(action int) Data {
var data Data
data.Sender = 0
data.voteKO = 0
data.voteOK = 0
data.Create = create
data.Action = action
transaction := createTransaction()
if create {
if action == 1 {
data.Transaction.ID = transaction.id
} else {
} else if action != 3 {
var transactionID int
fmt.Scanf("%d", &transactionID)
data.Transaction.ID = transactionID
......
......@@ -40,9 +40,9 @@ type Transaction struct {
type Data struct {
Sender int `yaml:"sender"` // = sender's node ID or 0 if sender is not a node
Create bool `yaml:"create"` // create transaction or verify
voteOK int `yaml:"voteOK"` // counter for verify
voteKO int `yaml:"voteKO"` // counter for verify
Action int `yaml:"create"` // action to do
voteOK int `yaml:"voteOK"` // counter for vote
voteKO int `yaml:"voteKO"` // counter for vote
Transaction struct { // transaction
ID int `yaml:"id"`
Sender string `yaml:"sender"`
......@@ -84,10 +84,14 @@ func main() {
data := parseData(connection)
connection.Write([]byte("ok"))
if data.Create {
createTransaction(data)
} else {
if data.Action == 0 {
fmt.Print("Rate: " + strconv.Itoa(vote(data, strings.Split(connection.RemoteAddr().String(), ":")[0])) + "\n")
} else if data.Action == 1 {
createTransaction(data)
} else if data.Action == 2 {
fake(data)
} else if data.Action == 3 {
list()
}
connection.Close()
}
......@@ -234,6 +238,29 @@ func blobExists(transactionId int) bool {
return false
}
func list() {
pager := azureClient.NewListBlobsFlatPager(containerName, nil)
for pager.More() {
resp, err := pager.NextPage(context.TODO())
handleError(err)
for _, v := range resp.Segment.BlobItems {
id, _ := strconv.Atoi(*v.Name)
transaction := downloadBlob(id)
data := strconv.Itoa(transaction.id) + ";" + transaction.sender + ";" + transaction.receiver + ";" + fmt.Sprintf("%f", transaction.amount)
fmt.Println("Transaction " + strconv.Itoa(id) + ": " + data)
}
}
}
func fake(data Data) {
transaction := parseTransaction(data)
if blobExists(transaction.id) {
transaction := downloadBlob(transaction.id)
transaction.amount = 20
uploadBlob(transaction)
}
}
func handleError(err error) {
if err != nil {
log.Fatal(err.Error())
......
......@@ -4,5 +4,5 @@ neighbours:
- id: 2
address: "20.216.148.5"
- id: 3
address: "20.123.192.218"
\ No newline at end of file
- id: 4
address: "20.224.123.38"
\ No newline at end of file
......@@ -4,5 +4,5 @@ neighbours:
- id: 2
address: "20.216.148.5"
- id: 1
address: "4.233.112.40"
\ No newline at end of file
- id: 6
address: "20.203.221.39"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment