# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import socket
from time import strptime
import re

from dmunit import DeviceManagerTestCase


class DataChannelTestCase(DeviceManagerTestCase):

    runs_on_test_device = False

    def runTest(self):
        """ This tests the heartbeat and the data channel
        """
        ip = self.dm.host
        port = 20700

        # Let's connect
        self._datasock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # Assume 60 seconds between heartbeats
        self._datasock.settimeout(float(60 * 2))
        self._datasock.connect((ip, port))
        self._connected = True

        # Let's listen
        numbeats = 0
        capturedHeader = False
        while(numbeats < 3):
            data = self._datasock.recv(1024)
            print data
            self.assertNotEqual(len(data), 0)

            # Check for the header
            if not capturedHeader:
                m = re.match(r"(.*?) trace output", data)
                self.assertNotEqual(m, None,
                    'trace output line does not match. The line: ' + str(data))
                lastHeartbeatTime = strptime(m.group(1), "%Y%m%d-%H:%M:%S")
                capturedHeader = True

            # Check for standard heartbeat messsage
            m = re.match(r"(.*?) Thump thump - (.*)", data)
            if m == None:
                # This isn't an error, it usually means we've obtained some
                # unexpected data from the device
                continue

            # Ensure it matches our format
            mHeartbeatTime = m.group(1)
            mHeartbeatTime = strptime(mHeartbeatTime, "%Y%m%d-%H:%M:%S")
            mDeviceID = m.group(2)
            numbeats = numbeats + 1
