add lock map
This commit is contained in:
parent
0bdee626ad
commit
58031455dc
1 changed files with 30 additions and 11 deletions
41
P2P/Http.hs
41
P2P/Http.hs
|
@ -30,10 +30,12 @@ import Network.HTTP.Client (defaultManagerSettings, newManager)
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.Encoding as TE
|
import qualified Data.Text.Encoding as TE
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
|
import qualified Data.Map as M
|
||||||
import Text.Read (readMaybe)
|
import Text.Read (readMaybe)
|
||||||
import Data.Aeson hiding (Key)
|
import Data.Aeson hiding (Key)
|
||||||
import Control.DeepSeq
|
import Control.DeepSeq
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
|
import Control.Concurrent.Async
|
||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
|
|
||||||
|
@ -84,7 +86,7 @@ p2pHttpApp = serve p2pHttpAPI . serveP2pHttp
|
||||||
|
|
||||||
serveP2pHttp :: P2PHttpServerState -> Server P2PHttpAPI
|
serveP2pHttp :: P2PHttpServerState -> Server P2PHttpAPI
|
||||||
serveP2pHttp st
|
serveP2pHttp st
|
||||||
= serveGet st
|
= serveGet st
|
||||||
:<|> serveGet st
|
:<|> serveGet st
|
||||||
:<|> serveGet st
|
:<|> serveGet st
|
||||||
:<|> serveGet st
|
:<|> serveGet st
|
||||||
|
@ -440,20 +442,18 @@ serveKeepLocked
|
||||||
-> Maybe KeepAlive
|
-> Maybe KeepAlive
|
||||||
-> S.SourceT IO UnlockRequest
|
-> S.SourceT IO UnlockRequest
|
||||||
-> Handler LockResult
|
-> Handler LockResult
|
||||||
serveKeepLocked _st k cu su _ _ _ unlockrequeststream = do
|
serveKeepLocked st key cu su _ _ _ unlockrequeststream = do
|
||||||
_ <- liftIO $ S.unSourceT unlockrequeststream go
|
_ <- liftIO $ S.unSourceT unlockrequeststream go
|
||||||
return (LockResult False)
|
return (LockResult False)
|
||||||
where
|
where
|
||||||
go S.Stop = do
|
go S.Stop = dropLock lckid st
|
||||||
print "lost connection to client, drop lock here" -- XXX TODO
|
go (S.Error _err) = dropLock lckid st
|
||||||
go (S.Error err) = do
|
|
||||||
print ("Error", err)
|
|
||||||
print "error, drop lock here" -- XXX TODO
|
|
||||||
go (S.Skip s) = go s
|
go (S.Skip s) = go s
|
||||||
go (S.Effect ms) = ms >>= go
|
go (S.Effect ms) = ms >>= go
|
||||||
go (S.Yield (UnlockRequest False) s) = go s
|
go (S.Yield (UnlockRequest False) s) = go s
|
||||||
go (S.Yield (UnlockRequest True) _) = do
|
go (S.Yield (UnlockRequest True) _) = dropLock lckid st
|
||||||
print ("got unlock request, drop lock here") -- XXX TODO
|
|
||||||
|
lckid = undefined -- FIXME
|
||||||
|
|
||||||
clientKeepLocked
|
clientKeepLocked
|
||||||
:: P2P.ProtocolVersion
|
:: P2P.ProtocolVersion
|
||||||
|
@ -527,10 +527,28 @@ testClientLock = do
|
||||||
[]
|
[]
|
||||||
keeplocked
|
keeplocked
|
||||||
|
|
||||||
|
|
||||||
data P2PHttpServerState = P2PHttpServerState
|
data P2PHttpServerState = P2PHttpServerState
|
||||||
|
{ openLocks :: TMVar (M.Map LockID Locker)
|
||||||
|
}
|
||||||
|
|
||||||
mkP2PHttpServerState :: IO P2PHttpServerState
|
mkP2PHttpServerState :: IO P2PHttpServerState
|
||||||
mkP2PHttpServerState = return P2PHttpServerState
|
mkP2PHttpServerState = P2PHttpServerState
|
||||||
|
<$> newTMVarIO mempty
|
||||||
|
|
||||||
|
type LockID = B64UUID Lock
|
||||||
|
|
||||||
|
data Locker = Locker
|
||||||
|
{ lockerThread :: Async ()
|
||||||
|
, lockerVar :: TMVar Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
storeLock :: LockID -> P2PHttpServerState -> IO ()
|
||||||
|
storeLock lckid st = error "TODO" -- XXX
|
||||||
|
|
||||||
|
dropLock :: LockID -> P2PHttpServerState -> IO ()
|
||||||
|
dropLock lckid st = error "TODO" -- XXX
|
||||||
|
|
||||||
|
|
||||||
type ClientUUID req = QueryParam' '[req] "clientuuid" (B64UUID ClientSide)
|
type ClientUUID req = QueryParam' '[req] "clientuuid" (B64UUID ClientSide)
|
||||||
|
|
||||||
|
@ -553,6 +571,7 @@ data ClientSide
|
||||||
data ServerSide
|
data ServerSide
|
||||||
data Bypass
|
data Bypass
|
||||||
data Plus
|
data Plus
|
||||||
|
data Lock
|
||||||
|
|
||||||
-- Keys, UUIDs, and filenames are base64 encoded since Servant uses
|
-- Keys, UUIDs, and filenames are base64 encoded since Servant uses
|
||||||
-- Text and so needs UTF-8.
|
-- Text and so needs UTF-8.
|
||||||
|
@ -560,7 +579,7 @@ newtype B64Key = B64Key Key
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
newtype B64UUID t = B64UUID UUID
|
newtype B64UUID t = B64UUID UUID
|
||||||
deriving (Show, Generic, NFData)
|
deriving (Show, Ord, Eq, Generic, NFData)
|
||||||
|
|
||||||
newtype B64FilePath = B64FilePath RawFilePath
|
newtype B64FilePath = B64FilePath RawFilePath
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
Loading…
Reference in a new issue