Rpc.hs 13 KB

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