Browse Source

working implementation of modular transports, generator still pubsub only

feature_modular_transports
imw 5 years ago
parent
commit
7128e24db3
4 changed files with 12 additions and 12 deletions
  1. +2
    -2
      cmd/generator/generator.go
  2. +3
    -3
      net/http.go
  3. +3
    -3
      net/net.go
  4. +4
    -4
      net/pubsub.go

+ 2
- 2
cmd/generator/generator.go

@ -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
}

+ 3
- 3
net/http.go

@ -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

+ 3
- 3
net/net.go

@ -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:

+ 4
- 4
net/pubsub.go

@ -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)
}

Loading…
Cancel
Save