bf05e0549485d2899b800a3b0048f8a1ad70fdca
[mTask.git] / Devices / mTaskTCP.icl
1 implementation module Devices.mTaskTCP
2
3 import Devices.mTaskDevice
4 import iTasks
5
6 derive class iTask TCPSettings
7
8 getmTaskTCPDevice :: Task MTaskResource
9 getmTaskTCPDevice = TCPDevice <$> enterInformation "Settings" []
10
11 instance MTaskDuplex TCPSettings where
12 synFun :: TCPSettings (Shared Channels) -> Task ()
13 synFun s channels = catchAll (
14 tcpconnect s.host s.port channels {ConnectionHandlers|
15 onConnect=onConnect,
16 whileConnected=whileConnected,
17 onDisconnect=onDisconnect} @! ())
18 (\v->traceValue v @! ())
19 where
20 onConnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
21 onConnect _ (msgs,send,sendStopped) = (Ok "", Just (msgs,[],sendStopped), map encode send, False)
22
23 whileConnected :: (Maybe String) String ([MTaskMSGRecv], [MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool), [String], Bool)
24 whileConnected Nothing acc (msgs,send,sendStopped) = (Ok acc, Nothing, [], False)
25 whileConnected (Just newData) acc (msgs,send,sendStopped) = (Ok acc, Just (msgs ++ [decode newData],[],False), map encode send, False)
26
27 onDisconnect :: String ([MTaskMSGRecv],[MTaskMSGSend],Bool) -> (MaybeErrorString String, Maybe ([MTaskMSGRecv],[MTaskMSGSend],Bool))
28 onDisconnect l (msgs,send,sendStopped) = (Ok l, Nothing)