Commits

Fredrik Håård committed 4cf7289 Draft

Updated logic from production

Comments (0)

Files changed (3)

 """
 This is a test protocol, that contains a few messages for illustration
 """
-from ptypes import t, i16, i64, string
+from ptypes import i16, i64, string
 
 #Tokens
-t('message', string)
-t('timestamp',  i64)
-t('signal', i16)
-t('voltage', i16)
+string('message')
+i64('timestamp')
+i16('signal')
+i16('voltage')
 
 #Messages
 LOG = ('A log message containing some debug info', 
 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)
+def typedef(type_spec):
+    """Function that creates a type function,
+    which associates a name with a type specification"""
+    def type_fn(name):
+        """Inject the type definition type_def into 
+        the calling scope with the given name"""
+        frame = currentframe().f_back
+        frame.f_locals[name] =  mktoken(name, type_spec)       
+        del frame
+    return type_fn
+
+i64 = typedef({'ctypes': 'q', 'java': 'long'})
+i16 = typedef({'ctypes': 'h', 'java': 'short'})
+string = typedef({'ctypes': 'x', 'java': 'String'})
 
 class Token(object):
     """Represents one or more tokens"""
 import protocols, struct
+from makejava import javaprotocol
 
 protocols = protocols.load_protocols(".")
 
 result = protocols[4.0].parse(test_data)
 
 assert result == {'timestamp': 123456789, 'type': 'ALIVE'}
-print("OK")
+assert (javaprotocol(protocols[4.0]) == ('Protocol40', '\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.DataOutputStream;\n\npublic class Protocol40 {\n    public static String VERSION = "4.0";\n    \npublic static final byte[] packAlive(long timestamp) {\n    try\n    {\n        final ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        final DataOutputStream daos = new DataOutputStream(baos);\n        daos.writeByte(0x21);\n        daos.writeLong(timestamp);\n        final byte[] data = baos.toByteArray();\n        baos.close();\n        daos.close();\n        return data;\n    } catch (Exception e) {\n        e.printStackTrace();\n    }    \n    return null;\n}\n\n\n\npublic static final byte[] packLog(long timestamp,String message,short signal,short voltage) {\n    try\n    {\n        final ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        final DataOutputStream daos = new DataOutputStream(baos);\n        daos.writeByte(0x23);\n        daos.writeLong(timestamp);\n        daos.writeString(message);\n        daos.writeShort(signal);\n        daos.writeShort(voltage);\n        final byte[] data = baos.toByteArray();\n        baos.close();\n        daos.close();\n        return data;\n    } catch (Exception e) {\n        e.printStackTrace();\n    }    \n    return null;\n}\n\n}\n')
+)
+
+print("OK")