Хранилища Subversion pytwidcpp

Редакция

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

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