On 2012-09-19 23:30:25 +0000, James Cameron said:

> It is a scriptable observer client without a graphics head.  It was
> cut down from an earlier version of Gytha.  It can be used as the
> basis for a protocol sniffer, but by far the best sniffer we have at
> the moment is Gytha.

James can you give a description of how this code works?

I've never seen the __metaclass__ stuff.

I understand in Python classes are objects and metaclasses are classes' 
classes :-) Duplicating the functionality in obj-c has been a stumbling 
block.  Just hoping for some plain English on how this works so I 
better understand how to simulation the concept in obj-c.



""" server originated packets
"""

sp_table = {}

class ServerPacket(type):
    def __new__(cls, name, bases, dct):
        server_packet = type.__new__(cls, name, bases, dct)

        if dct['code'] not in sp_table:
            obj = server_packet()
            sp_table[server_packet.code] = (
                struct.calcsize(server_packet.format), obj)
            if name.lower() not in globals():
                globals()[name.lower()] = obj

        return server_packet

class SP:
    __metaclass__ = ServerPacket
    code = -1
    format = ''

    def find(self, number):
        """ given a packet type return a tuple consisting of
            (size, instance), or (1, self) if type not known
        """
        if number not in sp_table:
            return (1, self)
        return sp_table[number]

    def handler(self, data):
        raise NotImplemented


class SP_MOTD(SP):
    code = 11
    format = '!bxxx80s'

    def handler(self, data):
        (ignored, message) = struct.unpack(self.format, data)
        message = strnul(message)
        if opt.sp: print "SP_MOTD message=", message
        galaxy.motd.add(message)
-- 
Bob Tanner <basic at us.netrek.org>          
Key fingerprint = 9906 320A 8BB6 64AD 96A7  7785 CBFB 10BF 568B F98C