2021-01-25 15:23:00 +01:00
|
|
|
// Same as 02 example, but using PublishMsg method instead of Request method
|
|
|
|
// for publishing.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-01-27 14:02:57 +01:00
|
|
|
"os"
|
2021-01-25 15:23:00 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
2021-01-25 15:50:44 +01:00
|
|
|
type messageType int
|
|
|
|
|
|
|
|
const (
|
2021-01-27 09:45:52 +01:00
|
|
|
// shellCommand, command that will just wait for an
|
|
|
|
// ack, and nothing of the output of the command are
|
|
|
|
// delivered back in the reply ack message.
|
|
|
|
// The message should contain the unique ID of the
|
|
|
|
// command.
|
2021-01-27 14:02:57 +01:00
|
|
|
commandReturnOutput messageType = iota
|
2021-01-25 15:50:44 +01:00
|
|
|
// shellCommand, wait for and return the output
|
|
|
|
// of the command in the ACK message. This means
|
|
|
|
// that the command should be executed immediately
|
|
|
|
// and that we should get the confirmation that it
|
|
|
|
// was successful or not.
|
2021-01-27 14:02:57 +01:00
|
|
|
eventReturnAck messageType = iota
|
2021-01-25 15:50:44 +01:00
|
|
|
// eventCommand, just wait for the ACK that the
|
|
|
|
// message is received. What action happens on the
|
|
|
|
// receiving side is up to the received to decide.
|
|
|
|
)
|
|
|
|
|
2021-01-25 15:23:00 +01:00
|
|
|
type Message struct {
|
2021-01-25 15:50:44 +01:00
|
|
|
// The Unique ID of the message
|
|
|
|
ID int
|
|
|
|
// The actual data in the message
|
2021-01-25 16:50:21 +01:00
|
|
|
// TODO: Change this to a slice instead...or maybe use an
|
|
|
|
// interface type here to handle several data types ?
|
2021-01-27 09:45:52 +01:00
|
|
|
Data []string
|
2021-01-25 15:50:44 +01:00
|
|
|
// The type of the message being sent
|
|
|
|
MessageType messageType
|
2021-01-25 15:23:00 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
// session are represent the communication to one individual host
|
|
|
|
type session struct {
|
|
|
|
messageID int
|
|
|
|
subject string
|
|
|
|
hostID hostID
|
|
|
|
}
|
2021-01-27 09:45:52 +01:00
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
type hostID string
|
2021-01-27 09:45:52 +01:00
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
type server struct {
|
|
|
|
natsConn *nats.Conn
|
|
|
|
sessions map[hostID]session
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServer(brokerAddress string) (*server, error) {
|
|
|
|
return &server{
|
|
|
|
sessions: make(map[hostID]session),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-01-27 09:45:52 +01:00
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
func (s *server) Run() error {
|
|
|
|
// create the initial configuration for a sessions communicating with 1 host.
|
|
|
|
session := session{
|
|
|
|
messageID: 0,
|
|
|
|
hostID: "btship1",
|
|
|
|
}
|
|
|
|
s.sessions["btship1"] = session
|
|
|
|
|
|
|
|
// Loop creating one new message every second to simulate getting new
|
|
|
|
// messages to deliver.
|
|
|
|
for {
|
|
|
|
m := getMessageToDeliver()
|
|
|
|
m.ID = s.sessions["btship1"].messageID
|
|
|
|
messageDeliver("btship1", m, s.natsConn)
|
2021-01-27 09:45:52 +01:00
|
|
|
|
|
|
|
// Increment the counter for the next message to be sent.
|
2021-01-27 14:02:57 +01:00
|
|
|
session.messageID++
|
|
|
|
s.sessions["btship1"] = session
|
2021-01-27 09:45:52 +01:00
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
// get MessageToDeliver will pick up the next message to be created.
|
|
|
|
// TODO: read this from local file or rest or....?
|
|
|
|
func getMessageToDeliver() Message {
|
|
|
|
return Message{
|
|
|
|
Data: []string{"uname", "-a"},
|
|
|
|
MessageType: eventReturnAck,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:45:52 +01:00
|
|
|
func messageDeliver(edgeID string, message Message, natsConn *nats.Conn) {
|
|
|
|
for {
|
2021-01-27 14:02:57 +01:00
|
|
|
dataPayload, err := gobEncodePayload(message)
|
2021-01-27 09:45:52 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: createDataPayload: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &nats.Msg{
|
|
|
|
Subject: edgeID,
|
|
|
|
Reply: "subjectReply",
|
|
|
|
Data: dataPayload,
|
2021-01-25 15:23:00 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:45:52 +01:00
|
|
|
// The SubscribeSync used in the subscriber, will get messages that
|
|
|
|
// are sent after it started subscribing, so we start a publisher
|
|
|
|
// that sends out a message every second.
|
|
|
|
//
|
|
|
|
// Create a subscriber for the reply message.
|
|
|
|
subReply, err := natsConn.SubscribeSync(msg.Reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nc.SubscribeSync failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish message
|
|
|
|
err = natsConn.PublishMsg(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: publish failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait up until 10 seconds for a reply,
|
|
|
|
// continue and resend if to reply received.
|
|
|
|
msgReply, err := subReply.NextMsg(time.Second * 10)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: subRepl.NextMsg failed: %v\n", err)
|
|
|
|
// did not receive a reply, continuing from top again
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("publisher: received: %s\n", msgReply.Data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:02:57 +01:00
|
|
|
// gobEncodePayload will encode the message structure along with its
|
|
|
|
// valued in gob binary format.
|
|
|
|
func gobEncodePayload(m Message) ([]byte, error) {
|
2021-01-27 09:45:52 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
gobEnc := gob.NewEncoder(&buf)
|
|
|
|
err := gobEnc.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error: gob.Enode failed: %v", err)
|
|
|
|
}
|
2021-01-25 15:23:00 +01:00
|
|
|
|
2021-01-27 09:45:52 +01:00
|
|
|
return buf.Bytes(), nil
|
2021-01-25 15:23:00 +01:00
|
|
|
}
|
2021-01-27 14:02:57 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
s, err := newServer("localhost")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: failed to connect to broker: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
// Create a connection to nats server, and publish a message.
|
|
|
|
s.natsConn, err = nats.Connect("localhost", nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nats.Connect failed: %v\n", err)
|
|
|
|
}
|
|
|
|
defer s.natsConn.Close()
|
|
|
|
|
|
|
|
s.Run()
|
|
|
|
}
|