summaryrefslogtreecommitdiff
path: root/Types
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-11-17 17:19:04 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-11-17 18:30:50 -0400
commit49da2d5efdad0038f22bc5e3bc50cf117849d472 (patch)
treea5b5bb140c712f2d49e6ff5f3488fcdd5539ac60 /Types
parent6a176acc6e40a68740dab363eac4bdd324a77eb8 (diff)
implementation of peer-to-peer protocol
For use with tor hidden services, and perhaps other transports later. Based on Utility.SimpleProtocol, it's a line-based protocol, interspersed with transfers of bytestrings of a specified size. Implementation of the local and remote sides of the protocol is done using a free monad. This lets monadic code be included here, without tying it to any particular way to get bytes peer-to-peer. This adds a dependency on the haskell package "free", although that was probably pulled in transitively from other dependencies already. This commit was sponsored by Jeff Goeke-Smith on Patreon.
Diffstat (limited to 'Types')
-rw-r--r--Types/Key.hs5
-rw-r--r--Types/UUID.hs6
2 files changed, 11 insertions, 0 deletions
diff --git a/Types/Key.hs b/Types/Key.hs
index 3642eca1c..598fe43cc 100644
--- a/Types/Key.hs
+++ b/Types/Key.hs
@@ -27,6 +27,7 @@ import qualified Data.Text as T
import Common
import Utility.QuickCheck
import Utility.Bloom
+import qualified Utility.SimpleProtocol as Proto
{- A Key has a unique name, which is derived from a particular backend,
- and may contain other optional metadata. -}
@@ -129,6 +130,10 @@ instance FromJSON Key where
parseJSON (String t) = maybe mempty pure $ file2key $ T.unpack t
parseJSON _ = mempty
+instance Proto.Serializable Key where
+ serialize = key2file
+ deserialize = file2key
+
instance Arbitrary Key where
arbitrary = Key
<$> (listOf1 $ elements $ ['A'..'Z'] ++ ['a'..'z'] ++ ['0'..'9'] ++ "-_\r\n \t")
diff --git a/Types/UUID.hs b/Types/UUID.hs
index 4212eaa7f..f5c9cda30 100644
--- a/Types/UUID.hs
+++ b/Types/UUID.hs
@@ -13,6 +13,8 @@ import qualified Data.Map as M
import qualified Data.UUID as U
import Data.Maybe
+import qualified Utility.SimpleProtocol as Proto
+
-- A UUID is either an arbitrary opaque string, or UUID info may be missing.
data UUID = NoUUID | UUID String
deriving (Eq, Ord, Show, Read)
@@ -35,3 +37,7 @@ isUUID :: String -> Bool
isUUID = isJust . U.fromString
type UUIDMap = M.Map UUID String
+
+instance Proto.Serializable UUID where
+ serialize = fromUUID
+ deserialize = Just . toUUID