Хранилища Subversion pytwidcpp

Редакция

Редакция 3 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 3 Редакция 4
1
# -*- coding: utf-8 -*-
1
# -*- coding: utf-8 -*-
2
""" PyCODC NMDC/ADC Client """
2
""" PyCODC NMDC/ADC Client """
3
from twisted.internet import protocol
3
from twisted.internet import protocol
4
from twisted.protocols import basic
4
from twisted.protocols import basic
5
from twisted.python import log
5
from twisted.python import log
6
6
7
import socket
7
import socket
8
import re
8
import re
9
9
10
class NMDC(protocol.Protocol):
10
class NMDC(basic.LineReceiver):
11
    """
11
    """
12
    NMDC Protocol implementation
12
    NMDC Protocol implementation
13
    """
13
    """
14
14
-
 
15
    delimiter = "|"
-
 
16
15
    buffer = ""
17
    buffer = ""
16
    encoding = None
18
    encoding = None
17
    hostname = None
19
    hostname = None
18
20
19
    def dataReceived(self, data):
21
    def lineReceived(self, line):
20
        print "Data: %s" % data
-
 
21
        lines = (self.buffer + data).split("|")
-
 
22
        print self.buffer
-
 
23
        for line in lines:
-
 
24
            if len(line) <= 2:
-
 
25
                continue
-
 
26
            print "Line: %s" % line
22
        print "Line Received: %s" % line
27
            command, params = self.parseLine(line)
23
        command, params = self.parseLine(line)
28
        self.handleCommand(command, params)
24
        self.handleCommand(command, params)
29
25
30
    def connectionLost(self, reason):
26
    def connectionLost(self, reason):
31
        print "Conlost: %s" %self
27
        print "Conlost: %s" %self
32
        pass
-
 
33
28
34
    def connectionMade(self):
29
    def connectionMade(self):
35
        print "Made connect"
30
        print "Made connect"
36
        if self.hostname is None:
31
        if self.hostname is None:
37
            self.hostname = socket.getfqdn()
32
            self.hostname = socket.getfqdn()
38
33
39
    def sendLine(self, line):
34
    def sendLine(self, line):
40
        _to_send = line.encode('utf-8')
35
        _to_send = line.encode('utf-8')
41
        self.transport.write(_to_send)
36
        self.transport.write(_to_send)
42
37
43
    def parseLine(self, line):
38
    def parseLine(self, line):
44
        """
39
        """
45
        Returns NMDC command and params
40
        Returns NMDC command and params
46
        """
41
        """
47
        print "parse: %s" % line
42
        print "parse: %s" % line
48
        if line.startswith("$"):
43
        if line.startswith("$"):
49
            try:
44
            try:
50
                m = re.match("^\$(?P<command>[a-zA-Z]+)[ ](?P<params>.+$)", line)
45
                m = re.match("^\$(?P<command>[a-zA-Z]+)[ ](?P<params>.+$)", line)
51
46
52
                command = m.group("command")
47
                command = m.group("command")
53
                params = m.group("params")
48
                params = m.group("params")
54
                print "NMDC Command: %s; Params: %s" %(command, params)
49
                print "NMDC Command: %s; Params: %s" %(command, params)
55
            except:
50
            except:
56
                print "Exception: %s"% line
51
                print "Exception: %s"% line
57
            return command, params
52
            return command, params
58
        else:
53
        else:
59
            return "", ""
54
            return "", ""
60
55
61
    def handleCommand(self, command, params):
56
    def handleCommand(self, command, params):
62
        print "handleCommand: %s %s" % (command, params)
57
        print "handleCommand: %s %s" % (command, params)
63
        method = getattr(self, "nmdc_%s" % command, None)
58
        method = getattr(self, "nmdc_%s" % command, None)
64
        try:
59
        try:
65
            if method is not None:
60
            if method is not None:
66
                method(params)
61
                method(params)
67
            else: self.nmdc_unknown(command, params)
62
            else: self.nmdc_unknown(command, params)
68
        except:
63
        except:
69
            print "handleCommand except"
64
            print "handleCommand except"
70
            log.deferr()
65
            log.deferr()
71
        pass
66
        pass
72
67
73
    def makeAnswer(self, command, params):
68
    def makeAnswer(self, command, params):
74
        if params is not "":
69
        if params is not "":
75
            answer = "$%s %s|" % (command, params)
70
            answer = "$%s %s|" % (command, params)
76
        else:
71
        else:
77
            answer = "$%s|"% command
72
            answer = "$%s|"% command
78
73
79
        self.sendLine(answer)
74
        self.sendLine(answer)
80
        print "makeAnswer: %s" % answer
75
        print "makeAnswer: %s" % answer
81
76
82
    def nmdc_unknown(self, command, params):
77
    def nmdc_unknown(self, command, params):
83
        print "nmdc_%s doesn't exist. Called with params: %s" % (command,params)
78
        print "nmdc_%s doesn't exist. Called with params: %s" % (command,params)
84
79
85
 
80