handler.py 2.12 KB
Newer Older
Aurel's avatar
Aurel committed
1
#
2
# Copyright (C) 2009-2012  Nexedi SA
3
#
Aurel's avatar
Aurel committed
4 5 6 7
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
Aurel's avatar
Aurel committed
9 10 11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Aurel's avatar
Aurel committed
16

17 18
from neo.lib.handler import EventHandler
from neo.lib.protocol import ErrorCodes, Packets
Aurel's avatar
Aurel committed
19 20 21 22 23 24

class CommandEventHandler(EventHandler):
    """ Base handler for command """

    def connectionCompleted(self, conn):
        # connected to admin node
25
        self.app.connected = True
Vincent Pelletier's avatar
Vincent Pelletier committed
26
        super(CommandEventHandler, self).connectionCompleted(conn)
Aurel's avatar
Aurel committed
27

28 29 30 31
    def __disconnected(self):
        app = self.app
        app.connected = False
        app.connection = None
Aurel's avatar
Aurel committed
32

33 34
    def __respond(self, response):
        self.app.response_queue.append(response)
Aurel's avatar
Aurel committed
35 36

    def connectionClosed(self, conn):
37 38 39 40 41 42 43
        super(CommandEventHandler, self).connectionClosed(conn)
        self.__disconnected()

    def connectionFailed(self, conn):
        super(CommandEventHandler, self).connectionFailed(conn)
        self.__disconnected()

44 45
    def ack(self, conn, msg):
        self.__respond((Packets.Error, ErrorCodes.ACK, msg))
46

47 48 49
    def protocolError(self, conn, msg):
        self.__respond((Packets.Error, ErrorCodes.PROTOCOL_ERROR, msg))

50 51
    def notReady(self, conn, msg):
        self.__respond((Packets.Error, ErrorCodes.NOT_READY, msg))
52

53 54 55 56
    def __answer(packet_type):
        def answer(self, conn, *args):
            self.__respond((packet_type, ) + args)
        return answer
57

58 59 60 61
    answerPartitionList = __answer(Packets.AnswerPartitionList)
    answerNodeList = __answer(Packets.AnswerNodeList)
    answerClusterState = __answer(Packets.AnswerClusterState)
    answerPrimary = __answer(Packets.AnswerPrimary)