initial code
authorTero Marttila <terom@fixme.fi>
Thu, 20 Mar 2008 17:22:24 +0200
changeset 0 b610bb7f8823
child 1 16d7aadc6f31
initial code

committer: Tero Marttila <terom@fixme.fi>
buffer.py
event.py
nexus.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.py	Thu Mar 20 17:22:24 2008 +0200
@@ -0,0 +1,410 @@
+try:
+    from cStringIO import StringIO
+except ImportError:
+    from StringIO import StringIO
+import struct
+
+# prefixed to all struct format strings
+STRUCT_PREFIX = '!'
+
+def hex (bytes) :
+    return ' '.join(['%#04x' % ord(b) for b in bytes])
+    
+class NotEnoughDataError (Exception) : 
+    pass
+
+class IStreamBase (object) :
+    # prefixed to all struct format strings
+    STRUCT_PREFIX = '!'
+
+class IReadStream (IStreamBase) :
+    """
+        IReadStream simply provides various interfaces for reading bytes from a
+        stream in various ways
+    """
+
+    def read (self, size=None) :
+        """
+            Read and return up to the given amount of bytes, or all bytes
+            available if no size given.
+        """
+
+        abstract
+
+    def readStruct (self, fmt) :
+        """
+            Reads the correct amount of data and then unpacks it according to
+            the given format. Note that this always returns a tuple, for single
+            items, use readItem
+        """
+        
+        fmt = self.STRUCT_PREFIX + fmt
+
+        fmt_size = struct.calcsize(fmt)
+        data = self.read(fmt_size)
+        
+        return struct.unpack(fmt, data)
+
+    def readItem (self, fmt) :
+        """
+            Reads the correct amount of data, unpacks it according to the 
+            given format, and then returns the first item.
+        """
+
+        return self.readStruct(fmt)[0]
+
+    def readVarLen (self, len_type) :
+        """
+            Return the data part of a <length><data> structure.
+            len_type indicates what type length has (struct format code).
+
+            In the case of <length> being zero, returns an empty string.
+        """
+        
+        size = self.readItem(len_type)
+        
+        if size :
+            return self.read(size)
+        else :
+            return ""
+
+    def readEnum (self, enum) :
+        """
+            Returns the item from the given list of enum values that corresponds
+            to a single-byte value read from the stream
+        """
+
+        return enum[self.readItem('B')]
+
+class ISeekableStream (IStreamBase) :
+    """
+        Extends IStreamBase to provide the ability to seek backwards into the
+        stream (which still does not know it's length, and thence cannot seek
+        forwards).
+    """
+
+    _position = None
+
+    def tell (self) :
+        """
+            Return the current offset into the seekable stream
+        """
+        
+        abstract
+
+    def seek (self, pos) :
+        """
+            Seek to the given position in the stream. 
+        """
+
+        abstract
+
+    def mark (self) :
+        """
+            Set a mark that can be later rolled back to with .reset()
+        """
+        
+        self._position = self.tell()
+
+    def unmark (self) :
+        """
+            Remove the mark without affecting the current position
+        """
+        
+        self._position = None
+    
+    def reset (self) :
+        """
+            Rolls the buffer back to the position set earlier with mark()
+        """
+        
+        if self._position is not None :
+            self.seek(self._position)
+            self._position = None
+
+        else :
+            raise Exception("reset() without mark()")
+
+class ISeekableReadStream (ISeekableStream, IReadStream) :
+    def peek (self, len=None) :
+        """
+            Return a string representing what buf.read(len) would return, but do
+            not affect future read operations
+        """
+
+        pos = self.tell()
+
+        data = self.read(len)
+
+        self.seek(pos)
+        
+        return data
+    
+
+class INonBlockingReadStream (IReadStream) :
+    """
+        Otherwise identical to IReadStream, but read will either return size
+        bytes, or raise a NotEnoughDataError
+    """
+    
+    pass
+
+class IWriteStream (IStreamBase) :
+    """
+        IWriteStream provides various ways to write data to a byte stream
+    """
+
+    def write (self, data) :
+        """
+            Write the given bytes to the stream
+        """
+
+        abstract
+
+    def writeStruct (self, fmt, *args) :
+        """
+            Pack the given arguments with the given struct format, and write it
+            to the stream.
+        """
+
+        self.write(struct.pack(self.STRUCT_PREFIX + fmt, *args))
+        
+    def writeVarLen (self, len_type, data) :
+        """
+            Write a <length><data> field into the buffer. Len_type is the
+            struct format code for the length field.
+        """
+
+        self.writeStruct(len_type, len(data))
+        self.write(data)
+
+    def writeEnum (self, enum, name) :
+        """
+            Write the single-byte value correspnding to the given name's
+            position in the given enum
+        """
+
+        self.writeStruct('B', enum.index(name))
+
+class IBufferBase (ISeekableStream) :
+    """
+        A buffer simply provides a way to read and write data to/from a byte
+        sequence stored in memory.
+    """
+
+    def tell (self) :
+        return self._buf.tell()
+    
+    def seek (self, offset) :
+        return self._buf.seek(offset)
+
+    def getvalue (self) :
+        """
+            Returns the value of the buffer, i.e. a string with the contents of
+            the buffer from position zero to the end.
+        """
+
+        return self._buf.getvalue()
+
+class ReadBuffer (INonBlockingReadStream, ISeekableReadStream, IBufferBase) :
+    """
+       A read-only buffer. Can be initialized with a given value and then later
+       replaced in various ways, but cannot be modified.
+    """
+
+    def __init__ (self, data="") :
+        """
+            Initialize the buffer with the given data
+        """
+
+        self._buf = StringIO(data)
+    
+    def read (self, size=None) :
+        """
+            Return the given number of bytes, or raise a NotEnoughDataError
+        """
+
+        if size == 0 :
+            raise ValueError("can't read zero bytes")
+         
+        if size :
+            ret = self._buf.read(size)
+        else :
+            ret = self._buf.read()
+
+        if size and len(ret) < size :
+            raise NotEnoughDataError()
+
+        return ret    
+    
+    def append (self, data) :
+        """
+            Modify the buffer such that it contains the old data from this
+            buffer, and the given data at the end. The read position in the buffer
+            is kept the same.
+        """
+
+        pos = self.tell()
+
+        self._buf = StringIO(self._buf.getvalue() + data)
+
+        self.seek(pos)
+
+    def chop (self) :
+        """
+            Discard the data in the buffer before the current read position.
+            Also removes any marks.
+        """
+
+        self._position = None
+        
+        self._buf = StringIO(self.read())
+
+    def processWith (self, func) :
+        """
+            Call the given function with this buffer as an argument after
+            calling mark(). If the function 
+                a) returns None, the buffer is .chop()'d, and we repeat the
+                   process.
+                b) raises a NotEnoughDataError, whereupon the buffer is rolled
+                   back to where it was before calling the function with 
+                   chop().
+                c) raises a StopIteration, whereupon we chop the buffer and 
+                   return.
+                d) returns something (i.e. ret is not None), whereupon we
+                   return that (and leave the current buffer position untouched).
+        """
+        ret = None
+        
+        try :
+            while ret is None :
+                self.mark()  # mark the position of the packet we are processing
+                ret = func(self)
+
+                if ret is None :
+                    # discard the processed packet and proceed to the next one
+                    self.chop()
+                
+        except NotEnoughDataError, e :
+            self.reset() # reset position back to the start of the packet
+            return e
+            
+        except StopIteration, e:
+            self.chop()
+            return e # processed ok, but we don't want to process any further packets
+            
+        else :
+            return ret
+
+class WriteBuffer (IWriteStream, IBufferBase) :
+    """
+        A write-only buffer. Data can be written to this buffer in various
+        ways, but cannot be read from it except as a whole.
+    """
+
+    def __init__ (self) :
+        """
+            Initialize the buffer
+        """
+
+        self._buf = StringIO()
+
+    def write (self, data) :
+        """
+            Write the given data to the current position in the stream,
+            overwriting any previous data in that position, and extending
+            the buffer if needed
+        """
+
+        return self._buf.write(data)
+
+def readStringStream (stream, varlen_type) :
+    """
+        Does readVarLen on an IReadStream until it returns something that evaluates to false ( == zero-length string)
+    """
+
+    while True :
+        item = stream.readVarLen(varlen_type)
+
+        if item :
+            yield item
+        else :
+            return
+
+def writeStringStream (stream, varlen_type, strings) :
+    """
+    """
+
+
+class StreamProtocol (object) :
+    """
+        A mixin to let you use Buffer with twisted.internet.protocol.Protocol
+    """
+    
+    # a list of receivable command names 
+    RECV_COMMANDS = None
+
+    # a list of sendable command names
+    SEND_COMMANDS = None
+
+    def __init__ (self) :
+        """
+            Initialize the cross-dataReceived buffer
+        """
+
+        self.in_buffer = ReadBuffer()
+
+    def send (self, buf) :
+        """
+            Write the contents of the given WriteBuffer to the transport
+        """
+
+        self.transport.write(buf.getvalue())
+
+    def dataReceived (self, data) :
+        """
+            Buffer the incoming data and then try and process it
+        """
+
+        self.in_buffer.append(data)
+        
+        ret = self.in_buffer.processWith(self.processPacket)
+        
+    def processPacket (self, buf) :
+        """
+            Call processCommand with the buffer, handling the return value (either None or a deferred)
+        """
+
+        ret = self.processCommand(buf)
+
+        if ret :
+            ret.addCallback(self.send)
+
+    def processCommand (self, buf) :
+        """
+            Process a command from the given buffer. May return a callback
+        """
+
+        return self.readMethod(buf, self.RECV_COMMANDS, buf)
+
+    # conveniance read/write
+    def startCommand (self, cmd) :
+        buf = WriteBuffer()
+        
+        buf.writeEnum(self.SEND_COMMANDS, cmd)
+
+        return buf
+    
+    def readMethod (self, buf, methods, *args, **kwargs) :
+        """
+            Reads a single-byte <methods>-enum value from the given buffer and
+            use it to find the corresponding method (as <prefix>_<method-name>,
+            prefix can be overriden with a keyword argument and defaults to
+            'on'. If any extra arguments are given, they will be passed to the
+            method.
+        """
+
+        prefix = kwargs.pop("prefix", "on")
+
+        return getattr(self, "%s_%s" % (prefix, buf.readEnum(methods)))(*args, **kwargs)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/event.py	Thu Mar 20 17:22:24 2008 +0200
@@ -0,0 +1,38 @@
+from datetime import datetime
+
+class ModuleInfo (object) :
+    """
+        Some info about a module
+    """
+
+    # module's name
+    name = None
+
+    # module's version, as a 16-bit integer
+    version = None
+
+    # list of valid event types (strings)
+    event_types = None
+
+class Event (object) :
+    # the ModuleInfo object
+    module = None
+
+    # the event type as an integer
+    type = None
+
+    # event message as a string (under 255 bytes in length!)
+    msg = None
+
+    # timestamp as a datetime.datetime
+    when = None
+
+    def __init__ (self, type, msg) :
+        self.type = type
+        self.msg = msg
+
+        self.when = datetime.now()
+
+    def __str__ (self) :
+        return "[%s] %s" % (self.module.event_types[self.type], self.msg)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nexus.py	Thu Mar 20 17:22:24 2008 +0200
@@ -0,0 +1,95 @@
+from twisted.words.protocols import irc
+from twisted.internet import reactor, protocol
+from twisted.python import log
+
+import buffer
+
+NICKNAME        = "FixBot"
+USERNAME        = "fixme"
+CHANNEL         = "#fixme"
+API_PORT        = 34888
+
+class IRCBot (irc.IRCClient) :
+    """
+        Fixme IRC bot
+    """
+    
+    # housekeeping
+    def connectionMade (self) :
+        log.msg("Connected")
+        super(FixBot, self).connectionMade()
+
+    def connectionLost (self, reason) :
+        log.msg("Connection lost: %s" % reason)
+        super(FixBot, self).connectionLost(reason)
+
+    def signedOn (self) :
+        log.msg("Signed on, joining channel %s" % channel)
+        self.join(CHANNEL)
+
+    def joined (self, channel) :
+        log.msg("Joined channel %s" % channel)
+    
+    # our actual functionality
+    def sendEvent (self, module, event) :
+        self.msg(CHANNEL, "[%s] %s" % (module, event))
+
+    def moduleConnected (self, module, version) :
+        self.msg(CHANNEL, "{modules} Module %s connected, version %s" % (module, version))
+
+    def moduleDisconnected (self, module) :
+        self.msg(CHANNEL, "{modules} Module %s disconnected" % (module, ))
+
+class APIProtocol (buffer.StreamProtocol, protocol.Protocol) :
+    RECV_COMMANDS = [
+        "module_init",
+        "module_event"
+    ]
+
+    SEND_COMMANDS = [
+
+    ]
+
+    VALID_STATES = [
+        "wait_init",
+        "wait_event"
+    ]
+    
+    # proto state
+    state = None
+
+    # module info
+    module = None
+
+
+    def on_module_init (self, i) :
+        m = self.module = ModuleInfo()
+
+        m.name = i.readVarLen('B')
+        m.version = i.readItem('H')
+
+        
+        
+        addr = self.transport.getPeer()
+
+        self.module_name = module_name
+
+        log.msg("Got mod_init for %s:%d from %s:%d" % (module_name, module_version, addr.host, addr.port))
+
+        self.factory.registerModule(module_name, module_version, self)
+
+    def on_module_event (self, i) :
+        event_type = i.readVarLen('B')
+        event_msg = i.readVarLen('B')
+
+        log.msg("Got mod_event of type %s" % (event_type, ))
+
+        self.factory.handleEvent(self, event_type, event_msg)
+
+class IRCFactory (protocol.ClientFactory) :
+    protocol = IRCBot
+
+    def __init__ (self) :
+
+class APIFactory (protocol.ServerFactory
+    protocol = APIProtocol