Хранилища Subversion pytwidcpp

Редакция

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

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