source: epoptes/trunk/fuentes/epoptes/daemon/guiplex.py @ 295

Last change on this file since 295 was 295, checked in by mabarracus, 4 years ago

copy trusty epoptes code

File size: 2.6 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4###########################################################################
5# GUI plex.
6#
7# Copyright (C) 2010 Fotis Tsamis <ftsamis@gmail.com>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FINESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# On Debian GNU/Linux systems, the complete text of the GNU General
23# Public License can be found in `/usr/share/common-licenses/GPL".
24###########################################################################
25
26import os
27import tempfile
28
29from twisted.internet import protocol
30from twisted.protocols import amp
31
32import commands
33import exchange
34
35class GUI(amp.AMP):
36
37    def connectionMade(self):
38        amp.AMP.connectionMade(self)
39        exchange.knownGUIs.append(self)
40
41
42    def connectionLost(self, reason):
43        amp.AMP.connectionLost(self, reason)
44        exchange.knownGUIs.remove(self)
45
46
47    def clientConnected(self, handle):
48        self.callRemote(commands.ClientConnected, handle=handle)
49
50
51    def clientDisconnected(self, handle):
52        self.callRemote(commands.ClientDisconnected, handle=handle)
53
54
55    @commands.EnumerateClients.responder
56    def enumerateClients(self):
57        return {'handles': sorted(exchange.knownClients.iterkeys())}
58
59
60    @commands.ClientCommand.responder
61    def clientCommand(self, handle, command):
62        if handle not in exchange.knownClients:
63            print " Unknown client %s, can't execute %s" %(handle,command)
64            #raise ValueError("Unknown client")
65            return {'filename': '', 'result': ''}
66
67        d = exchange.knownClients[handle].command(command.encode("utf-8"))
68       
69        def sendResult(result):
70            if len(result) > 65000:
71                tf = tempfile.NamedTemporaryFile('wb', dir="/var/run/epoptes", delete=False)
72                tf.write(result)
73                os.fchmod(tf.file.fileno(), 0660)
74                return {'filename': tf.name, 'result': ''}
75            else:
76                return {'result': result, 'filename': ''}
77
78        d.addCallback(sendResult)
79        return d
80
81
82class GUIFactory(protocol.ServerFactory):
83    protocol = GUI
Note: See TracBrowser for help on using the repository browser.