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