handler.py 2.07 KB
Newer Older
Aurel's avatar
Aurel committed
1
#
Grégory Wisniewski's avatar
Grégory Wisniewski committed
2
# Copyright (C) 2009-2010  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 15
# 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
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Aurel's avatar
Aurel committed
17

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

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

    def connectionCompleted(self, conn):
        # connected to admin node
26
        self.app.connected = True
Aurel's avatar
Aurel committed
27 28
        EventHandler.connectionCompleted(self, conn)

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

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

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

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

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

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

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

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