mirror of
https://github.com/postmannen/ctrl.git
synced 2025-01-18 21:59:30 +00:00
chanced it into it's own package
This commit is contained in:
parent
d2b3994f26
commit
223f472f5b
3 changed files with 44 additions and 38 deletions
33
cmd/main.go
Normal file
33
cmd/main.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/RaaLabs/steward"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nodeName := flag.String("node", "0", "some unique string to identify this Edge unit")
|
||||||
|
brokerAddress := flag.String("brokerAddress", "0", "the address of the message broker")
|
||||||
|
modePublisher := flag.Bool("modePublisher", false, "set to true if it should be able to publish")
|
||||||
|
modeSubscriber := flag.Bool("modeSubscriber", false, "set to true if it should be able to subscribe")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
s, err := steward.NewServer(*brokerAddress, *nodeName)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: failed to connect to broker: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if *modePublisher {
|
||||||
|
go s.RunPublisher()
|
||||||
|
}
|
||||||
|
|
||||||
|
if *modeSubscriber {
|
||||||
|
go s.RunSubscriber()
|
||||||
|
}
|
||||||
|
|
||||||
|
select {}
|
||||||
|
}
|
|
@ -1,13 +1,11 @@
|
||||||
// Notes:
|
// Notes:
|
||||||
package main
|
package steward
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -62,8 +60,15 @@ type server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// newServer will prepare and return a server type
|
// newServer will prepare and return a server type
|
||||||
func newServer(brokerAddress string) (*server, error) {
|
func NewServer(brokerAddress string, nodeName string) (*server, error) {
|
||||||
|
conn, err := nats.Connect(brokerAddress, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: nats.Connect failed: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
return &server{
|
return &server{
|
||||||
|
thisNodeName: nodeName,
|
||||||
|
natsConn: conn,
|
||||||
processes: make(map[node]process),
|
processes: make(map[node]process),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -220,35 +225,3 @@ func gobEncodePayload(m Message) ([]byte, error) {
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
node := flag.String("node", "0", "some unique string to identify this Edge unit")
|
|
||||||
modePublisher := flag.Bool("modePublisher", false, "set to true if it should be able to publish")
|
|
||||||
modeSubscriber := flag.Bool("modeSubscriber", false, "set to true if it should be able to subscribe")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
s, err := newServer("localhost")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error: failed to connect to broker: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.thisNodeName = *node
|
|
||||||
|
|
||||||
// Create a connection to nats server
|
|
||||||
s.natsConn, err = nats.Connect("localhost", nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error: nats.Connect failed: %v\n", err)
|
|
||||||
}
|
|
||||||
defer s.natsConn.Close()
|
|
||||||
|
|
||||||
if *modePublisher {
|
|
||||||
go s.RunPublisher()
|
|
||||||
}
|
|
||||||
|
|
||||||
if *modeSubscriber {
|
|
||||||
go s.RunSubscriber()
|
|
||||||
}
|
|
||||||
|
|
||||||
select {}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package steward
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
Loading…
Add table
Reference in a new issue