fixbot/nexus.py
author Tero Marttila <terom@fixme.fi>
Sat, 20 Feb 2010 22:31:17 +0200
changeset 62 e4db89a5f6bc
parent 61 1e26ee53381f
permissions -rw-r--r--
restructure api, using AMP as the client-server protocol
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     1
from twisted.application import internet, service
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     2
from twisted.internet import reactor, protocol
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     3
from twisted.python import log
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     4
import sys
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     5
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     6
import irc, api
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     7
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
     8
class Nexus (object) :
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
     9
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    10
        The core component, which has the set of registered ModuleInfo's from api, and the IRC connection
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    11
    """
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    12
    
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    13
    # runtime config options as a dict
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    14
    config = None
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    15
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    16
    # the irc.Factory instance
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    17
    irc = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    18
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    19
    # the api.ServerFactory instance
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    20
    api = None
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    21
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    22
    def __init__ (self, config) :
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    23
        self.config = config
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    24
        self.modules = dict()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    25
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    26
    def registerModule (self, module, transport) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    27
        self.modules[module.name] = (module, transport)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    28
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    29
        self.irc.connection.moduleConnected(module, transport.transport.getPeer())
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    30
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    31
    def unregisterModule (self, module, reason) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    32
        del self.modules[module.name]
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    33
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    34
        self.irc.connection.moduleDisconnected(module, reason)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    35
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    36
    def handleEvent (self, event) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    37
        self.irc.connection.sendEvent(event)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    38
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    39
    def getModules (self) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    40
        return (module for (module, transport) in self.modules.itervalues())
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    41
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    42
    def getModuleInfo (self, name) :
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    43
        module, connection = self.modules[name]
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    44
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    45
        return module, connection.transport.getPeer()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    46
 
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    47
def makeService (config) :
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    48
    n = Nexus(config)
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    49
    s = service.MultiService()
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    50
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    51
    # the IRC side
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    52
    n.irc = irc.Factory(n, 
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    53
        config['irc-nickname'], 
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    54
        config['irc-username'],
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    55
        config['irc-channel']
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    56
    )
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    57
    
37
6fd6a9fdc001 add --config, and rename some of the option names/shortnames
Tero Marttila <terom@fixme.fi>
parents: 35
diff changeset
    58
    log.msg("Connecting to IRC server on [%s:%d]", config['irc-server'], config['irc-port'])
6fd6a9fdc001 add --config, and rename some of the option names/shortnames
Tero Marttila <terom@fixme.fi>
parents: 35
diff changeset
    59
    irc_client = internet.TCPClient(config['irc-server'], config['irc-port'], n.irc)
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    60
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    61
    irc_client.setServiceParent(s)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    62
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    63
    # the API side
62
e4db89a5f6bc restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents: 61
diff changeset
    64
    n.api = api.ServerFactory(n, config)
21
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    65
    
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    66
    log.msg("Starting API server on [%s:%d]", config['api-port'], config['api-listen'])
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    67
    api_server = internet.TCPServer(config['api-port'], n.api, interface=config['api-listen'])
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    68
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    69
    api_server.setServiceParent(s)
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    70
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    71
    # return the service collection
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    72
    return s
aa6df8f9c44a add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff changeset
    73