Browse Source

main loop signaling working, db setup in batch functions :-(

batching_initial_approach
imw 6 years ago
parent
commit
dadc5e3291
4 changed files with 88 additions and 38 deletions
  1. +50
    -4
      batch/batch.go
  2. +21
    -24
      main.go
  3. +13
    -6
      net/net.go
  4. +4
    -4
      types/types.go

+ 50
- 4
batch/batch.go

@ -7,20 +7,66 @@ import (
"github.com/vocdoni/dvote-relay/types"
)
var DBPath string
var BDBPath string
var DB *leveldb.DB
var BDB *leveldb.DB
var BatchSignal chan bool
var BatchSize int
var currentBatchSize int
var err error
func Setup() {
DB, err = leveldb.OpenFile(DBPath, nil)
if err != nil {
panic(err)
}
defer DB.Close()
BDB, err = leveldb.OpenFile(BDBPath, nil)
if err != nil {
panic(err)
}
defer BDB.Close()
fmt.Println(BDB)
}
//add (queue for counting)
func Add(p types.Packet, bdb *leveldb.DB) error {
err := bdb.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil)
func Add(p types.Packet) error {
BDB, err = leveldb.OpenFile(BDBPath, nil)
if err != nil {
panic(err)
}
defer BDB.Close()
err := BDB.Put([]byte(fmt.Sprintf("%v", p.Nullifier)),[]byte(fmt.Sprintf("%v", p)), nil)
if err != nil {
return err
}
currentBatchSize++
fmt.Println(currentBatchSize)
if currentBatchSize >= BatchSize {
fmt.Println("sending ready")
BatchSignal <- true
fmt.Println("sent ready")
}
fmt.Println("returning")
return nil
}
//create (return batch)
func Create(bdb *leveldb.DB) []byte {
func Create() []byte {
fmt.Println("Creating batch")
DB, err = leveldb.OpenFile(DBPath, nil)
if err != nil {
panic(err)
}
defer DB.Close()
var b []byte
iter := bdb.NewIterator(nil, nil)
iter := BDB.NewIterator(nil, nil)
for iter.Next() {
err := iter.Error()
if err != nil {

+ 21
- 24
main.go

@ -2,53 +2,50 @@ package main
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"time"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/net"
)
var dbPath = "$HOME/.dvote/relay.db"
var bdbPath = "$HOME/.dvote/batch.db"
var batchSeconds = 3 //seconds
var batchSeconds = 30 //seconds
var batchSize = 3 //packets
var err error
var db *leveldb.DB
var bdb *leveldb.DB
var batchTimer *time.Ticker
var batchSignal chan int
var batchSignal chan bool
var signal bool
func setup() {
db, err = leveldb.OpenFile(dbPath, nil)
if err != nil {
panic(err)
}
defer db.Close()
bdb, err = leveldb.OpenFile(bdbPath, nil)
if err != nil {
panic(err)
}
defer bdb.Close()
batchTimer = time.NewTicker(time.Second * time.Duration(batchSeconds))
//create batch signal channel
batchSignal = make(chan bool)
batch.DBPath = dbPath
batch.BDBPath = bdbPath
batch.BatchSignal = batchSignal
batch.BatchSize = batchSize
}
func main() {
setup()
// batch.Setup()
fmt.Println("Entering main loop")
go net.Listen("8080")
for {
select {
case <- batchTimer.C:
fmt.Println("Timer triggered")
fmt.Println(batch.Create(bdb))
fmt.Println(batch.Create())
//replace with chain link
case <- batchSignal:
fmt.Println("Signal triggered")
fmt.Println(batch.Create(bdb))
// case <- stopSignal:
// return
case signal := <-batchSignal:
if signal == true {
fmt.Println("Signal triggered")
fmt.Println(batch.Create())
}
default:
continue
}
}
}

+ 13
- 6
net/net.go

@ -4,16 +4,17 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"io"
"github.com/vocdoni/dvote-relay/batch"
"github.com/vocdoni/dvote-relay/types"
)
func parse(rw http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
var s Submission
var s types.Submission
var p types.Packet
err := decoder.Decode(&s)
if err != nil {
@ -24,19 +25,25 @@ func parse(rw http.ResponseWriter, request *http.Request) {
//check key
//decrypt
//check franchise
err = batch.add(p)
//construct packet
p.PID = 1
p.Nullifier = []byte{1,2,3}
p.Vote = []byte{4,5,6}
p.Franchise = []byte{7,8,9}
err = batch.Add(p)
if err != nil {
return err
panic(err)
}
j, err := json.Marshal(s)
io.WriteString(rw, string(j))
}
func listen(port string) {
func Listen(port string) {
http.HandleFunc("/submit", parse)
//add waitgroup
go func() {
func() {
fmt.Println("serving on " + port)
err := http.ListenAndServe(":" + port, nil)
if err != nil {

+ 4
- 4
types/types.go

@ -22,9 +22,9 @@ type Batch struct {
}
type Packet struct {
PID uint
Nullifier string
Vote string
Franchise string
PID int
Nullifier []byte
Vote []byte
Franchise []byte
}

Loading…
Cancel
Save