Skip to content

Commit

Permalink
fix codification to use codec/base64 in publisher and test_Service
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Dec 3, 2018
1 parent c95e79d commit 808f84a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions gossip/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package publisher

import (
"encoding/json"
"encoding/base64"

"github.com/bbva/qed/gossip"
"github.com/bbva/qed/log"
Expand Down Expand Up @@ -54,11 +54,12 @@ func NewPublisher(conf *Config) *Publisher {
}

func (p *Publisher) Process(b *protocol.BatchSnapshots) {
body, err := json.Marshal(&b)
buf, err := b.Encode()
if err != nil {
log.Debug("\nPublisher: Error marshalling: %s", err.Error())
return
}
body := []byte(base64.StdEncoding.EncodeToString(buf))

req := fasthttp.AcquireRequest()
// TODO: Implement send to different endpoints
Expand Down
25 changes: 18 additions & 7 deletions tests/gossip/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -37,15 +38,17 @@ func (kv *kv) Put(b *protocol.BatchSnapshots) {
defer kv.Unlock()

for _, s := range b.Snapshots {
fmt.Println("Storing ", s.Snapshot.Version, " snapshot")
kv.d[s.Snapshot.Version] = s
}

}

func (kv *kv) Get(version uint64) *protocol.SignedSnapshot {
func (kv *kv) Get(version uint64) (v *protocol.SignedSnapshot, ok bool) {
kv.Lock()
defer kv.Unlock()
return kv.d[version]
v, ok = kv.d[version]
return v, ok
}

func (s *stats) Add(nodeType string, id, v int) {
Expand Down Expand Up @@ -93,7 +96,6 @@ func statHandler(w http.ResponseWriter, r *http.Request) {
}

func postBatchHandler(kv *kv) func(http.ResponseWriter, *http.Request) {

return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// Decode batch to get signed snapshots and batch version.
Expand All @@ -103,7 +105,12 @@ func postBatchHandler(kv *kv) func(http.ResponseWriter, *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = b.Decode(buf)
sDec, err := base64.StdEncoding.DecodeString(string(buf))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = b.Decode(sDec)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -125,9 +132,13 @@ func getSnapshotHandler(kv *kv) func(http.ResponseWriter, *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b := kv.Get(uint64(version))
b, ok := kv.Get(uint64(version))
if !ok {
http.Error(w, fmt.Sprintf("Version not found: %v", version), http.StatusMethodNotAllowed)
return
}
buf, err := b.Encode()
_, err = w.Write(buf)
_, err = w.Write([]byte(base64.StdEncoding.EncodeToString(buf)))
if err != nil {
fmt.Println("ERROR: %v", err)
}
Expand Down Expand Up @@ -158,6 +169,6 @@ func main() {

http.HandleFunc("/stat", statHandler)
http.HandleFunc("/batch", postBatchHandler(&store))
http.HandleFunc("/snapshot/{id}", getSnapshotHandler(&store))
http.HandleFunc("/snapshot", getSnapshotHandler(&store))
log.Fatal(http.ListenAndServe("127.0.0.1:8888", nil))
}

0 comments on commit 808f84a

Please sign in to comment.