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"
|
// "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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user