2019-07-24 04:34:16 +03:00
|
|
|
module Arvo.Event where
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
import UrbitPrelude hiding (Term)
|
2019-07-21 04:29:39 +03:00
|
|
|
import Urbit.Time
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
import Arvo.Common (NounMap, NounSet)
|
|
|
|
import Arvo.Common (AtomIf, AtomIs, Desk, Lane, Mime, Turf)
|
|
|
|
import Arvo.Common (HttpEvent, HttpServerConf)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
-- Misc Types ------------------------------------------------------------------
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
type Pass = Atom -- Public Key
|
|
|
|
type Life = Word -- Number of Azimoth key revs.
|
|
|
|
type Bloq = Atom -- TODO
|
2019-07-21 07:13:21 +03:00
|
|
|
type Ring = Atom -- Private Key
|
|
|
|
type Oath = Atom -- Signature
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
|
|
|
|
-- Parsed URLs -----------------------------------------------------------------
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
type Host = Either Turf AtomIf
|
|
|
|
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
|
|
|
|
2019-07-23 00:26:40 +03:00
|
|
|
data PUrl = PUrl Hart Pork Quay
|
2019-07-21 07:36:25 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''PUrl
|
|
|
|
|
|
|
|
|
|
|
|
-- Dawn Records ----------------------------------------------------------------
|
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
data Seed = Seed Ship Life Ring (Maybe Oath)
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-23 00:26:40 +03:00
|
|
|
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)
|
|
|
|
|
2019-07-21 04:29:39 +03:00
|
|
|
data Dawn = MkDawn
|
|
|
|
{ dSeed :: Seed
|
|
|
|
, dShip :: Ship
|
2019-07-23 03:46:06 +03:00
|
|
|
, dCzar :: NounMap Ship (Life, Pass)
|
2019-07-21 04:29:39 +03:00
|
|
|
, dTurf :: [Turf]
|
|
|
|
, dBloq :: Bloq
|
2019-07-23 00:26:40 +03:00
|
|
|
, dNode :: (Maybe PUrl)
|
|
|
|
, dSnap :: (Maybe Snap)
|
2019-07-21 04:29:39 +03:00
|
|
|
}
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-23 00:26:40 +03:00
|
|
|
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 04:29:39 +03:00
|
|
|
|
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- HTTP ------------------------------------------------------------------------
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
type ServerId = Atom
|
|
|
|
|
|
|
|
data Address
|
2019-07-24 04:34:16 +03:00
|
|
|
= AIpv4 AtomIf
|
|
|
|
| AIpv6 AtomIs
|
|
|
|
| AAmes Ship
|
2019-07-21 04:29:39 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
data HttpRequest = HttpRequest
|
2019-07-22 21:10:27 +03:00
|
|
|
{ reqId :: Cord
|
|
|
|
, reqUrl :: Cord
|
2019-07-23 00:26:40 +03:00
|
|
|
, reqHead :: [(Bytes, Bytes)]
|
2019-07-24 04:34:16 +03:00
|
|
|
, reqBody :: Maybe File
|
2019-07-21 04:29:39 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data HttpClientEv
|
|
|
|
= HttpClientEvReceive (Atom, ()) ServerId HttpEvent
|
|
|
|
| HttpClientEvBorn (Atom, ()) ()
|
|
|
|
| HttpClientEvCrud Path Cord Tang
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
data HttpServerEv
|
|
|
|
= HttpServerEvRequest (Atom, Word, Word, ()) HttpServerReq
|
|
|
|
| HttpServerEvRequestLocal Path HttpServerReq
|
|
|
|
| HttpServerEvLive (Atom, ()) Atom (Maybe Word)
|
|
|
|
| HttpServerEvBorn (Atom, ()) ()
|
|
|
|
| HttpServerEvSetConfig (Atom, ()) HttpServerConf
|
2019-07-21 04:29:39 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
deriveNoun ''Address
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''HttpClientEv
|
2019-07-23 03:46:06 +03:00
|
|
|
deriveNoun ''HttpRequest
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''HttpServerEv
|
2019-07-23 03:46:06 +03:00
|
|
|
deriveNoun ''HttpServerReq
|
2019-07-21 04:29:39 +03:00
|
|
|
|
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- Ames ------------------------------------------------------------------------
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data AmesEv
|
|
|
|
= AmesEvHear () Lane Atom
|
|
|
|
| AmesEvWake () ()
|
|
|
|
| AmesEvWant Path Ship Path Noun
|
|
|
|
| AmesEvCrud Path Cord Tang
|
2019-07-21 04:29:39 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''AmesEv
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- Arvo Events -----------------------------------------------------------------
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data ArvoEv
|
|
|
|
= ArvoEvWhom () Ship
|
|
|
|
| ArvoEvWack () Word512
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''ArvoEv
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- Boat Events -----------------------------------------------------------------
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data BoatEv
|
|
|
|
= BoatEvBoat () ()
|
|
|
|
| BoatEvVoid Void
|
2019-07-21 07:36:25 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''BoatEv
|
2019-07-21 07:13:21 +03:00
|
|
|
|
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- Timer Events ----------------------------------------------------------------
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data BehnEv
|
|
|
|
= BehnEvWake () ()
|
|
|
|
| BehnEvBorn (Wen, ()) ()
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''BehnEv
|
2019-07-21 07:13:21 +03:00
|
|
|
|
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
-- Newt Events -----------------------------------------------------------------
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data NewtEv
|
|
|
|
= NewtEvBarn (Atom, ()) ()
|
|
|
|
| NewtEvBorn Void
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''NewtEv
|
2019-07-21 07:36:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- FileSystem Events -----------------------------------------------------------
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data SyncEv
|
|
|
|
= SyncEvInto (Nullable (Atom, ())) Desk Bool [(Path, Maybe Mime)]
|
|
|
|
| SyncEvCrud Path Cord Tang
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
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
|
2019-07-23 05:35:15 +03:00
|
|
|
| Bac ()
|
2019-07-23 00:26:40 +03:00
|
|
|
| Ctl Cord
|
2019-07-23 05:35:15 +03:00
|
|
|
| Del ()
|
2019-07-23 00:26:40 +03:00
|
|
|
| Met Cord
|
2019-07-23 05:35:15 +03:00
|
|
|
| Ret ()
|
2019-07-21 07:36:25 +03:00
|
|
|
| Txt Tour
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data TermEv
|
|
|
|
= TermEvBelt (Atom, ()) Belt
|
|
|
|
| TermEvBlew (Atom, ()) Word Word
|
|
|
|
| TermEvBoot (Atom, ()) LegacyBootEvent
|
|
|
|
| TermEvHail (Atom, ()) ()
|
|
|
|
| TermEvBorn Void
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
deriveNoun ''LegacyBootEvent
|
|
|
|
deriveNoun ''ArrowKey
|
|
|
|
deriveNoun ''Belt
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''TermEv
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:36:25 +03:00
|
|
|
|
|
|
|
-- Events for Device Drivers ---------------------------------------------------
|
2019-07-21 07:13:21 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data BlipEv
|
|
|
|
= BlipEvAmes AmesEv
|
|
|
|
| BlipEvArvo ArvoEv
|
|
|
|
| BlipEvBehn BehnEv
|
|
|
|
| BlipEvBoat BoatEv
|
|
|
|
| BlipEvHttpClient HttpClientEv
|
|
|
|
| BlipEvHttpServer HttpServerEv
|
|
|
|
| BlipEvNewt NewtEv
|
|
|
|
| BlipEvSync SyncEv
|
|
|
|
| BlipEvTerm TermEv
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''BlipEv
|
2019-07-21 07:36:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Boot Events -----------------------------------------------------------------
|
|
|
|
|
|
|
|
data Vane
|
2019-07-24 04:34:16 +03:00
|
|
|
= VaneVane VaneEv
|
|
|
|
| VaneZuse ZuseEv
|
2019-07-21 07:36:25 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
data VaneName
|
2019-07-21 04:29:39 +03:00
|
|
|
= Ames | Behn | Clay | Dill | Eyre | Ford | Gall | Iris | Jael
|
2019-07-21 07:13:21 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data ZuseEv
|
2019-07-21 07:13:21 +03:00
|
|
|
= ZOVeer () Cord Path BigTape
|
|
|
|
| ZOVoid Void
|
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
data VaneEv
|
2019-07-21 07:13:21 +03:00
|
|
|
= VOVeer (VaneName, ()) Cord Path BigTape
|
|
|
|
| VOVoid Void
|
|
|
|
deriving (Eq, Ord, Show)
|
2019-07-21 04:29:39 +03:00
|
|
|
|
2019-07-21 07:13:21 +03:00
|
|
|
deriveNoun ''Vane
|
2019-07-21 07:36:25 +03:00
|
|
|
deriveNoun ''VaneName
|
2019-07-24 04:34:16 +03:00
|
|
|
deriveNoun ''VaneEv
|
|
|
|
deriveNoun ''ZuseEv
|
2019-07-21 07:36:25 +03:00
|
|
|
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
-- The Main Event Type ---------------------------------------------------------
|
2019-07-21 07:36:25 +03:00
|
|
|
|
|
|
|
{-
|
|
|
|
This parses an ovum in a somewhat complicated way.
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
The event structure is not setup to be easily parsed into typed data,
|
2019-07-21 07:36:25 +03:00
|
|
|
since the type of the event depends on the head of the path, and
|
|
|
|
the shape of the rest of the path depends on the shape of the event.
|
|
|
|
|
|
|
|
To make parsing easier (indeed, to allow use to use `deriveEvent` to
|
|
|
|
generate parsers for this) we first re-arrange the data in the ovum.
|
|
|
|
|
|
|
|
And ovum is `[path event]`, but the first two fields of the path
|
|
|
|
are used for routing, the event is always a head-tagged structure,
|
|
|
|
and the rest of the path is basically data that's a part of the event.
|
|
|
|
|
|
|
|
So, we take something with this struture:
|
|
|
|
|
|
|
|
[[fst snd rest] [tag val]]
|
|
|
|
|
|
|
|
Then restructure it into *this* shape:
|
|
|
|
|
|
|
|
[fst [snd [tag rest val]]]
|
|
|
|
|
|
|
|
And then proceed with parsing as usual.
|
|
|
|
-}
|
2019-07-24 04:34:16 +03:00
|
|
|
data Ev
|
|
|
|
= EvBlip (Lenient BlipEv)
|
|
|
|
| EvVane Vane
|
2019-07-21 07:36:25 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
instance FromNoun Ev where
|
|
|
|
parseNoun n = named "Ev" $ do
|
2019-07-21 07:36:25 +03:00
|
|
|
(path::Path, tag::Cord, v::Noun) <- parseNoun n
|
|
|
|
case path of
|
2019-07-24 04:34:16 +03:00
|
|
|
Path ("" : m : p) -> EvBlip <$> parseNoun (toNoun (m, tag, p, v))
|
|
|
|
Path ("vane" : m : p) -> EvVane <$> parseNoun (toNoun (m, tag, p, v))
|
2019-07-21 07:36:25 +03:00
|
|
|
Path (_:_:_) -> fail "path must start with %$ or %vane"
|
|
|
|
Path (_:_) -> fail "path too short"
|
|
|
|
Path _ -> fail "empty path"
|
|
|
|
|
2019-07-24 04:34:16 +03:00
|
|
|
instance ToNoun Ev where
|
2019-07-21 07:36:25 +03:00
|
|
|
toNoun o =
|
|
|
|
fromNounErr noun & \case
|
|
|
|
Left err -> error (show err)
|
|
|
|
Right (pathSnd::Knot, tag::Cord, Path path, val::Noun) ->
|
|
|
|
toNoun (Path (pathHead:pathSnd:path), (tag, val))
|
|
|
|
where
|
|
|
|
(pathHead, noun) =
|
2019-07-24 04:34:16 +03:00
|
|
|
case o of EvBlip bo -> ("", toNoun bo)
|
|
|
|
EvVane vo -> ("vane", toNoun vo)
|