Browse Source

Consolidating asymetric messages

feature/pss_asym
p4u 5 years ago
parent
commit
0734f03a83
2 changed files with 31 additions and 7 deletions
  1. +24
    -4
      cmd/pssTest/psstest.go
  2. +7
    -3
      net/swarm/swarm.go

+ 24
- 4
cmd/pssTest/psstest.go

@ -10,26 +10,46 @@ import (
) )
func main() { func main() {
if len(os.Args) < 2 {
fmt.Println("Use <sym|asym> <key>")
return
}
sn := new(swarm.SwarmNet) sn := new(swarm.SwarmNet)
err := sn.Init() err := sn.Init()
if err != nil { if err != nil {
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)
return return
} }
key := os.Args[1]
err = sn.SetLog("crit")
if err != nil {
fmt.Printf("Cannot set loglevel %v\n", err)
}
kind := os.Args[1]
topic := "vocdoni_test" topic := "vocdoni_test"
var key string
if kind == "sym" || kind == "asym" {
key = os.Args[2]
sn.PssSub(kind, key, topic, "")
defer sn.PssTopics[topic].Unregister()
} else {
fmt.Println("First parameter must be sym or asym")
return
}
sn.PssSub("asym", key, topic, "")
go func() { go func() {
for { for {
msg := <-sn.PssTopics[topic].Delivery msg := <-sn.PssTopics[topic].Delivery
fmt.Printf("Pss received: %s\n", msg)
fmt.Printf("<- Pss received: %s\n", msg)
} }
}() }()
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
for { for {
err := sn.PssPub("asym", key, topic, fmt.Sprintf("Hello world from %s", hostname), "")
fmt.Printf("-> Sending %s pss to [%s]\n", kind, key)
err := sn.PssPub(kind, key, topic, fmt.Sprintf("Hello world from %s", hostname), "")
log.Info("pss sent", "err", err) log.Info("pss sent", "err", err)
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
} }

+ 7
- 3
net/swarm/swarm.go

@ -132,14 +132,18 @@ type SwarmNet struct {
Ports *swarmPorts Ports *swarmPorts
} }
func (sn *SwarmNet) SetLog() {
func (sn *SwarmNet) SetLog(level string) error {
// ensure good log formats for terminal // ensure good log formats for terminal
// handle verbosity flag // handle verbosity flag
loglevel, err := log.LvlFromString(level)
if err != nil {
return err
}
hs := log.StreamHandler(os.Stderr, log.TerminalFormat(true)) hs := log.StreamHandler(os.Stderr, log.TerminalFormat(true))
loglevel := log.LvlInfo
hf := log.LvlFilterHandler(loglevel, hs) hf := log.LvlFilterHandler(loglevel, hs)
h := log.CallerFileHandler(hf) h := log.CallerFileHandler(hf)
log.Root().SetHandler(h) log.Root().SetHandler(h)
return nil
} }
func (sn *SwarmNet) PrintStats() { func (sn *SwarmNet) PrintStats() {
@ -177,7 +181,7 @@ func (sn *SwarmNet) Init() error {
os.MkdirAll(sn.Datadir, 0755) os.MkdirAll(sn.Datadir, 0755)
} }
sn.SetLog()
sn.SetLog("info")
sn.Ports = NewSwarmPorts() sn.Ports = NewSwarmPorts()
// create node // create node

Loading…
Cancel
Save