IWorld.dcl 8 KB
Newer Older
1
definition module iTasks._Framework.IWorld
2

3
from System.FilePath		import :: FilePath
4 5
from Data.Map				import :: Map
from Data.Maybe				import :: Maybe
6
from Data.Error 			import :: MaybeError(..), :: MaybeErrorString(..)
7 8 9 10
from Data.Set               import :: Set
from StdFile			                import class FileSystem		
from System.Time				        import :: Timestamp
from Text.JSON				            import :: JSONNode
11
from iTasks.API.Core.Types		        import :: Date, :: Time, :: DateTime, :: Config, :: InstanceNo, :: TaskNo, :: TaskId, :: TaskListItem, :: ParallelTaskType, :: TaskTime, :: SessionId
12
from iTasks.UI.Definition				import :: UI, :: UINodeType
13
from iTasks._Framework.TaskState		import :: ParallelTaskState, :: TIMeta, :: DeferredJSON
14
from iTasks._Framework.Task             import :: TaskValue, :: ConnectionTask, :: BackgroundTask, :: Event
15 16
from iTasks._Framework.SDS import :: SDSNotifyRequest, :: BasicShareId
from iTasks._Framework.SDS import :: RWShared, :: ReadWriteShared, :: Shared, :: JSONShared
17

18 19
from Sapl.Linker.LazyLinker import :: LoaderState
from Sapl.Linker.SaplLinkerShared import :: LineType, :: FuncTypeMap
20
from Sapl.Target.Flavour import :: Flavour
21
from Sapl.SaplParser import :: ParserState
22
from TCPIP import :: TCP_Listener, :: TCP_Listener_, :: TCP_RChannel_, :: TCP_SChannel_, :: TCP_DuplexChannel, :: DuplexChannel, :: IPAddress, :: ByteSeq
23

24 25
CLEAN_HOME_VAR	:== "CLEAN_HOME"

26 27
:: *IWorld		=	{ server                :: !ServerInfo                              // Static server info, initialized at startup
					, config				:: !Config									// Server configuration
28
                    , clocks                :: !SystemClocks                            // Server side clocks
29 30 31
                    , current               :: !TaskEvalState                           // Shared state during task evaluation

                    , random                :: [Int]                                    // Infinite random stream
32

33
                    , sdsNotifyRequests     :: ![SDSNotifyRequest]                      // Notification requests from previously read sds's
34 35
                    , memoryShares          :: !Map (String,String) Dynamic             // Run-time memory shares
                    , cachedShares          :: !ShareCache                              // Cached json file shares
36
					, exposedShares			:: !Map String (Dynamic, JSONShared)        // Shared source
37
					, jsCompilerState 		:: !Maybe JSCompilerState 					// Sapl to Javascript compiler state
38

39
	                , ioTasks               :: !*IOTasks                                // The low-level input/output tasks
40
                    , ioStates              :: !IOStates                                // Results of low-level io tasks, indexed by the high-level taskid that it is linked to
Bas Lijnse's avatar
Bas Lijnse committed
41

42
					, world					:: !*World									// The outside world
43 44

                    //Experimental database connection cache
45
                    , resources             :: !*(Maybe *Resource)
46
                    , onClient				:: !Bool									// "False" on the server, "True" on the client
47
					, shutdown				:: !Bool									// Flag that signals the server function to shut down
48
					}
49

50 51 52 53 54 55 56 57 58
:: ServerInfo =
    { serverName      :: !String				// The name of the server application
	, serverURL		  :: !String				// URL of the server like "//any.com:80"
	, buildID		  :: !String				// The date/time identifier of the server's build
    , paths           :: !SystemPaths           // Filesystem paths that are used by iTasks
    }

:: SystemPaths =
    { appDirectory			:: !FilePath		// Location of the application's executable
59
	, dataDirectory			:: !FilePath		// Location of the application's data files
60
    , publicWebDirectories  :: ![FilePath]      // List of directories that contain files that are served publicly by the iTask webserver
61 62
	, saplDirectory 		:: !FilePath 		// Location of the application's sapl files
	, saplFlavourFile 		:: !FilePath  		// Location of the sapl flavour file
63 64
    }

65
:: SystemClocks =
66 67
    { timestamp 			:: !Timestamp
	, localDate             :: !Date
68 69 70 71 72
    , localTime             :: !Time
    , utcDate               :: !Date
    , utcTime               :: !Time
    }

