|
|
@@ -0,0 +1,112 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "net"
|
|
|
+ "os"
|
|
|
+)
|
|
|
+
|
|
|
+func (pool *Pool) tcplistener() {
|
|
|
+ // Listen for incoming connections.
|
|
|
+ l, err := net.Listen("tcp", "0.0.0.0:12345")
|
|
|
+ if err != nil {
|
|
|
+ pool.logger.Println("Error listening:", err.Error())
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Close the listener when the application closes.
|
|
|
+ defer l.Close()
|
|
|
+
|
|
|
+ for {
|
|
|
+ // Listen for an incoming connection.
|
|
|
+ conn, err := l.Accept()
|
|
|
+ if err != nil {
|
|
|
+ pool.logger.Println("Error accepting: ", err.Error())
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
+ // Handle connections in a new goroutine.
|
|
|
+ pool.logger.Println("DCS Mission Started")
|
|
|
+ go pool.handleRequest(conn)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Handles incoming requests.
|
|
|
+func (pool *Pool) handleRequest(conn net.Conn) {
|
|
|
+
|
|
|
+ // Close the connection when you're done with it.
|
|
|
+ defer conn.Close()
|
|
|
+
|
|
|
+ for {
|
|
|
+
|
|
|
+ // Make an MB of buffer to hold incoming data.
|
|
|
+ buf := make([]byte, 1024*1024)
|
|
|
+
|
|
|
+ // Read the incoming connection into the buffer.
|
|
|
+ read_len, err := conn.Read(buf)
|
|
|
+ if err != nil {
|
|
|
+ if err.Error() == "EOF" {
|
|
|
+ pool.logger.Println("Socket closed -- DCS Mission over?")
|
|
|
+ // Reset buffers
|
|
|
+ pool.latest = ""
|
|
|
+ pool.oldest = ""
|
|
|
+ pool.jsonst = ""
|
|
|
+ } else {
|
|
|
+ pool.logger.Println("Error reading:", err.Error())
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Copy messages around
|
|
|
+ pool.oldest = pool.latest
|
|
|
+ pool.latest = string(buf)[:read_len]
|
|
|
+
|
|
|
+ // Make sure we only do things if there is actual data:
|
|
|
+ if pool.oldest != "" {
|
|
|
+
|
|
|
+ // Parse JSON
|
|
|
+ var err error
|
|
|
+ var json_oldest json_units
|
|
|
+ var json_latest json_units
|
|
|
+
|
|
|
+ err = json.Unmarshal([]byte(pool.oldest), &json_oldest)
|
|
|
+ if err != nil {
|
|
|
+ pool.logger.Println("json_oldest error:", err)
|
|
|
+ pool.logger.Println("pool.oldest", pool.oldest)
|
|
|
+ pool.logger.Println("json_oldest", json_oldest)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = json.Unmarshal([]byte(pool.latest), &json_latest)
|
|
|
+ if err != nil {
|
|
|
+ pool.logger.Println("json.latest error:", err)
|
|
|
+ pool.logger.Println("pool.latest", pool.latest)
|
|
|
+ pool.logger.Println("json_latest", json_latest)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Compare JSON
|
|
|
+ var units []Unit
|
|
|
+
|
|
|
+ for _, vvoor := range json_latest.Units {
|
|
|
+ for _, vna := range json_oldest.Units {
|
|
|
+ if vna.UnitName == vvoor.UnitName {
|
|
|
+ SpeedInMps := calcMps(calcDistance(vvoor.Latitude, vvoor.Longitude, vna.Latitude, vna.Longitude), vvoor.AgeInSeconds-vna.AgeInSeconds)
|
|
|
+ SpeedInKnots := calcKnots(SpeedInMps)
|
|
|
+ SpeedInKph := calcKph(SpeedInMps)
|
|
|
+ SpeedInMph := calcMph(SpeedInMps)
|
|
|
+ units = append(units, Unit{vna.AgeInSeconds, vna.UnitName, vna.GroupName, vna.Name, vna.Latitude, vna.Longitude, vna.AltitudeInFeet, SpeedInKnots, SpeedInKph, SpeedInMps, SpeedInMph, vna.HeadingInRads, rad2deg(vna.HeadingInRads), vna.PitchInRads, rad2deg(vna.PitchInRads), vna.BankInRads, rad2deg(vna.BankInRads), vna.Coalition, vna.Type})
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if pool.jsonst == "" {
|
|
|
+ pool.logger.Println("Making JSON available on HTTP.")
|
|
|
+ }
|
|
|
+ json_final, _ := json.Marshal(units)
|
|
|
+ pool.jsonst = string(json_final)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Send a response back to person contacting us.
|
|
|
+ //conn.Write([]byte("Message received\n"))
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|