Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
747 olof 1
###########################################################
2
#
3
# Node interface through raw TCP socket (telnet)
4
#
5
###########################################################
6
import logging as log
7
from NodeIfBase import NodeIfBase
8
from threading import Thread
9
from TCPClient import TCPClient
10
import time
11
 
12
class TCPThread(Thread):
13
    ownerNotifier = None
14
    terminated = False
15
    tcpClient = None
16
    pktHandler = None
17
    host = None
18
    port = None
19
 
20
    def __init__ (self, pktHandler, ownerNotifier, host, port):
21
        Thread.__init__(self)
22
        self.pktHandler = pktHandler
23
        self.ownerNotifier = ownerNotifier
24
        self.host = host
25
        self.port = port
26
 
27
    def cleanup(self):
28
        if self.tcpClient is not None:
29
            self.tcpClient.disconnect()
30
        self.ownerNotifier.notify(self.ownerNotifier.TERMINATE)
31
 
32
    def run(self):
33
        try:
34
            log.debug('TCPThread.run')
35
 
36
            self.tcpClient = TCPClient(self.host, self.port)
37
            if not self.tcpClient.connect(10):
38
                print 'Connection to host failed'
39
                self.terminated = True
40
 
41
            while not self.terminated:
42
                data = self.tcpClient.read()
43
                if data is not None:
44
                    data = data[1:-3] # remove leading ' and trailing \n'
45
                    self.pktHandler.input(data)
46
 
47
                time.sleep(0.001)
48
            self.cleanup()
49
        except: # sys.excepthook does not work in threads
50
            import traceback
51
            traceback.print_exc()
52
            self.terminated = True
53
            self.cleanup()
54
 
55
    def terminate(self):
56
        log.debug('TCPThread.terminate')
57
        self.terminated = True
58
 
59
class NodeIfTCP:
60
 
61
    DEFAULT_CONFIG = {'host':'sra.eta.chalmers.se',
62
                      'port':8002,
63
                      }
64
 
65
    config = {}
66
    pktHandler = None
67
    tcpThread = None
68
    ownerNotifier = None
69
 
750 olof 70
    def __init__(self, pktHandler, cfg = None):
747 olof 71
        if cfg is None:
72
            self.config = self.DEFAULT_CONFIG
73
        else:
750 olof 74
            self.config = cfg
747 olof 75
        self.pktHandler = pktHandler
750 olof 76
        log.debug('Creating iftcp interface, config: ' + str(self.config))
747 olof 77
 
78
    def setPktHandler(self, pktHandler):
79
        self.pktHandler = pktHandler
750 olof 80
 
81
    def setIfNotifier(self, ifNotifier):
82
        self.ownerNotifier = ifNotifier
747 olof 83
 
84
    def start(self):
85
        host = self.config['host']
86
        port = self.config['port']
87
        self.tcpThread = TCPThread(self.pktHandler, self.ownerNotifier, host, port)
88
        self.tcpThread.start()
89
        return True
90
 
91
    def stop(self):
92
        if self.running():
93
            self.tcpThread.terminate()
94
        else:
95
            log.debug('TCP interface not started, or already stopped')
96
 
97
    def running(self):
98
        if self.tcpThread is not None:
99
            return not self.tcpThread.terminated
100
        else:
101
            return False