73 74
:: ShareCache :== Map (String,String) (Dynamic,Bool,Maybe DeferredJSON)

75 76 77 78 79 80 81 82
:: JSCompilerState =
	{ loaderState 			:: !LoaderState							// State of the lazy loader
	, functionMap 			:: !FuncTypeMap 						// Function name -> source code mapping
 	, flavour 				:: !Flavour 							// Clean flavour for JS compilation
	, parserState 			:: !Maybe ParserState 					// Some information collected by the parser for the code generator
	, skipMap 				:: !Map InstanceNo (Set String) 		// Per client information of the names of the already generated functions
	}

83
:: TaskEvalState =
84 85 86 87 88
    { taskTime				 :: !TaskTime							// The 'virtual' time for the task. Increments at every event
	, taskInstance		     :: !InstanceNo							// The current evaluated task instance
    , sessionInstance        :: !Maybe InstanceNo                   // If we are evaluating a task in response to an event from a session
    , attachmentChain        :: ![TaskId]                           // The current way the evaluated task instance is attached to other instances
    , nextTaskNo			 :: !TaskNo								// The next task number to assign
89 90
    }

91 92 93
:: *IOTasks =
    { done :: !*[IOTaskInstance]
    , todo :: !*[IOTaskInstance]
94 95
    }

96
:: *IOTaskInstance
97 98
    = ListenerInstance !ListenerInstanceOpts !*TCP_Listener
    | ConnectionInstance !ConnectionInstanceOpts !*TCP_DuplexChannel
99
    | BackgroundInstance !BackgroundInstanceOpts !BackgroundTask
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
:: ListenerInstanceOpts =
    { taskId                :: !TaskId          //Reference to the task that created the listener
    , nextConnectionId      :: !ConnectionId    
    , port                  :: !Int
    , connectionTask        :: !ConnectionTask
    , removeOnClose         :: !Bool            //If this flag is set, states of connections accepted by this listener are removed when the connection is closed
    }

:: ConnectionInstanceOpts =
    { taskId                :: !TaskId          //Reference to the task that created the connection
    , connectionId          :: !ConnectionId    //Unique connection id (per listener/outgoing connection)
    , remoteHost            :: !IPAddress      
    , connectionTask        :: !ConnectionTask  //The io task definition that defines how the connection is handled
    , removeOnClose         :: !Bool            //If this flag is set, the connection state is removed when the connection is closed
    }

:: ConnectionId             :== Int

119 120 121 122 123 124 125
:: BackgroundInstanceOpts =
    { bgInstId              :: !BackgroundTaskId
    }

:: BackgroundTaskId         :== Int


126 127 128 129 130 131 132 133 134
:: IOStates :== Map TaskId IOState
:: IOState
    = IOActive      !(Map ConnectionId (!Dynamic,!Bool))
    | IODestroyed   !(Map ConnectionId (!Dynamic,!Bool))
    | IOException   !String
:: IOConnectionState =
    { connectionTaskState   :: !Dynamic //The persisted local state of the connection task that handles the connection
    , closed                :: !Bool
    }
Bas Lijnse's avatar
Bas Lijnse committed
135

136 137
:: *Resource = Resource | .. //Extensible resource type for caching database connections etc...

138 139 140 141 142 143 144 145
//Creation and destruction of the iworld
/**
* Creates and initializes the IWorld state
*
* @param The application's name
* @param The path where the iTasks SDK can be found (optional)
* @param Additional paths where static web assets can be found (optional)
* @param The path where the iTasks data store is located (optional)
146
* @param Path to where the applications's SAPL files are stored (optional)
147 148 149 150
* @param The world
*
* @return An initialized iworld
*/
151
createIWorld :: !String !(Maybe FilePath) !(Maybe [FilePath]) !(Maybe FilePath) !(Maybe FilePath) !*World -> *IWorld
152 153 154

/**
* Initialize the SAPL->JS compiler state
155
* 
156
*/
157
initJSCompilerState :: *IWorld -> *(!MaybeErrorString (), !*IWorld)
158 159 160 161 162 163

/**
* Destroys the iworld state
*/
destroyIWorld :: !*IWorld -> *World

164
//Internally used clock shares
165
iworldTimestamp :: Shared Timestamp
166 167 168 169
iworldLocalDate :: Shared Date
iworldLocalTime :: Shared Time
iworldUTCDate   :: Shared Date
iworldUTCTime   :: Shared Time
170

171
instance FileSystem IWorld