Rpc.hs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. module Gidl.Backend.Rpc (
  2. rpcBackend
  3. ) where
  4. import qualified Paths_gidl as P
  5. import Gidl.Backend.Cabal (cabalFileArtifact,defaultCabalFile,filePathToPackage)
  6. import Gidl.Backend.Haskell.Interface (interfaceModule,ifModuleName)
  7. import Gidl.Backend.Haskell.Types
  8. (typeModule,isUserDefined,typeModuleName,userTypeModuleName
  9. ,importType,importDecl)
  10. import Gidl.Interface
  11. (Interface(..),InterfaceEnv(..),MethodName,Method(..),Perm(..)
  12. ,interfaceMethods)
  13. import Gidl.Schema
  14. (Schema(..),producerSchema,consumerSchema,Message(..)
  15. ,consumerMessages)
  16. import Gidl.Types (Type,TypeEnv(..))
  17. import Data.Char (isSpace)
  18. import Data.List (nub)
  19. import Ivory.Artifact
  20. (Artifact,artifactPath,artifactFileName,artifactPath,artifactText
  21. ,artifactCabalFile)
  22. import Ivory.Artifact.Template (artifactCabalFileTemplate)
  23. import Text.PrettyPrint.Mainland
  24. (Doc,prettyLazyText,text,empty,(<+>),(</>),(<>),char,line,parens
  25. ,punctuate,stack,tuple,dot,spread,cat,hang,nest,align,comma
  26. ,braces,brackets,dquotes)
  27. -- External Interface ----------------------------------------------------------
  28. rpcBackend :: TypeEnv -> InterfaceEnv -> String -> String -> [Artifact]
  29. rpcBackend (TypeEnv te) (InterfaceEnv ie) pkgName nsStr =
  30. cabalFileArtifact (defaultCabalFile pkgName modules buildDeps)
  31. : artifactCabalFile P.getDataDir "support/rpc/Makefile"
  32. : map (artifactPath "src") sourceMods
  33. where
  34. namespace = strToNs nsStr
  35. buildDeps = [ "cereal", "QuickCheck", "snap-core", "snap-server", "stm"
  36. , "aeson", "transformers" ]
  37. modules = [ filePathToPackage (artifactFileName m) | m <- sourceMods ]
  38. sourceMods = tmods ++ imods ++ [rpcBaseModule namespace]
  39. tmods = [ typeModule True (namespace ++ ["Types"]) t
  40. | (_tn, t) <- te
  41. , isUserDefined t
  42. ]
  43. imods = concat [ [ interfaceModule True (namespace ++ ["Interface"]) i
  44. , rpcModule namespace i ]
  45. | (_iname, i) <- ie
  46. ]
  47. rpcBaseModule :: [String] -> Artifact
  48. rpcBaseModule ns =
  49. artifactPath (foldr (\ p rest -> p ++ "/" ++ rest) "Rpc" ns) $
  50. artifactCabalFileTemplate P.getDataDir "support/rpc/Base.hs.template" env
  51. where
  52. env = [ ("module_path", foldr (\p rest -> p ++ "." ++ rest) "Rpc" ns) ]
  53. -- Utilities -------------------------------------------------------------------
  54. strToNs :: String -> [String]
  55. strToNs str =
  56. case break (== '.') (dropWhile isSpace str) of
  57. (a,'.' : b) | null a -> strToNs b
  58. | otherwise -> trim a : strToNs b
  59. (a,_) | null a -> []
  60. | otherwise -> [trim a]
  61. where
  62. trim = takeWhile (not . isSpace)
  63. allMethods :: Interface -> [(MethodName,Method)]
  64. allMethods (Interface _ ps ms) = concatMap allMethods ps ++ ms
  65. isEmptySchema :: Schema -> Bool
  66. isEmptySchema (Schema _ ms) = null ms
  67. -- Server Generation -----------------------------------------------------------
  68. rpcModule :: [String] -> Interface -> Artifact
  69. rpcModule ns iface =
  70. artifactPath (foldr (\ p rest -> p ++ "/" ++ rest) "Rpc" ns) $
  71. artifactText (ifaceMod ++ ".hs") $
  72. prettyLazyText 80 $
  73. genServer ns iface ifaceMod
  74. where
  75. ifaceMod = ifModuleName iface
  76. genServer :: [String] -> Interface -> String -> Doc
  77. genServer ns iface ifaceMod = stack $
  78. [ text "{-# LANGUAGE RecordWildCards #-}" | useManager ] ++
  79. [ text "{-# LANGUAGE OverloadedStrings #-}"
  80. , moduleHeader ns ifaceMod
  81. , line
  82. , importTypes ns iface
  83. , importInterface ns ifaceMod
  84. , line
  85. , text "import" <+> (ppModName (ns ++ ["Rpc","Base"]))
  86. , line
  87. , webServerImports hasConsumer
  88. , line
  89. , line
  90. , managerDefs
  91. , runServer hasConsumer useManager iface input output
  92. ]
  93. where
  94. hasConsumer = not (isEmptySchema (consumerSchema iface))
  95. (useManager,managerDefs) = managerDef hasConsumer iface input
  96. (input,output) = queueTypes iface
  97. moduleHeader :: [String] -> String -> Doc
  98. moduleHeader ns m =
  99. spread [ text "module"
  100. , dots (map text (ns ++ ["Rpc", m]))
  101. , tuple [ text "rpcServer", text "Config(..)" ]
  102. , text "where"
  103. ]
  104. -- | Import the type modules required by the interface. Import hiding
  105. -- everything, as we just need the ToJSON/FromJSON instances.
  106. importTypes :: [String] -> Interface -> Doc
  107. importTypes ns iface = stack
  108. $ map (streamImport . importType) streams
  109. ++ map (typeImport . importType) types
  110. where
  111. (streams,types) = partitionTypes iface
  112. streamImport ty = importDecl addNs ty
  113. typeImport ty = importDecl addNs ty <+> text "()"
  114. prefix = dots (map text (ns ++ ["Types"]))
  115. addNs m = prefix <> char '.' <> text m
  116. -- | Separate the types that are used from a stream method, from those used
  117. -- in attribute methods.
  118. partitionTypes :: Interface -> ([Type],[Type])
  119. partitionTypes iface = go [] [] (interfaceMethods iface)
  120. where
  121. go s a [] = (nub s, nub a)
  122. go s a ((_,StreamMethod _ ty):rest) = go (ty:s) a rest
  123. go s a ((_,AttrMethod _ ty):rest) = go s (ty:a) rest
  124. importInterface :: [String] -> String -> Doc
  125. importInterface ns ifaceName =
  126. text "import" <+> (dots (map text (ns ++ ["Interface", ifaceName])))
  127. webServerImports :: Bool -> Doc
  128. webServerImports hasConsumer = stack $
  129. [ text "import Control.Monad (msum)" | hasConsumer ] ++
  130. [ text "import Data.Aeson (decode)" | hasConsumer ] ++
  131. [ text "import qualified Snap.Core as Snap"
  132. , text "import Control.Concurrent (forkIO)"
  133. , text "import Control.Concurrent.STM"
  134. , text "import Control.Monad (forever)"
  135. , text "import Control.Monad.IO.Class (liftIO)"
  136. , text "import Data.Aeson (encode)"
  137. ]
  138. type InputQueue = Doc
  139. type OutputQueue = Doc
  140. queueTypes :: Interface -> (InputQueue,OutputQueue)
  141. queueTypes iface = (input,output)
  142. where
  143. Schema prodName _ = producerSchema iface
  144. Schema consName _ = consumerSchema iface
  145. prod = ifModuleName iface ++ prodName
  146. cons = ifModuleName iface ++ consName
  147. input = text "TQueue" <+> text prod
  148. output = text "TQueue" <+> text cons
  149. runServer :: Bool -> Bool -> Interface -> InputQueue -> OutputQueue -> Doc
  150. runServer hasConsumer useMgr iface input output =
  151. runServerSig hasConsumer input output </>
  152. runServerDef hasConsumer useMgr iface
  153. runServerSig :: Bool -> InputQueue -> OutputQueue -> Doc
  154. runServerSig hasConsumer input output =
  155. text "rpcServer ::" <+> hang 2 (arrow tys)
  156. where
  157. tys = [ input ] ++
  158. [ output | hasConsumer ] ++
  159. [ text "Config", text "IO ()" ]
  160. -- | Generate a definition for the server.
  161. runServerDef :: Bool -> Bool -> Interface -> Doc
  162. runServerDef hasConsumer useMgr iface =
  163. hang 2 (text "rpcServer" <+> body)
  164. where
  165. args = spread $
  166. [ text "input" ] ++
  167. [ text "output" | hasConsumer ] ++
  168. [ text "cfg" ]
  169. body = args <+> char '=' </> nest 2 (doStmts stmts)
  170. stmts = [ text "state <- mkState" | useMgr ]
  171. ++ [ defInput ]
  172. ++ [ spread $ [ text "_ <- forkIO (manager state input" ]
  173. ++ [ text "input'" | hasConsumer ]
  174. ++ [ text ")" ] | useMgr ]
  175. ++ [ text "conn <- newConn output" <+> input' | hasConsumer ]
  176. ++ [ text "runServer cfg $ Snap.route" </> routesDef ]
  177. (input',defInput)
  178. | hasConsumer && useMgr = (text "input'", text "input' <- newTQueueIO")
  179. | otherwise = (text "input", empty)
  180. routesDef = nest 2 (align (routes iface (text "state")))
  181. -- | Define one route for each interface member
  182. routes :: Interface -> Doc -> Doc
  183. routes iface state =
  184. align (char '[' <> nest 1 (stack (commas handlers)) <> char ']')
  185. where
  186. Interface pfx _ _ = iface
  187. Schema suffix _ = consumerSchema iface
  188. handlers = map (mkRoute pfx suffix state) (allMethods iface)
  189. mkRoute :: String -> String -> Doc -> (MethodName,Method) -> Doc
  190. mkRoute ifacePfx consSuffix state method@(name,mty) =
  191. parens (url <> comma </> guardMethods (handlersFor mty))
  192. where
  193. url = dquotes (text ifacePfx <> char '/' <> text name)
  194. guardMethods [h] = h
  195. guardMethods hs = nest 2 $ text "msum"
  196. </> brackets (stack (commas hs))
  197. handlersFor StreamMethod {} =
  198. [ readStream state name ]
  199. handlersFor (AttrMethod Read _) =
  200. [ readAttr consSuffix m | m <- consumerMessages method ]
  201. handlersFor (AttrMethod Write _) =
  202. [ writeAttr consSuffix m | m <- consumerMessages method ]
  203. handlersFor (AttrMethod ReadWrite ty) =
  204. [ readAttr consSuffix m | m <- consumerMessages (name,AttrMethod Read ty) ] ++
  205. [ writeAttr consSuffix m | m <- consumerMessages (name,AttrMethod Write ty) ]
  206. readStream :: Doc -> MethodName -> Doc
  207. readStream state name = nest 2 $ text "Snap.method Snap.GET $"
  208. </> doStmts
  209. [ text "x <- liftIO (atomically (readTSampleVar" <+> svar <> text "))"
  210. , text "Snap.writeLBS (encode x)"
  211. ]
  212. where
  213. svar = parens (fieldName name <+> state)
  214. constrName :: String -> Message -> String
  215. constrName suffix (Message n _) = userTypeModuleName n ++ suffix
  216. readAttr :: String -> Message -> Doc
  217. readAttr suffix msg = text "Snap.method Snap.GET $" <+> doStmts
  218. [ text "resp <- liftIO $ sendRequest conn $" <+>
  219. text (constrName suffix msg) <+> text "()"
  220. , text "Snap.writeLBS (encode resp)"
  221. ]
  222. writeAttr :: String -> Message -> Doc
  223. writeAttr suffix msg = text "Snap.method Snap.POST $" <+> doStmts
  224. [ text "bytes <- Snap.readRequestBody 32768"
  225. , text "case decode bytes of" </>
  226. text "Just req -> liftIO $ sendRequest_ conn $" <+>
  227. text con <+> text "req" </>
  228. text "Nothing -> Snap.modifyResponse $ Snap.setResponseCode 400"
  229. ]
  230. where
  231. con = constrName suffix msg
  232. -- The stream manager ----------------------------------------------------------
  233. -- | Define everything associated with the manager, but only if there are stream
  234. -- values to manage.
  235. managerDef :: Bool -> Interface -> InputQueue -> (Bool,Doc)
  236. managerDef hasConsumer iface input
  237. | null streams = (False,empty)
  238. | otherwise = (True,stack defs </> empty)
  239. where
  240. streams = [ (name,ty) | (name,StreamMethod _ ty) <- allMethods iface ]
  241. (stateType,stateDecl) = stateDef streams
  242. defs = [ stateDecl
  243. , empty
  244. , mkStateDef streams
  245. , empty
  246. , text "manager ::" <+> arrow ([ stateType, input ] ++
  247. [ input | hasConsumer ] ++
  248. [ text "IO ()" ])
  249. , nest 2 $ spread $
  250. [ text "manager state input" ] ++
  251. [ text "filtered" | hasConsumer ] ++
  252. [ text "= forever $" </> doStmts stmts ]
  253. ]
  254. stmts = [ text "msg <- atomically (readTQueue input)"
  255. , nest 2 (text "case msg of" </>
  256. stack (map mkCase streams ++ [ defCase | hasConsumer ])) ]
  257. -- name the producer constructor for a stream element
  258. Schema prodSuffix _ = producerSchema iface
  259. prodName ty = text (typeModuleName ty ++ prodSuffix)
  260. -- update the state for this stream element
  261. mkCase (n,ty) = prodName ty <+> text "x -> atomically (writeTSampleVar"
  262. <+> parens (fieldName n <+> text "state")
  263. <+> text "x)"
  264. defCase = text "notStream -> atomically (writeTQueue filtered notStream)"
  265. -- | Generate the data type used to hold the streaming values, or nothing if
  266. -- there aren't any present in the interface.
  267. stateDef :: [(MethodName,Type)] -> (Doc,Doc)
  268. stateDef streams = (text "State",def)
  269. where
  270. def = nest 2 (text "data State = State" <+> braces fields)
  271. fields = align (stack (punctuate comma (map mkField streams)))
  272. mkField (name,ty) =
  273. fieldName name
  274. <+> text "::"
  275. <+> text "TSampleVar"
  276. <+> text (typeModuleName ty)
  277. mkStateDef :: [(MethodName,Type)] -> Doc
  278. mkStateDef streams = stack
  279. [ text "mkState :: IO State"
  280. , nest 2 (text "mkState =" </> nest 3 (doStmts stmts))
  281. ]
  282. where
  283. stmts = [ fieldName n <+> text "<- newTSampleVarIO" | (n,_) <- streams ]
  284. ++ [ text "return State { .. }" ]
  285. -- | Given the name of a stream in the interface, produce the selector for the
  286. -- state data type.
  287. fieldName :: MethodName -> Doc
  288. fieldName name = text "stream_" <> text name
  289. -- Pretty-printing Helpers -----------------------------------------------------
  290. arrow :: [Doc] -> Doc
  291. arrow ts = spread (punctuate (text "->") ts)
  292. commas :: [Doc] -> [Doc]
  293. commas = punctuate comma
  294. dots :: [Doc] -> Doc
  295. dots = cat . punctuate dot
  296. ppModName :: [String] -> Doc
  297. ppModName = dots . map text
  298. doStmts :: [Doc] -> Doc
  299. doStmts [d] = nest 2 d
  300. doStmts ds = text "do" <+> align (stack (map (nest 2) ds))