updates
[mTask.git] / mTaskInterpret.icl
index 760281c..51771a3 100644 (file)
@@ -22,20 +22,21 @@ from Text import class Text(concat,join,toUpperCase), instance Text String
 import Text.Encodings.Base64
 
 encode :: MTaskMessage -> String
-encode (MTSds i v) = "s" +++ to16bit i +++ v
-encode (MTTask to data) = "t" +++ to16bit to +++ toString (size data) +++ data
-encode (MTPub i v) = "u" +++ to16bit i +++ v
-encode (MTUpd i v) = "u" +++ to16bit i +++ v
+encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
+encode (MTTask to data) = "t" +++ to16bit to +++ to16bit (size data) +++ data +++ "\n"
+encode (MTPub i v) = "u" +++ to16bit i +++ v +++ "\n"
+encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
+encode MTEmpty = ""
 
 decode :: String -> MTaskMessage
-decode x = case x.[0] of
-       'u' = MTUpd (toInt x.[1]) (x % (2,4))
-       _ = abort ("Didn't understand message: " +++ x)
+decode x
+| size x == 0 = MTEmpty
+= case x.[0] of
+       '\0' = MTEmpty
+       'u' = MTUpd (from16bit (x % (1,3))) (x % (3,5))
+       _ = abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
 
-safePrint :: String -> String
-safePrint s = join " " [saf c\\c<-:s]
-       where
-               saf c = "\x" +++ toString (toInt c)
+safePrint :== toString o toJSON
 
 derive gPrint MTaskMessage
 instance toString MTaskMessage where
@@ -47,6 +48,7 @@ instance toString MTaskMessage where
                +++ " value " +++ safePrint v
        toString (MTUpd i v) = "Update id: " +++ toString i
                +++ " value " +++ safePrint v
+       toString MTEmpty = "Empty message"
 
 toByteVal :: BC -> [Char]
 toByteVal b