KissTcp._write_interface()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
"""KISS Core Classes."""
5
6
# These imports are for python3 compatibility inside python2
7
from __future__ import absolute_import
8
from __future__ import division
9
from __future__ import print_function
10
11
import logging
12
import socket
13
14
from apex.kiss import constants as kiss_constants
15
from .kiss import Kiss
16
17
__author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
18
__maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)'
19
__email__ = '[email protected]'
20
__license__ = 'Apache License, Version 2.0'
21
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors'
22
__credits__ = []
23
__version__ = '0.0.2'
24
25
26
class KissTcp(Kiss):
27
28
    """KISS TCP Object Class."""
29
30
    logger = logging.getLogger(__name__)
31
    logger.setLevel(kiss_constants.LOG_LEVEL)
32
    console_handler = logging.StreamHandler()
33
    console_handler.setLevel(kiss_constants.LOG_LEVEL)
34
    formatter = logging.Formatter(kiss_constants.LOG_FORMAT)
35
    console_handler.setFormatter(formatter)
36
    logger.addHandler(console_handler)
37
    logger.propagate = False
38
39
    def __init__(self,
40
                 strip_df_start=True,
41
                 host=None,
42
                 tcp_port=8000):
43
        super(KissTcp, self).__init__(strip_df_start)
44
45
        self.host = host
46
        self.tcp_port = tcp_port
47
        self.socket = None
48
49
        self.logger.info('Using interface_mode=TCP')
50
51
    def __enter__(self):
52
        return self
53
54
    def __exit__(self, exc_type, exc_val, exc_tb):
55
        self.socket.close()
56
57
    def _read_interface(self):
58
        return self.socket.recv(kiss_constants.READ_BYTES)
59
60
    def _write_interface(self, data):
61
        self.socket.write(data)
62
63
    def connect(self, mode_init=None, **kwargs):
64
        """
65
        Initializes the KISS device and commits configuration.
66
67
        See http://en.wikipedia.org/wiki/KISS_(TNC)#Command_codes
68
        for configuration names.
69
70
        :param **kwargs: name/value pairs to use as initial config values.
71
        """
72
        self.logger.debug('kwargs=%s', kwargs)
73
74
        address = (self.host, self.tcp_port)
75
        self.socket = socket.create_connection(address)
76
77
    def close(self):
78
        super(KissTcp, self).close()
79
80
        if not self.socket:
81
            raise RuntimeError('Attempting to close before the class has been started.')
82
83
        self.socket.shutdown()
84
        self.socket.close()
85
86
    def shutdown(self):
87
        self.socket.shutdown()
88