# Path: mlat/geodesy.py
# WGS84_A = 6378137.0
# WGS84_F = 1.0/298.257223563
# WGS84_B = WGS84_A * (1 - WGS84_F)
# WGS84_ECC_SQ = 1 - WGS84_B * WGS84_B / (WGS84_A * WGS84_A)
# WGS84_ECC = math.sqrt(WGS84_ECC_SQ)
# SPHERICAL_R = 6371e3
#     N = WGS84_A / math.sqrt(1 - WGS84_ECC_SQ * math.sin(lat)**2)
# def llh2ecef(llh):
# def ecef2llh(ecef):
# def greatcircle(p0, p1):
# def ecef_distance(p0, p1):
# 
# Path: mlat/server/net.py
# class MonitoringListener(object):
# class MonitoringConnector(object):
#     def __init__(self, host, port, factory, logger=glogger, description=None):
#     def start(self):
#     def _start(self):
#     def _new_client(self, r, w):
#     def _close(self):
#     def start_client(self, r, w):
#     def monitor_client(self, client):
#     def close(self):
#     def wait_closed(self):
#     def __init__(self, host, port, reconnect_interval, factory):
#     def start(self):
#     def reconnect(self):
#     def close(self):
#     def wait_closed(self):
# 
# Path: mlat/server/util.py
# def fuzzy(t):
# def safe_wait(coros_or_futures, **kwargs):
#     def process(self, msg, kwargs):
# def setproctitle(title):
# class TaggingLogger(logging.LoggerAdapter):
# 
# Path: mlat/server/connection.py
# class Connection(object):
#     def request_traffic(self, receiver, icao_set):
#     def report_mlat_position(self, receiver,
#                              receive_timestamp, address, ecef, ecef_cov, receivers, distinct):
# 
# Path: mlat/server/config.py
# MIN_NUC = 6
# MAX_RANGE = 500e3
# MAX_INTERMESSAGE_RANGE = 10e3
# MAX_ALT = 50000 * constants.FTOM
# MLAT_DELAY = 2.5
# SOLVER_MAXFEV = 50


import asyncio
import zlib
import logging
import json
import struct
import time
import random
import socket
import inspect
import sys
import math
from mlat import constants, geodesy
from mlat.server import net, util, connection, config
            # if we have seen no activity recently, declare the
            # connection dead and close it down
            if (time.monotonic() - self._last_message_time) > self.read_heartbeat_interval:
                self.logger.warn("No recent messages seen, closing connection")
                self.close()
                return

            # write a heartbeat message
            self.send(heartbeat={'server_time': round(time.time(), 3)})

    @asyncio.coroutine
    def handle_connection(self):
        """A coroutine that handle reading from the client and processing messages.

        This does the initial handshake, then reads and processes messages
        after the handshake iscomplete.

        It also does any client cleanup needed when the connection is closed.

        This coroutine's task is stashed as self.read_task; cancelling this
        task will cause the client connection to be closed and cleaned up."""

        self.logger.info("Accepted new client connection")

        try:
            hs = yield from asyncio.wait_for(self.r.readline(), timeout=30.0)
            if not self.process_handshake(hs):
                return

            # start heartbeat handling now that the handshake is done

# Path: mlat/geodesy.py
# WGS84_A = 6378137.0
# WGS84_F = 1.0/298.257223563
# WGS84_B = WGS84_A * (1 - WGS84_F)
# WGS84_ECC_SQ = 1 - WGS84_B * WGS84_B / (WGS84_A * WGS84_A)
# WGS84_ECC = math.sqrt(WGS84_ECC_SQ)
# SPHERICAL_R = 6371e3
#     N = WGS84_A / math.sqrt(1 - WGS84_ECC_SQ * math.sin(lat)**2)
# def llh2ecef(llh):
# def ecef2llh(ecef):
# def greatcircle(p0, p1):
# def ecef_distance(p0, p1):
# 
# Path: mlat/server/net.py
# class MonitoringListener(object):
# class MonitoringConnector(object):
#     def __init__(self, host, port, factory, logger=glogger, description=None):
#     def start(self):
#     def _start(self):
#     def _new_client(self, r, w):
#     def _close(self):
#     def start_client(self, r, w):
#     def monitor_client(self, client):
#     def close(self):
#     def wait_closed(self):
#     def __init__(self, host, port, reconnect_interval, factory):
#     def start(self):
#     def reconnect(self):
#     def close(self):
#     def wait_closed(self):
# 
# Path: mlat/server/util.py
# def fuzzy(t):
# def safe_wait(coros_or_futures, **kwargs):
#     def process(self, msg, kwargs):
# def setproctitle(title):
# class TaggingLogger(logging.LoggerAdapter):
# 
# Path: mlat/server/connection.py
# class Connection(object):
#     def request_traffic(self, receiver, icao_set):
#     def report_mlat_position(self, receiver,
#                              receive_timestamp, address, ecef, ecef_cov, receivers, distinct):
# 
# Path: mlat/server/config.py
# MIN_NUC = 6
# MAX_RANGE = 500e3
# MAX_INTERMESSAGE_RANGE = 10e3
# MAX_ALT = 50000 * constants.FTOM
# MLAT_DELAY = 2.5
# SOLVER_MAXFEV = 50


# Path: mlat/server/jsonclient.py
import asyncio
import zlib
import logging
import json
import struct
import time
import random
import socket
import inspect
import sys
import math
from mlat import constants, geodesy
from mlat.server import net, util, connection, config

            # if we have seen no activity recently, declare the
            # connection dead and close it down
            if (time.monotonic() - self._last_message_time) > self.read_heartbeat_interval:
                self.logger.warn("No recent messages seen, closing connection")
                self.close()
                return

            # write a heartbeat message
            self.send(heartbeat={'server_time': round(time.time(), 3)})

    @asyncio.coroutine
    def handle_connection(self):
        """A coroutine that handle reading from the client and processing messages.

        This does the initial handshake, then reads and processes messages
        after the handshake iscomplete.

        It also does any client cleanup needed when the connection is closed.

        This coroutine's task is stashed as self.read_task; cancelling this
        task will cause the client connection to be closed and cleaned up."""

        self.logger.info("Accepted new client connection")

        try:
            hs = yield from asyncio.wait_for(self.r.readline(), timeout=30.0)
            if not self.process_handshake(hs):
                return

            # start heartbeat handling now that the handshake is done

            self._last_message_time = time.monotonic()