mirror of
https://github.com/arnaucube/go-dvote.git
synced 2026-02-28 05:26:46 +01:00
working implementation of modular transports, generator still pubsub only
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
// "bytes"
|
||||
// "io/ioutil"
|
||||
"github.com/vocdoni/dvote-relay/types"
|
||||
"github.com/vocdoni/dvote-relay/data"
|
||||
"github.com/vocdoni/dvote-relay/net"
|
||||
)
|
||||
|
||||
func makeBallot() string {
|
||||
@@ -85,7 +85,7 @@ func main() {
|
||||
case <- timer.C:
|
||||
var jsonStr = makeEnvelope(makeBallot())
|
||||
fmt.Println(jsonStr)
|
||||
data.PsPublish(topic, jsonStr)
|
||||
net.PsPublish(topic, jsonStr)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ type HttpHandle struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (h HttpHandle) Init(c string) error {
|
||||
func (h *HttpHandle) Init(c string) error {
|
||||
//split c to port and path
|
||||
cs := strings.Split(c, "/")
|
||||
h.port = cs[0]
|
||||
@@ -66,11 +66,11 @@ func parse(rw http.ResponseWriter, request *http.Request) {
|
||||
io.WriteString(rw, string(j))
|
||||
}
|
||||
|
||||
func (h HttpHandle) Listen() error {
|
||||
func (h *HttpHandle) Listen() error {
|
||||
http.HandleFunc(h.path, parse)
|
||||
//add waitgroup
|
||||
func() {
|
||||
fmt.Println("serving on " + h.port)
|
||||
fmt.Println("serving on " + h.port + "/" + h.path)
|
||||
err := http.ListenAndServe(":" + h.port, nil)
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
)
|
||||
|
||||
type Transport interface {
|
||||
Init(c string) error
|
||||
Listen() error
|
||||
Init(c string) error
|
||||
}
|
||||
|
||||
type TransportID int
|
||||
@@ -30,11 +30,11 @@ func TransportIDFromString(i string) TransportID {
|
||||
func Init(t TransportID) (Transport, error) {
|
||||
switch t {
|
||||
case PubSub :
|
||||
var p PubSubHandle
|
||||
p := new(PubSubHandle)
|
||||
p.Init("vocdoni_pubsub_testing")
|
||||
return p, nil
|
||||
case HTTP :
|
||||
var h HttpHandle
|
||||
h := new(HttpHandle)
|
||||
h.Init("8080/submit")
|
||||
return h, nil
|
||||
default:
|
||||
|
||||
@@ -34,19 +34,19 @@ func PsPublish(topic, data string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p PubSubHandle) Init(topic string) error {
|
||||
func (p *PubSubHandle) Init(topic string) error {
|
||||
p.topic = topic
|
||||
p.subscription = PsSubscribe(p.topic)
|
||||
fmt.Println("Subscribed > " + p.topic)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p PubSubHandle) Listen() error {
|
||||
func (p *PubSubHandle) Listen() error {
|
||||
var msg *shell.Message
|
||||
var err error
|
||||
for {
|
||||
msg, err = p.subscription.Next()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "recieve error: %s", 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user