fixbot/api/amp.py
author Tero Marttila <terom@fixme.fi>
Sat, 20 Feb 2010 22:31:17 +0200
changeset 62 e4db89a5f6bc
child 64 8574aeff9b36
permissions -rw-r--r--
restructure api, using AMP as the client-server protocol
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
from twisted.protocols import amp
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
from fixbot.module import ModuleInfo, Event
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
class CmdModuleRegister (amp.Command) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
        Register module
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    arguments = [
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        ('name',    amp.String()),
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    ]
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
class CmdModuleEvent (amp.Command) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
        Broadcast event
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    arguments = [
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        ('type',    amp.String()),
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        ('msg',     amp.String()),
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    ]
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
class ServerProtocol (amp.AMP) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        Nexus-side command handler
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    # the registered ModuleInfo
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    module = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    @CmdModuleRegister.responder
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    def on_ModuleRegister (self, name) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        # construct the ModuleInfo
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        mi = ModuleInfo()
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        mi.name = name
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        # register
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        self.factory.nexus.registerModule(mi, self)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        self.module = mi
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        # ok
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        return {}
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    @CmdModuleEvent.responder
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    def on_ModuleEvent (self, type, msg) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        # as Event
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        e = Event(self.module, type, msg)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        # publish
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        self.factory.nexus.handleEvent(e)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        # ok
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        return {}
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
class ClientProtocol (amp.AMP) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        Module-side command sender/handler
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    def connectionMade (self) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
            Connected to nexus, send ModuleRegister
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
        # register
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        self.sendModuleRegister(self.factory.name).addCallback(self._ModuleRegisterOK)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    def sendModuleRegister (self, name) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
            Register with given module name
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
        return self.callRemote(CmdModuleRegister, name=name)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    def _ModuleRegisterOK (self, ret) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
            Registered with nexus, commence operation
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
        self.factory._onRegistered(self)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    def sendEvent (self, event) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
            Broadcast event to nexus
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        self.callRemote(CmdModuleEvent, type=event.type, msg=event.msg)
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95