+#define _BSD_SOURCE
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
void killHandler(int i)
{
- printf("%s caught, Bye...\n", strsignal(i));
+ printf("%i caught, Bye...\n", i);
exit(1);
}
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);
}
}
}
//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<argc){
+ port = atoi(argv[++opti]);
+ if(port < 1)
+ die("Port numbers are > 1\n");
+ } else {
+ usage(stderr, argv[0]);
+ exit(EXIT_FAILURE);
}
-
+ opti++;
}
open_filedescriptors();
debug("Waiting for 500ms\n");
usleep(500000);
debug("done waiting\n");
+ write(fd, "\n", 1);
}
return 0;
}
#include "misc.h"
#include "sds.h"
-struct sds sdss[MAXSDSS] = {0};
+struct sds sdss[MAXSDSS];
uint8_t c;
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;
}
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;
}
}
for(cs = 0; cs<MAXSDSS; cs++){
if(sdss[cs].used && sdss[cs].id == id){
debug("Publish %d=%d\n", sdss[cs].id, sdss[cs].value);
- sprintf(msg, "s%c%c%c\n",
+ sprintf(msg, "u%c%c%c%c\n",
+ 0,
sdss[cs].id,
sdss[cs].value/265,
sdss[cs].value%265);
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
+++ " value " +++ safePrint v
toString (MTUpd i v) = "Update id: " +++ toString i
+++ " value " +++ safePrint v
+ toString MTEmpty = "Empty message"
toByteVal :: BC -> [Char]
toByteVal b
import StdDebug, StdMisc
from Text import class Text(concat,join,split), instance Text String
+from Control.Monad import mapM
import iTasks
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
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)]) <! id) @! ()
- where
- ifProcess (received,receiveStopped,_,_)
- = receiveStopped || (not (isEmpty received))
-
- process (received,receiveStopped,_,_)
- = upd empty channel
- >>| 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)]) <! id) @! ()
+// where
+// ifProcess (received,receiveStopped,_,_)
+// = receiveStopped || (not (isEmpty received))
+//
+// process (received,receiveStopped,_,_)
+// = upd empty channel
+// >>| if (isEmpty received) (return ()) (processTask received)
+// @! receiveStopped
+//
+// empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool)
+// empty (_,rs,s,ss) = ([],rs,s,ss)