Skip to content

Commit

Permalink
Fix exiting too fast for servers to start
Browse files Browse the repository at this point in the history
  • Loading branch information
toxuin committed Jun 7, 2021
1 parent a8b60a3 commit 823e2ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/toxuin/alarmserver/servers/ftp"
"github.com/toxuin/alarmserver/servers/hikvision"
"github.com/toxuin/alarmserver/servers/hisilicon"
"sync"
)

var config *conf.Config
Expand All @@ -23,6 +24,8 @@ func main() {
config.Printout()
}

processesWaitGroup := sync.WaitGroup{}

// INIT BUSES
mqttBus := mqtt.Bus{Debug: config.Debug}
if config.Mqtt.Enabled {
Expand Down Expand Up @@ -53,6 +56,7 @@ func main() {
// START HISILICON ALARM SERVER
hisiliconServer := hisilicon.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Port: config.Hisilicon.Port,
MessageHandler: messageHandler,
}
Expand All @@ -66,6 +70,7 @@ func main() {
// START HIKVISION ALARM SERVER
hikvisionServer := hikvision.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Cameras: &config.Hikvision.Cams,
MessageHandler: messageHandler,
}
Expand All @@ -79,6 +84,7 @@ func main() {
// START FTP SERVER
ftpServer := ftp.Server{
Debug: config.Debug,
WaitGroup: &processesWaitGroup,
Port: config.Ftp.Port,
AllowFiles: config.Ftp.AllowFiles,
RootPath: config.Ftp.RootPath,
Expand All @@ -90,4 +96,6 @@ func main() {
fmt.Println("STARTED FTP SERVER")
}
}

processesWaitGroup.Wait()
}
5 changes: 5 additions & 0 deletions servers/ftp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ftp
import (
"fmt"
"goftp.io/server/v2"
"sync"
)

type Server struct {
Debug bool
WaitGroup *sync.WaitGroup
Port int
AllowFiles bool
RootPath string
Expand All @@ -32,6 +34,9 @@ func (serv *Server) Start() {
serv.Password = "root"
}

defer serv.WaitGroup.Done()
serv.WaitGroup.Add(1)

eventChannel := make(chan Event, 5)

// START MESSAGE PROCESSOR
Expand Down
20 changes: 13 additions & 7 deletions servers/hikvision/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type HikEvent struct {

type Server struct {
Debug bool
WaitGroup *sync.WaitGroup
Cameras *[]HikCamera
MessageHandler func(topic string, data string)
}
Expand Down Expand Up @@ -87,23 +88,28 @@ func (server *Server) Start() {
}
}

waitGroup := sync.WaitGroup{}
cameraWaitGroup := sync.WaitGroup{}
eventChannel := make(chan HikEvent, 5)

// START ALL CAMERA LISTENERS
for _, camera := range *server.Cameras {
server.addCamera(&waitGroup, &camera, eventChannel)
server.addCamera(&cameraWaitGroup, &camera, eventChannel)
}

// START MESSAGE PROCESSOR
go func(waitGroup *sync.WaitGroup, channel <-chan HikEvent) {
defer waitGroup.Done()
go func(camWaitGroup *sync.WaitGroup, channel <-chan HikEvent) {
// WAIT GROUP FOR INDIVIDUAL CAMERAS
defer camWaitGroup.Done()

// EXTERNAL WAIT GROUP FOR PROCESSES
defer server.WaitGroup.Done()
server.WaitGroup.Add(1)
for {
event := <-channel
go server.MessageHandler(event.Camera.Name+"/"+event.Type, event.Message)
}
}(&waitGroup, eventChannel)
waitGroup.Add(1)
}(&cameraWaitGroup, eventChannel)
cameraWaitGroup.Add(1)

waitGroup.Wait()
cameraWaitGroup.Wait()
}
17 changes: 11 additions & 6 deletions servers/hisilicon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"strconv"
"strings"
"sync"
)

// Converts 0x1704A8C0 to 192.168.4.23
Expand Down Expand Up @@ -36,6 +37,13 @@ func hexIpToCIDR(hexAddr string) string {
return ipAddr
}

type Server struct {
Debug bool
WaitGroup *sync.WaitGroup
Port string
MessageHandler func(topic string, data string)
}

func (server *Server) handleTcpConnection(conn net.Conn) {
defer conn.Close()

Expand Down Expand Up @@ -84,12 +92,6 @@ func (server *Server) handleTcpConnection(conn net.Conn) {
server.MessageHandler(serialId+"/"+event, string(jsonBytes))
}

type Server struct {
Debug bool
Port string
MessageHandler func(topic string, data string)
}

func (server *Server) Start() {
if server.Port == "" {
server.Port = "15002" // DEFAULT PORT
Expand All @@ -102,6 +104,9 @@ func (server *Server) Start() {
}

go func() {
defer server.WaitGroup.Done()
server.WaitGroup.Add(1)

// START TCP SERVER
tcpListener, err := net.Listen("tcp4", ":"+server.Port)
if err != nil {
Expand Down

0 comments on commit 823e2ce

Please sign in to comment.