working implementation of modular transports, generator still pubsub only

This commit is contained in:
imw
2019-02-07 13:32:49 +01:00
parent 5ce8bb33ee
commit 7128e24db3
4 changed files with 12 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ import (
// "bytes" // "bytes"
// "io/ioutil" // "io/ioutil"
"github.com/vocdoni/dvote-relay/types" "github.com/vocdoni/dvote-relay/types"
"github.com/vocdoni/dvote-relay/data" "github.com/vocdoni/dvote-relay/net"
) )
func makeBallot() string { func makeBallot() string {
@@ -85,7 +85,7 @@ func main() {
case <- timer.C: case <- timer.C:
var jsonStr = makeEnvelope(makeBallot()) var jsonStr = makeEnvelope(makeBallot())
fmt.Println(jsonStr) fmt.Println(jsonStr)
data.PsPublish(topic, jsonStr) net.PsPublish(topic, jsonStr)
default: default:
continue continue
} }

View File

@@ -16,7 +16,7 @@ type HttpHandle struct {
path string path string
} }
func (h HttpHandle) Init(c string) error { func (h *HttpHandle) Init(c string) error {
//split c to port and path //split c to port and path
cs := strings.Split(c, "/") cs := strings.Split(c, "/")
h.port = cs[0] h.port = cs[0]
@@ -66,11 +66,11 @@ func parse(rw http.ResponseWriter, request *http.Request) {
io.WriteString(rw, string(j)) io.WriteString(rw, string(j))
} }
func (h HttpHandle) Listen() error { func (h *HttpHandle) Listen() error {
http.HandleFunc(h.path, parse) http.HandleFunc(h.path, parse)
//add waitgroup //add waitgroup
func() { func() {
fmt.Println("serving on " + h.port) fmt.Println("serving on " + h.port + "/" + h.path)
err := http.ListenAndServe(":" + h.port, nil) err := http.ListenAndServe(":" + h.port, nil)
if err != nil { if err != nil {
return return

View File

@@ -5,8 +5,8 @@ import (
) )
type Transport interface { type Transport interface {
Init(c string) error
Listen() error Listen() error
Init(c string) error
} }
type TransportID int type TransportID int
@@ -30,11 +30,11 @@ func TransportIDFromString(i string) TransportID {
func Init(t TransportID) (Transport, error) { func Init(t TransportID) (Transport, error) {
switch t { switch t {
case PubSub : case PubSub :
var p PubSubHandle p := new(PubSubHandle)
p.Init("vocdoni_pubsub_testing") p.Init("vocdoni_pubsub_testing")
return p, nil return p, nil
case HTTP : case HTTP :
var h HttpHandle h := new(HttpHandle)
h.Init("8080/submit") h.Init("8080/submit")
return h, nil return h, nil
default: default:

View File

@@ -34,19 +34,19 @@ func PsPublish(topic, data string) error {
return nil return nil
} }
func (p PubSubHandle) Init(topic string) error { func (p *PubSubHandle) Init(topic string) error {
p.topic = topic p.topic = topic
p.subscription = PsSubscribe(p.topic) p.subscription = PsSubscribe(p.topic)
fmt.Println("Subscribed > " + p.topic)
return nil return nil
} }
func (p PubSubHandle) Listen() error { func (p *PubSubHandle) Listen() error {
var msg *shell.Message var msg *shell.Message
var err error var err error
for { for {
msg, err = p.subscription.Next() msg, err = p.subscription.Next()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "recieve error: %s", err)
return err return err
} }
@@ -74,6 +74,6 @@ func (p PubSubHandle) Listen() error {
} }
} }
func (p PubSubHandle) Send(data string) error { func (p *PubSubHandle) Send(data string) error {
return PsPublish(p.topic, data) return PsPublish(p.topic, data)
} }