From 7edf1780ae3b91d9136db2d94b25e88d64896645 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 9 Jan 2017 13:18:03 +0100 Subject: [PATCH] updates --- Makefile | 2 +- int/Makefile | 2 +- int/main.c | 41 +++++++++++++-------------- int/misc.h | 7 +++++ int/sds.c | 21 +++++--------- int/task.c | 15 ++++------ mTaskInterpret.dcl | 4 ++- mTaskInterpret.icl | 24 ++++++++-------- miTask.icl | 69 +++++++++++++++++++++++----------------------- 9 files changed, 93 insertions(+), 92 deletions(-) diff --git a/Makefile b/Makefile index 3956c1d..4510a04 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CLEAN_HOME?=/opt/clean CLM:=clm -override CLMFLAGS+=-dynamics -l -no-pie -h 200M -nt +override CLMFLAGS+=-dynamics -h 200M -nt CLMLIBS:=\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Dynamics\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Generics\ diff --git a/int/Makefile b/int/Makefile index 2a16a1e..fb845f0 100644 --- a/int/Makefile +++ b/int/Makefile @@ -1,4 +1,4 @@ -CFLAGS:=-g -Wall -Wextra -Werror +CFLAGS:=-g -Wall -Wextra -Werror -std=c99 PROG:=main OBJS:=interpret.o sds.o task.o diff --git a/int/main.c b/int/main.c index 43911bc..de48671 100644 --- a/int/main.c +++ b/int/main.c @@ -1,3 +1,4 @@ +#define _BSD_SOURCE #include #include #include @@ -6,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -31,7 +33,7 @@ int port = 8123; void killHandler(int i) { - printf("%s caught, Bye...\n", strsignal(i)); + printf("%i caught, Bye...\n", i); exit(1); } @@ -81,11 +83,11 @@ void read_message(int fd_in, int fd_out) break; case '\n': break; - case '\0': - debug("iTasks server shut down\n"); - exit(EXIT_SUCCESS); +// case '\0': +// debug("iTasks server shut down\n"); +// exit(EXIT_SUCCESS); default: - debug("Unknown message: %X %X?\n", c, EOF); + debug("Unknown message: %X\n", c); } } @@ -133,22 +135,20 @@ int main(int argc, char *argv[]) } //Command line arguments - int opt; - while((opt = getopt(argc, argv, "hp:")) != -1){ - switch(opt){ - case 'p': - port = atoi(optarg); - if(port < 1) - die("Port numbers are > 1\n"); - break; - case 'h': - usage(stdout, argv[0]); - exit(EXIT_SUCCESS); - default: - usage(stderr, argv[0]); - exit(EXIT_FAILURE); + int opti = 1; + while(opti < argc){ + if(strcmp(argv[opti], "-h") == 0){ + usage(stdout, argv[0]); + exit(EXIT_SUCCESS); + } else if(strcmp(argv[opti], "-p") == 0 && opti+1 1\n"); + } else { + usage(stderr, argv[0]); + exit(EXIT_FAILURE); } - + opti++; } open_filedescriptors(); @@ -182,6 +182,7 @@ int main(int argc, char *argv[]) debug("Waiting for 500ms\n"); usleep(500000); debug("done waiting\n"); + write(fd, "\n", 1); } return 0; } diff --git a/int/misc.h b/int/misc.h index 83e3ac8..033dee6 100644 --- a/int/misc.h +++ b/int/misc.h @@ -14,4 +14,11 @@ #define pdie(s) {perror(s); exit(1);} #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);} +#define read16(fd, c, i) {\ + read(fd, &c, 1); \ + i = 256*c; \ + read(fd, &c, 1); \ + i += c; \ + } + #endif diff --git a/int/sds.c b/int/sds.c index ff37af3..3a14c60 100644 --- a/int/sds.c +++ b/int/sds.c @@ -8,7 +8,7 @@ #include "misc.h" #include "sds.h" -struct sds sdss[MAXSDSS] = {0}; +struct sds sdss[MAXSDSS]; uint8_t c; void sds_register(int fd) @@ -23,16 +23,11 @@ void sds_register(int fd) memset(&sdss[cs], 0, sizeof(struct sds)); //Read identifier - read(fd, &c, 1); - sdss[cs].id = c; - + read16(fd, c, sdss[cs].id); //Read value - read(fd, &c, 1); - sdss[cs].value = 255*c; - read(fd, &c, 1); - sdss[cs].value += c; + read16(fd, c, sdss[cs].value); - debug("Received sds %d: %d\n", sdss[cs].id, sdss[cs].value); + debug("\nReceived sds %d: %d\n", sdss[cs].id, sdss[cs].value); sdss[cs].used = true; } @@ -47,10 +42,7 @@ bool sds_update(int fd) continue; if(sdss[cs].id == id){ //Read value - read(fd, &c, 1); - sdss[cs].value = 255*c; - read(fd, &c, 1); - sdss[cs].value += c; + read16(fd, c, sdss[cs].value); return true; } } @@ -64,7 +56,8 @@ void sds_publish(int id, int fd) for(cs = 0; cs MAXTASKSIZE) die("Task is too long: %d\n", tasks[ct].tlen); //Read task bytecode diff --git a/mTaskInterpret.dcl b/mTaskInterpret.dcl index bca5df7..0fb9efc 100644 --- a/mTaskInterpret.dcl +++ b/mTaskInterpret.dcl @@ -10,9 +10,9 @@ import mTask | MTTask Int String | MTPub Int String | MTUpd Int String + | MTEmpty instance toString MTaskMessage -safePrint :: String -> String encode :: MTaskMessage -> String decode :: String -> MTaskMessage @@ -94,6 +94,8 @@ instance assign ByteCode instance seq ByteCode instance serial ByteCode +pub :: (ByteCode a b) -> ByteCode a b + toMessages :: Int (String, BCState) -> [MTaskMessage] toByteVal :: BC -> [Char] diff --git a/mTaskInterpret.icl b/mTaskInterpret.icl index 760281c..51771a3 100644 --- a/mTaskInterpret.icl +++ b/mTaskInterpret.icl @@ -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 diff --git a/miTask.icl b/miTask.icl index f8fa8ba..ab37901 100644 --- a/miTask.icl +++ b/miTask.icl @@ -3,6 +3,7 @@ module miTask import StdDebug, StdMisc from Text import class Text(concat,join,split), instance Text String +from Control.Monad import mapM import iTasks import mTask @@ -10,38 +11,36 @@ import mTask derive class iTask MTaskMessage Start :: *World -> *World -Start world = startEngine (withShared ([], False, [], False) mTaskTask) world +Start world = startEngine ( + withShared ([], False, [], False) (\ch-> + enterInformation "Port Number?" [] >>= \port->mTaskTask port ch + )) world //Start world = startEngine mTaskTask world -mTaskTask :: (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () -mTaskTask ch = - syncNetworkChannel "localhost" 8124 "\n" decode encode ch ||- +mTaskTask :: Int (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () +mTaskTask port ch = + syncNetworkChannel "localhost" port "\n" decode encode ch ||- ( - sendMsg (hd msgs) ch >>= \_-> - sendMsg (hd (tl msgs)) ch >>= \_-> + sendMsg msgs ch >>= \_-> viewSharedInformation "channels" [ViewWith lens] ch @! () - ) + ) >>* [OnAction ActionFinish (always shutDown)] where lens :: ([MTaskMessage],Bool,[MTaskMessage],Bool) -> ([String], [String]) - lens (r,_,s,_) = (map toString r, map toString s) + lens (r,_,s,_) = (f r, f s) + where + f [] = [] + f [MTEmpty:xs] = f xs + f [x:xs] = [toString x:f xs] msgs = toMessages 500 (toRealByteCode (unMain bc)) - bc :: Main (ByteCode Int Expr) - bc = sds \x=0 In {main = x =. x +. lit 1} + bc :: Main (ByteCode Int Stmt) + bc = sds \x=0 In {main = x =. x +. lit 1 :. pub x} -makemTask :: Int (Main (ByteCode a Expr)) -> String -makemTask to bc -# (bc, st) = toRealByteCode (unMain bc) -= "t" +++ toString (toChar (to / 265)) - +++ toString (toChar (to rem 265)) +++ toString bc +++ "\n" +sendMsg :: [MTaskMessage] (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () +sendMsg m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! () -sendMsg :: MTaskMessage (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () -sendMsg m ch -| not (trace_tn ("\'" +++ toString m +++ "\'")) = undef -= upd (\(r,rs,s,ss)->(r,rs,s ++ [m],ss)) ch @! () - -syncNetworkChannel :: String Int String (String -> m) (m -> String) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m +syncNetworkChannel :: String Int String (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n syncNetworkChannel server port msgSeparator decodeFun encodeFun channel = tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! () where @@ -59,17 +58,17 @@ syncNetworkChannel server port msgSeparator decodeFun encodeFun channel onDisconnect l (received,receiveStopped,send,sendStopped) = (Ok l,Just (received,True,send,sendStopped)) -consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m -consumeNetworkStream processTask channel - = ((watch channel >>* [OnValue (ifValue ifProcess process)]) >| if (isEmpty received) (return ()) (processTask received) - @! receiveStopped - - empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool) - empty (_,rs,s,ss) = ([],rs,s,ss) +//consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n +//consumeNetworkStream processTask channel +// = ((watch channel >>* [OnValue (ifValue ifProcess process)]) >| if (isEmpty received) (return ()) (processTask received) +// @! receiveStopped +// +// empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool) +// empty (_,rs,s,ss) = ([],rs,s,ss) -- 2.20.1