protocol-example /

from inspect import currentframe
import struct

i64 = {'ctypes': 'q', 'java': 'long'}
i16 = {'ctypes': 'h', 'java': 'short'}
string = {'ctypes': 'x', 'java': 'String'}
def mktoken(name, types):
    """Create a token"""
    return Token(types['java'], name, fmt=types['ctypes'], 
                 simple=not types['java'] == "String")

def t(name, data_type):
    """Inserts name = (name, data_type) in locals()
    of calling scope"""
    currentframe().f_back.f_locals[name] = mktoken(name, data_type)

class Token(object):
    """Represents one or more tokens"""
    simple = True

    def __init__(self, javaname, name, fmt="x", simple=True):
        self.simple = simple
        self.fmt = fmt
        self.javaname = javaname = name
        self.pattern_size = struct.calcsize(self.fmt)

    def parse(self, buf):
        if self.simple:
            values = struct.unpack("!%s" % (self.fmt), 
            return { values[0]}
            strlen = struct.unpack("!h",[:2])[0]
            str_ = struct.unpack("!%ss" % (strlen,),[2:2+strlen])[0]
            return { str_} 
    def __str__(self):
        type_ = "simple" if self.simple else "string"
        return "%s token with format %s" % (type_, self.fmt)
class Buffer(object):
    """Class to hold a message buffer. Each token will
    consume parts of the buffer as it parses the message"""
    def __init__(self, data): = data  
class Message(object):
    """A message, knows how to parse data"""
    def __init__(self, msgname, msgdesc, msgtype, *tokens):
        self.tokens = tokens
        self.__doc__ = msgdesc
        self.parsers = self.tokens = msgname
        self.type = hex(msgtype) #for java
    def parse(self, data):
        buf = Buffer(data[1:]) #remove type byte
        result = {'type':}
        for res in [x.parse(buf) for x in self.parsers]:            
        assert len( == 0
        return result
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.