Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Nov 29, 2022
1 parent f9de3ad commit 2931d07
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 44 deletions.
12 changes: 6 additions & 6 deletions cmd/goatak_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ type App struct {

ctx context.Context
uid string
ch chan *cot.Msg
ch chan *cot.CotMessage
}

func NewApp(config *AppConfig, logger *zap.SugaredLogger) *App {
app := &App{
Logger: logger,
config: config,
packageManager: NewPackageManager(logger),
ch: make(chan *cot.Msg, 20),
ch: make(chan *cot.CotMessage, 20),
handlers: sync.Map{},
units: sync.Map{},
uid: uuid.New().String(),
Expand Down Expand Up @@ -147,7 +147,7 @@ func (app *App) Run() {
cancel()
}

func (app *App) NewCotMessage(msg *cot.Msg) {
func (app *App) NewCotMessage(msg *cot.CotMessage) {
app.ch <- msg
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func (app *App) RemoveItem(uid string) {
}
}

func (app *App) ProcessItem(msg *cot.Msg) {
func (app *App) ProcessItem(msg *cot.CotMessage) {
cl := model.GetClass(msg)
if c := app.GetItem(msg.GetUid()); c != nil {
app.Logger.Infof("update %s %s (%s) %s", cl, msg.GetUid(), msg.GetCallsign(), msg.GetType())
Expand All @@ -297,7 +297,7 @@ func (app *App) ProcessItem(msg *cot.Msg) {
}
}

func (app *App) removeByLink(msg *cot.Msg) {
func (app *App) removeByLink(msg *cot.CotMessage) {
if msg.Detail != nil && msg.Detail.Has("link") {
uid := msg.Detail.GetFirst("link").GetAttr("uid")
typ := msg.Detail.GetFirst("link").GetAttr("type")
Expand Down Expand Up @@ -375,7 +375,7 @@ func (app *App) MessageProcessor() {
}
}

func (app *App) route(msg *cot.Msg) {
func (app *App) route(msg *cot.CotMessage) {
if len(msg.Detail.GetDest()) > 0 {
for _, s := range msg.Detail.GetDest() {
app.SendToCallsign(s, msg.TakMessage)
Expand Down
2 changes: 1 addition & 1 deletion cmd/goatak_server/udpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (app *App) ListenUDP(addr string) error {

msg, xd := cot.EventToProto(evt)

app.NewCotMessage(&cot.Msg{
app.NewCotMessage(&cot.CotMessage{
TakMessage: msg,
Detail: xd,
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (app *App) SendMsg(msg *cotproto.TakMessage) {
}
}

func (app *App) ProcessEvent(msg *cot.Msg) {
func (app *App) ProcessEvent(msg *cot.CotMessage) {
if c := app.GetItem(msg.GetUid()); c != nil {
c.Update(nil)
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func (app *App) ProcessEvent(msg *cot.Msg) {
}
}

func (app *App) ProcessItem(msg *cot.Msg) {
func (app *App) ProcessItem(msg *cot.CotMessage) {
cl := model.GetClass(msg)
if c := app.GetItem(msg.GetUid()); c != nil {
app.Logger.Infof("update %s %s (%s) %s", cl, msg.GetUid(), msg.GetCallsign(), msg.GetType())
Expand Down Expand Up @@ -252,7 +252,7 @@ func (app *App) GetItem(uid string) *model.Item {
return nil
}

func (app *App) removeByLink(msg *cot.Msg) {
func (app *App) removeByLink(msg *cot.CotMessage) {
if msg.Detail != nil && msg.Detail.Has("link") {
uid := msg.Detail.GetFirst("link").GetAttr("uid")
typ := msg.Detail.GetFirst("link").GetAttr("type")
Expand Down
8 changes: 4 additions & 4 deletions cot/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type HandlerConfig struct {
User string
Uid string
IsClient bool
MessageCb func(msg *Msg)
MessageCb func(msg *CotMessage)
RemoveCb func(ch *ClientHandler)
Logger *zap.SugaredLogger
}
Expand All @@ -43,7 +43,7 @@ type ClientHandler struct {
sendChan chan []byte
active int32
user string
messageCb func(msg *Msg)
messageCb func(msg *CotMessage)
removeCb func(ch *ClientHandler)
logger *zap.SugaredLogger
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (h *ClientHandler) handleRead() {
continue
}

cotmsg := &Msg{
cotmsg := &CotMessage{
From: h.addr,
TakMessage: msg,
Detail: d,
Expand Down Expand Up @@ -283,7 +283,7 @@ func (h *ClientHandler) GetVersion() int32 {
return atomic.LoadInt32(&h.ver)
}

func (h *ClientHandler) checkContact(msg *Msg) {
func (h *ClientHandler) checkContact(msg *CotMessage) {
if msg.IsContact() {
uid := msg.TakMessage.CotEvent.Uid
if strings.HasSuffix(uid, "-ping") {
Expand Down
26 changes: 13 additions & 13 deletions cot/msg.go → cot/cotmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,69 @@ import (
"github.com/kdudkov/goatak/cotproto"
)

type Msg struct {
type CotMessage struct {
From string
TakMessage *cotproto.TakMessage
Detail *Node
}

func (m *Msg) GetUid() string {
func (m *CotMessage) GetUid() string {
if m == nil || m.TakMessage == nil {
return ""
}

return m.TakMessage.GetCotEvent().Uid
}

func (m *Msg) GetType() string {
func (m *CotMessage) GetType() string {
if m == nil || m.TakMessage == nil {
return ""
}

return m.TakMessage.GetCotEvent().Type
}

func (m *Msg) GetCallsign() string {
func (m *CotMessage) GetCallsign() string {
if m == nil || m.TakMessage == nil {
return ""
}

return m.TakMessage.GetCotEvent().GetDetail().GetContact().GetCallsign()
}

func (m *Msg) GetEndpoint() string {
func (m *CotMessage) GetEndpoint() string {
if m == nil || m.TakMessage == nil {
return ""
}

return m.TakMessage.GetCotEvent().GetDetail().GetContact().GetEndpoint()
}

func (m *Msg) GetStale() time.Time {
func (m *CotMessage) GetStale() time.Time {
if m == nil || m.TakMessage == nil {
return time.Unix(0, 0)
}

return TimeFromMillis(m.TakMessage.CotEvent.StaleTime)
}

func (m *Msg) IsContact() bool {
func (m *CotMessage) IsContact() bool {
if m == nil || m.TakMessage == nil {
return false
}

return strings.HasPrefix(m.GetType(), "a-f-") && m.TakMessage.GetCotEvent().GetDetail().GetContact().GetEndpoint() != ""
}

func (m *Msg) IsChat() bool {
func (m *CotMessage) IsChat() bool {
if m == nil || m.TakMessage == nil {
return false
}

return m.GetType() == "b-t-f" && m.Detail != nil && m.Detail.Has("__chat")
}

func (m *Msg) PrintChat() string {
func (m *CotMessage) PrintChat() string {
chat := m.Detail.GetFirst("__chat")
if chat == nil {
return ""
Expand All @@ -83,31 +83,31 @@ func (m *Msg) PrintChat() string {
return fmt.Sprintf("%s -> %s: \"%s\"", from, to, text)
}

func (m *Msg) GetLatLon() (float64, float64) {
func (m *CotMessage) GetLatLon() (float64, float64) {
if m == nil {
return 0, 0
}

return m.TakMessage.GetCotEvent().GetLat(), m.TakMessage.GetCotEvent().GetLon()
}

func (m *Msg) GetLat() float64 {
func (m *CotMessage) GetLat() float64 {
if m == nil {
return 0
}

return m.TakMessage.GetCotEvent().GetLat()
}

func (m *Msg) GetLon() float64 {
func (m *CotMessage) GetLon() float64 {
if m == nil {
return 0
}

return m.TakMessage.GetCotEvent().GetLon()
}

func (m *Msg) GetParent() (string, string) {
func (m *CotMessage) GetParent() (string, string) {
for _, link := range m.Detail.GetAll("link") {
if link.GetAttr("relation") == "p-p" {
return link.GetAttr("uid"), link.GetAttr("parent_callsign")
Expand Down
4 changes: 4 additions & 0 deletions cot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func (n *Node) print(s *bytes.Buffer, prefix string) {
}

func (n *Node) AddChild(name string, params map[string]string, text string) *Node {
if n == nil {
return nil
}

nn := &Node{XMLName: xml.Name{Local: name}}

for k, v := range params {
Expand Down
File renamed without changes.
60 changes: 53 additions & 7 deletions cot/xmlevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@ func (e *Event) String() string {
return fmt.Sprintf("version=%s, type=%s, uid=%s, how=%s, stale=%s, detail={%s}", e.Version, e.Type, e.Uid, e.How, e.Stale.Sub(e.Start), e.Detail)
}

func (e *Event) AddDetail() *Node {
if e == nil {
return nil
}

if e.Detail == nil {
e.Detail = &Node{XMLName: xml.Name{Local: "detail"}}
}

return e.Detail
}

func (e *Event) AddGroup(group, role string) {
if e == nil {
return
}

e.AddDetail().AddChild("__group", map[string]string{"name": group, "role": role}, "")
}

func (e *Event) AddCallsign(callsign, endpoint string, addDroid bool) {
if e == nil {
return
}

e.AddDetail().AddChild("contact", map[string]string{"callsign": callsign, "endpoint": endpoint}, "")

if addDroid {
e.AddDetail().AddChild("uid", map[string]string{"Droid": callsign}, "")
}
}

func (e *Event) AddTrack(speed, course string) {
if e == nil {
return
}

e.AddDetail().AddChild("__track", map[string]string{"speed": speed, "course": course}, "")
}

func (e *Event) AddVersion(device, platform, os, version string) {
if e == nil {
return
}

e.AddDetail().AddChild("takv", map[string]string{"device": device, "platform": platform, "os": os, "version": version}, "")
}

type Point struct {
XMLName xml.Name `xml:"point"`
Lat float64 `xml:"lat,attr"`
Expand Down Expand Up @@ -63,29 +111,27 @@ func VersionSupportMsg(ver int8) *Event {
v := strconv.Itoa(int(ver))
ev := XmlBasicMsg("t-x-takp-v", "protouid", time.Minute)
ev.How = "m-g"
ev.Detail = NewXmlDetails()
ev.Detail.AddChild("TakControl", nil, "").AddChild("TakProtocolSupport", map[string]string{"version": v}, "")
ev.AddDetail().AddChild("TakControl", nil, "").AddChild("TakProtocolSupport", map[string]string{"version": v}, "")
return ev
}

func VersionReqMsg(ver int8) *Event {
v := strconv.Itoa(int(ver))
ev := XmlBasicMsg("t-x-takp-v", "protouid", time.Minute)
ev.How = "m-g"
ev.Detail = NewXmlDetails()
ev.Detail.AddChild("TakControl", nil, "").AddChild("TakRequest", map[string]string{"version": v}, "")
ev.AddDetail().AddChild("TakControl", nil, "").AddChild("TakRequest", map[string]string{"version": v}, "")
return ev
}

func ProtoChangeOkMsg() *Event {
ev := XmlBasicMsg("t-x-takp-r", "protouid", time.Minute)
ev.How = "m-g"
ev.Detail = NewXmlDetails()
ev.Detail.AddChild("TakControl", nil, "").AddChild("TakResponse", map[string]string{"status": "true"}, "")
ev.AddDetail().AddChild("TakControl", nil, "").AddChild("TakResponse", map[string]string{"status": "true"}, "")
return ev
}

// Geopointsrc = "USER" ce Altsrc - "DTED0"
// Geopointsrc = "USER" Altsrc - "DTED0"
// ce
// high 0 - cat1, 7 - CAT2 16 - CAT3 31 - CAT4 92 - CAT5
// medium 3 - cat1, 11 - CAT2 23 - CAT3 61 - CAT4 198.5 - CAT5
// low 6 - cat1, 15 - CAT2 30 - CAT3 91 - CAT4 305 - CAT5
Expand Down
2 changes: 1 addition & 1 deletion model/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ChatMessage struct {
// <__serverdestination destinations="192.168.1.72:4242:tcp:ANDROID-765a942cbe30d010"/>
// <remarks source="BAO.F.ATAK.ANDROID-765a942cbe30d010" time="2022-04-05T08:26:51.718Z">[UPDATED CONTACTS]</remarks>

func MsgToChat(m *cot.Msg) *ChatMessage {
func MsgToChat(m *cot.CotMessage) *ChatMessage {
chat := m.Detail.GetFirst("__chat")
if chat == nil {
return nil
Expand Down
4 changes: 2 additions & 2 deletions model/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (i *Item) ToWeb() *WebUnit {
return w
}

func (w *WebUnit) ToMsg() *cot.Msg {
func (w *WebUnit) ToMsg() *cot.CotMessage {
msg := &cotproto.TakMessage{
CotEvent: &cotproto.CotEvent{
Type: w.Type,
Expand Down Expand Up @@ -148,7 +148,7 @@ func (w *WebUnit) ToMsg() *cot.Msg {
msg.CotEvent.StaleTime = cot.TimeToMillis(time.Now().Add(time.Hour * 24))
}

return &cot.Msg{
return &cot.CotMessage{
From: "",
TakMessage: msg,
Detail: xd,
Expand Down
Loading

0 comments on commit 2931d07

Please sign in to comment.