fixbot/module.py
author Tero Marttila <terom@fixme.fi>
Sat, 20 Feb 2010 23:06:43 +0200
changeset 64 8574aeff9b36
parent 62 e4db89a5f6bc
permissions -rw-r--r--
blind error handling tweaks
from twisted.internet import protocol
from twisted.application import service, internet
from twisted.python import log

import datetime

class ModuleInfo (object) :
    """
        Nexus-side handle on a Module
    """

    # module's name
    name = None

    def __str__ (self) :
        return "%s" % (self.name)
    
    def __repr__ (self) :
        return "<module %s>" % (self.name, )


class Event (object) :
    """
        An Event, sent by a Module to the Nexus, to be distributed further
    """

    # the ModuleInfo object
    module = None

    # the event type as a string
    type = None

    # event message as a string (up to 64k, although that won't fit onto IRC..)
    msg = None

    # timestamp as a datetime.datetime
    when = None

    def __init__ (self, module, type, msg) :
        self.module = module
        self.type = type
        self.msg = msg

        self.when = datetime.datetime.now()
    
    def __str__ (self) :
        return "[%s] %s" % (self.type, self.msg)
    
    def __repr__ (self) :
        return "%s @ %s" % (self.type, self.when)


class Module (ModuleInfo, protocol.ClientFactory) :
    """
        Module core, handles the API connection state and processes all messages
    """
    
    # our API connection to the Nexus
    connection = None

    def __init__ (self, config, protocol) :
        """
            config      - configuration for connecting to nexus
            protocol    - API client protocol to use for this factory
        """
        
        self.protocol = protocol

        self.connection = None

        # XXX: legacy: self.secret = config['api-secret']


    def _onRegistered (self, connection) :
        """
            Connected to nexus and registered
        """

        log.msg("Connected and registered")

        self.connection = connection
        
        # XXX: abort on errors?
        self.handleConnect()

    
    # XXX: unused, bad interface
    def disconnect (self) :
        """
            Disconnect from Nexus
        """

        self.connection.transport.loseConnection()


    def sendEvent (self, type, msg) :
        """
            Send event to nexus
        """

        self.connection.sendEvent(Event(self, type, msg))


    def handleConnect (self) :
        """
            Do something once we are connected to nexus and registered
        """

        pass

    def abort (self, err) :
        """
            Abort this module, disconnecting with the given error
        """

        self.connection.abort(str(err))

def makeService (module_class, config, protocol) :
    s = service.MultiService()

    # build factory
    factory = module_class(config, protocol)
    
    # the API client
    log.msg("Connecting to API server on [%s:%d]" % (config['api-server'], config['api-port']))
    api_client = internet.TCPClient(config['api-server'], config['api-port'], factory)
    
    api_client.setServiceParent(s)

    return s