Files
openfsd/protocol/client_query.go
Reese Norris 57d54d6705 v0.1.0-alpha
Changes:
- Implement bootstrapping library for managing several concurrent internal services
- Refactor concurrency model for connections/logical clients and their associated I/O
- Refactor server context singleton
- Refactor error handling
    - Most errors are now gracefully sent to the FSD client directly encoded as an $ER packet,
      enhancing visibility and debugging
    - Most errors are now rightfully treated as non-fatal
- Refactor package/dependency graph
- Refactor calling conventions/interfaces for many packages
- Refactor database package
- Refactor post office

Features:
- Add VATSIM-esque HTTP/JSON "data feed"
- Add ephemeral in-memory database option
- Add user management REST API
- Add improved web interface
- Add MySQL support (drop SQLite support)
2024-10-07 12:50:39 -07:00

72 lines
1.5 KiB
Go

package protocol
import (
"fmt"
"strings"
)
type ClientQueryPDU struct {
From string `validate:"required,alphanum,max=16"`
To string `validate:"required,max=7"`
QueryType string `validate:"required,ascii,min=2,max=16"`
Payload string `validate:""`
}
func (p *ClientQueryPDU) Serialize() string {
if p.Payload == "" {
return fmt.Sprintf("$CQ%s:%s:%s%s",
p.From, p.To, p.QueryType, PacketDelimiter)
} else {
return fmt.Sprintf("$CQ%s:%s:%s:%s%s",
p.From, p.To, p.QueryType, p.Payload, PacketDelimiter)
}
}
func (p *ClientQueryPDU) Parse(packet string) error {
packet = strings.TrimSuffix(packet, PacketDelimiter)
packet = strings.TrimPrefix(packet, "$CQ")
// Extract fields
var fields []string
if fields = strings.SplitN(packet, Delimiter, 4); len(fields) < 3 {
return NewGenericFSDError(SyntaxError, "", "invalid parameter count")
}
pdu := ClientQueryPDU{
From: fields[0],
To: fields[1],
QueryType: fields[2],
}
if len(fields) == 4 {
pdu.Payload = fields[3]
}
if err := V.Struct(&pdu); err != nil {
if validatorErr := getFSDErrorFromValidatorErrors(err); err != nil {
return validatorErr
}
return err
}
switch pdu.QueryType {
case "ATC", "CAPS", "C?",
"RN", "SV", "ATIS",
"IP", "INF", "FP",
"IPC", "BY", "HI",
"HLP", "NOHLP", "WH",
"IT", "HT", "DR",
"FA", "TA", "BC",
"SC", "VT", "ACC",
"NEWINFO", "NEWATIS", "EST",
"GD":
default:
return NewGenericFSDError(SyntaxError, fields[2], "invalid query type")
}
// Copy new pdu into receiver
*p = pdu
return nil
}