Nafai77 (owner)

Revisions

gist: 203356 Download_button fork
public
Public Clone URL: git://gist.github.com/203356.git
Embed All Files: show embed
Python #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class CommandProtocol(basic.LineReceiver):
    cmds = {}
 
    delimiter = '\n'
 
    def lineReceived(self, line):
        first_space = line.find(' ')
        if first_space != -1:
            cmd = line[:first_space]
            args = line[first_space + 1:].strip()
        else:
            self.transport.write('Invalid input.\n')
            return self.transport.loseConnection()
 
        if cmd not in self.cmds:
            self.transport.write('Invalid input.\n')
            return self.transport.loseConnection()
 
        res = self.cmds[cmd](self, self.repairService, args)
 
        res.addCallbacks(self.writeResults, self.indicateError)
        res.addBoth(self.transport.loseConnection)
 
    def writeResults(self, results):
        if not results.endswith('\n'):
            results += '\n'
 
        self.transport.write(results)
 
    def indicateError(self, err):
        self.transport.write(err.getErrorMessage())
 
class StatusProtocol(CommandProtocol):
      cmds = {}
 
StatusProtocol.cmds.update({
        "start_repair": startRepairCommand,
        "start_next_operation": startNextOperationCommand, })