1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

first concept of receiving messages from end nodes

This commit is contained in:
postmannen 2021-02-10 05:11:48 +01:00
parent 042aee0c29
commit 271447d42d
8 changed files with 65 additions and 14 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
textlogging.go
ship1/
ship2/

View file

@ -14,8 +14,8 @@ import (
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")
// 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")
profilingPort := flag.String("profilingPort", "", "The number of the profiling port")
flag.Parse()
@ -34,13 +34,12 @@ func main() {
os.Exit(1)
}
if *modePublisher {
go s.PublisherStart()
}
// Start the messaging server
go s.Start()
if *modeSubscriber {
go s.RunSubscriber()
}
//if *modeSubscriber {
// go s.RunSubscriber()
//}
select {}
}

View file

@ -0,0 +1,15 @@
[
{
"subject":
{
"node":"central",
"messageKind":"event",
"method":"textlogging"
},
"message":
{
"data": ["some message sent from a ship"],
"messageType":"Event"
}
}
]

View file

@ -8,7 +8,7 @@
},
"message":
{
"data": ["bash","-c","ls -l"],
"data": ["bash","-c","ls -l ../"],
"messageType":"Command"
}
}

View file

@ -8,7 +8,7 @@
},
"message":
{
"data": ["bash","-c","tree"],
"data": ["bash","-c","tree ../"],
"messageType":"Command"
}
}

View file

@ -72,6 +72,8 @@ type server struct {
errorCh chan errProcess
// errorKernel
errorKernel *errorKernel
// TODO: replace this with some structure to hold the logCh value
logCh chan []byte
}
// newServer will prepare and return a server type
@ -87,6 +89,7 @@ func NewServer(brokerAddress string, nodeName string) (*server, error) {
processes: make(map[subjectName]process),
newMessagesCh: make(chan []jsonFromFile),
errorCh: make(chan errProcess, 2),
logCh: make(chan []byte),
}
// Start the error kernel that will do all the error handling
@ -98,10 +101,36 @@ func NewServer(brokerAddress string, nodeName string) (*server, error) {
}
func (s *server) PublisherStart() {
func (s *server) Start() {
// Start the checking the input file for new messages from operator.
go s.getMessagesFromFile("./", "inmsg.txt", s.newMessagesCh)
// Start the textlogging service that will run on the subscribers
// TODO: Figure out how to structure event services like these
go s.startTextLogging(s.logCh)
// Start a subscriber for shellCommand messages
{
fmt.Printf("nodeName: %#v\n", s.nodeName)
sub := newSubject(s.nodeName, "command", "shellcommand")
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
// fmt.Printf("*** %#v\n", proc)
go s.processSpawnWorker(proc)
}
// Start a subscriber for textLogging messages
{
fmt.Printf("nodeName: %#v\n", s.nodeName)
sub := newSubject(s.nodeName, "event", "textlogging")
proc := s.processPrepareNew(sub, s.errorCh, processKindSubscriber)
// fmt.Printf("*** %#v\n", proc)
go s.processSpawnWorker(proc)
}
time.Sleep(time.Second * 2)
fmt.Printf("*** Output of processes map: %#v\n", s.processes)
// Prepare and start a single process
//{
// sub := newSubject("ship1", "command", "shellcommand")
@ -306,7 +335,7 @@ func (s *server) processSpawnWorker(proc process) {
// We start one handler per message received by using go routines here.
// This is for being able to reply back the current publisher who sent
// the message.
go handler(s.natsConn, s.nodeName, msg)
go s.handler(s.natsConn, s.nodeName, msg)
})
if err != nil {
log.Printf("error: Subscribe failed: %v\n", err)

View file

@ -48,7 +48,7 @@ func (s *server) RunSubscriber() {
// the state of the message being processed, and then reply back to the
// correct sending process's reply, meaning so we ACK back to the correct
// publisher.
func handler(natsConn *nats.Conn, node string, msg *nats.Msg) {
func (s *server) handler(natsConn *nats.Conn, node string, msg *nats.Msg) {
message := Message{}
@ -84,7 +84,11 @@ func handler(natsConn *nats.Conn, node string, msg *nats.Msg) {
// Send a confirmation message back to the publisher
natsConn.Publish(msg.Reply, []byte("confirmed from: "+node+": "+fmt.Sprintf("%v\n%s", message.ID, out)))
case message.MessageType == "Event":
fmt.Printf("info: the event type is not implemented yet\n")
fmt.Printf("info: sending over the message %#v\n", message)
for _, d := range message.Data {
s.logCh <- []byte(d)
}
// Send a confirmation message back to the publisher
natsConn.Publish(msg.Reply, []byte("confirmed from: "+node+": "+fmt.Sprint(message.ID)))

1
textlogging.log Normal file
View file

@ -0,0 +1 @@
some message sent from a shipsome message sent from a ship