shrub/pkg/hs-urbit/lib/Arvo/Event.hs

296 lines
6.8 KiB
Haskell
Raw Normal View History

module Arvo.Event where
import UrbitPrelude hiding (Term)
2019-08-02 08:07:20 +03:00
import Arvo.Common (KingId(..))
import Arvo.Common (NounMap, NounSet)
import Arvo.Common (Desk, Mime)
import Arvo.Common (HttpEvent, Header(..))
import Arvo.Common (Ipv4, Ipv6, Port, Turf, AmesDest)
import Arvo.Common (ReOrg(..), reorgThroughNoun)
import qualified Network.HTTP.Types.Method as H
-- Misc Types ------------------------------------------------------------------
type Pass = Atom -- Public Key
type Life = Word -- Number of Azimoth key revs.
type Bloq = Atom -- TODO
type Ring = Atom -- Private Key
type Oath = Atom -- Signature
-- Parsed URLs -----------------------------------------------------------------
type Host = Either Turf Ipv4
2019-07-21 07:36:25 +03:00
type Hart = (Bool, Maybe Atom, Host)
2019-07-22 21:10:27 +03:00
type Pork = (Maybe Knot, [Cord])
type Quay = [(Cord, Cord)]
2019-07-21 07:36:25 +03:00
data PUrl = PUrl Hart Pork Quay
2019-07-21 07:36:25 +03:00
deriving (Eq, Ord, Show)
deriveNoun ''PUrl
-- Dawn Records ----------------------------------------------------------------
data Seed = Seed Ship Life Ring (Maybe Oath)
deriving (Eq, Ord, Show)
type Public = (Life, NounMap Life Pass)
data Dnses = Dnses { dPri::Cord, dSec::Cord, dTer::Cord }
deriving (Eq, Ord, Show)
type EthAddr = Bytes -- 20 bytes
type ContNum = Word
data EthPoint = EthPoint
{ epOwn :: (EthAddr, EthAddr, EthAddr, EthAddr)
, epNet :: Maybe (Life, Pass, ContNum, (Bool, Ship), Maybe Ship)
, epKid :: Maybe (EthAddr, NounSet Ship)
}
deriving (Eq, Ord, Show)
data EthEventId = EthEventId
{ eeiBlock :: Atom
, eeiLog :: Atom
}
deriving (Eq, Ord, Show)
data EthBookmark = EthBookmark
{ ebHeard :: NounSet EthEventId
, ebLatestBlock :: Atom
}
deriving (Eq, Ord, Show)
data Snap = Snap (NounMap Ship Public)
(Dnses, NounMap Ship EthPoint)
EthBookmark
deriving (Eq, Ord, Show)
data Dawn = MkDawn
{ dSeed :: Seed
, dShip :: Ship
2019-07-23 03:46:06 +03:00
, dCzar :: NounMap Ship (Life, Pass)
, dTurf :: [Turf]
, dBloq :: Bloq
, dNode :: (Maybe PUrl)
, dSnap :: (Maybe Snap)
}
deriving (Eq, Ord, Show)
deriveNoun ''EthEventId
deriveNoun ''EthBookmark
deriveNoun ''Dnses
deriveNoun ''EthPoint
deriveNoun ''Snap
2019-07-21 07:36:25 +03:00
deriveNoun ''Seed
deriveNoun ''Dawn
2019-07-21 07:36:25 +03:00
-- HTTP ------------------------------------------------------------------------
2019-07-21 07:36:25 +03:00
type ServerId = Atom
data Address
= AIpv4 Ipv4
| AIpv6 Ipv6
| AAmes Ship
deriving (Eq, Ord, Show)
data HttpRequest = HttpRequest
{ reqMeth :: H.StdMethod
2019-07-22 21:10:27 +03:00
, reqUrl :: Cord
, reqHead :: [Header]
, reqBody :: Maybe File
}
deriving (Eq, Ord, Show)
2019-07-23 03:46:06 +03:00
data HttpServerReq = HttpServerReq
{ hsrSecure :: Bool
, hsrAddress :: Address
, hsrRequest :: HttpRequest
}
deriving (Eq, Ord, Show)
data HttpClientEv
2019-08-02 08:07:20 +03:00
= HttpClientEvReceive (KingId, ()) ServerId HttpEvent
| HttpClientEvBorn (KingId, ()) ()
| HttpClientEvCrud Path Cord Tang
deriving (Eq, Ord, Show)
data HttpServerEv
2019-08-02 08:07:20 +03:00
= HttpServerEvRequest (KingId, Word, Word, ()) HttpServerReq
| HttpServerEvRequestLocal (KingId, Path) HttpServerReq
| HttpServerEvLive (KingId, ()) Port (Maybe Port)
| HttpServerEvBorn (KingId, ()) ()
deriving (Eq, Ord, Show)
2019-07-21 07:36:25 +03:00
deriveNoun ''Address
deriveNoun ''HttpClientEv
2019-07-23 03:46:06 +03:00
deriveNoun ''HttpRequest
deriveNoun ''HttpServerEv
2019-07-23 03:46:06 +03:00
deriveNoun ''HttpServerReq
2019-07-21 07:36:25 +03:00
-- Ames ------------------------------------------------------------------------
data AmesEv
= AmesEvHear () AmesDest Bytes
| AmesEvWake () ()
| AmesEvWant Path Ship Path Noun
| AmesEvCrud Path Cord Tang
deriving (Eq, Ord, Show)
deriveNoun ''AmesEv
2019-07-21 07:36:25 +03:00
-- Arvo Events -----------------------------------------------------------------
data ArvoEv
= ArvoEvWhom () Ship
| ArvoEvWack () Word512
deriving (Eq, Ord, Show)
deriveNoun ''ArvoEv
2019-07-21 07:36:25 +03:00
-- Boat Events -----------------------------------------------------------------
data BoatEv
= BoatEvBoat () ()
| BoatEvVoid Void
2019-07-21 07:36:25 +03:00
deriving (Eq, Ord, Show)
deriveNoun ''BoatEv
2019-07-21 07:36:25 +03:00
-- Timer Events ----------------------------------------------------------------
data BehnEv
= BehnEvWake () ()
2019-08-01 03:27:13 +03:00
| BehnEvBorn (Atom, ()) ()
deriving (Eq, Ord, Show)
deriveNoun ''BehnEv
2019-07-21 07:36:25 +03:00
-- Newt Events -----------------------------------------------------------------
data NewtEv
= NewtEvBarn (Atom, ()) ()
| NewtEvBorn Void
deriving (Eq, Ord, Show)
deriveNoun ''NewtEv
2019-07-21 07:36:25 +03:00
-- FileSystem Events -----------------------------------------------------------
data SyncEv
= SyncEvInto (Nullable (Atom, ())) Desk Bool [(Path, Maybe Mime)]
| SyncEvCrud Path Cord Tang
deriving (Eq, Ord, Show)
deriveNoun ''SyncEv
2019-07-21 07:36:25 +03:00
-- Terminal Events -------------------------------------------------------------
data LegacyBootEvent
= Fake Ship
| Dawn Dawn
deriving (Eq, Ord, Show)
data ArrowKey = D | L | R | U
deriving (Eq, Ord, Show)
data Belt
= Aro ArrowKey
| Bac ()
| Ctl Cord
| Del ()
| Met Cord
| Ret ()
2019-07-21 07:36:25 +03:00
| Txt Tour
deriving (Eq, Ord, Show)
data TermEv
= TermEvBelt (Atom, ()) Belt
| TermEvBlew (Atom, ()) Word Word
| TermEvBoot (Atom, ()) LegacyBootEvent
| TermEvHail (Atom, ()) ()
| TermEvBorn Void
deriving (Eq, Ord, Show)
2019-07-21 07:36:25 +03:00
deriveNoun ''LegacyBootEvent
deriveNoun ''ArrowKey
deriveNoun ''Belt
deriveNoun ''TermEv
2019-07-21 07:36:25 +03:00
-- Events for Device Drivers ---------------------------------------------------
data BlipEv
= BlipEvAmes AmesEv
| BlipEvArvo ArvoEv
| BlipEvBehn BehnEv
| BlipEvBoat BoatEv
| BlipEvHttpClient HttpClientEv
| BlipEvHttpServer HttpServerEv
| BlipEvNewt NewtEv
| BlipEvSync SyncEv
| BlipEvTerm TermEv
deriving (Eq, Ord, Show)
deriveNoun ''BlipEv
2019-07-21 07:36:25 +03:00
-- Boot Events -----------------------------------------------------------------
data Vane
= VaneVane VaneEv
| VaneZuse ZuseEv
2019-07-21 07:36:25 +03:00
deriving (Eq, Ord, Show)
data VaneName
= Ames | Behn | Clay | Dill | Eyre | Ford | Gall | Iris | Jael
deriving (Eq, Ord, Show, Enum, Bounded)
data ZuseEv
= ZEVeer () Cord Path BigTape
| ZEVoid Void
deriving (Eq, Ord, Show)
data VaneEv
= VEVeer (VaneName, ()) Cord Path BigTape
| VEVoid Void
deriving (Eq, Ord, Show)
deriveNoun ''Vane
2019-07-21 07:36:25 +03:00
deriveNoun ''VaneName
deriveNoun ''VaneEv
deriveNoun ''ZuseEv
2019-07-21 07:36:25 +03:00
-- The Main Event Type ---------------------------------------------------------
2019-07-21 07:36:25 +03:00
data Ev
= EvBlip BlipEv
| EvVane Vane
2019-07-21 07:36:25 +03:00
deriving (Eq, Ord, Show)
instance ToNoun Ev where
toNoun = \case
EvBlip v -> toNoun $ reorgThroughNoun (Cord "", v)
EvVane v -> toNoun $ reorgThroughNoun (Cord "vane", v)
instance FromNoun Ev where
parseNoun = parseNoun >=> \case
ReOrg "" s t p v -> fmap EvBlip $ parseNoun $ toNoun (s,t,p,v)
ReOrg "vane" s t p v -> fmap EvVane $ parseNoun $ toNoun (s,t,p,v)
ReOrg _ _ _ _ _ -> fail "First path-elem must be ?($ %vane)"