summaryrefslogtreecommitdiff
path: root/Presence
diff options
context:
space:
mode:
Diffstat (limited to 'Presence')
-rw-r--r--Presence/Server.hs61
1 files changed, 50 insertions, 11 deletions
diff --git a/Presence/Server.hs b/Presence/Server.hs
index 4cbaaa7d..5e54ff27 100644
--- a/Presence/Server.hs
+++ b/Presence/Server.hs
@@ -3,6 +3,9 @@
3{-# LANGUAGE StandaloneDeriving #-} 3{-# LANGUAGE StandaloneDeriving #-}
4{-# LANGUAGE OverloadedStrings #-} 4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE TupleSections #-} 5{-# LANGUAGE TupleSections #-}
6#ifdef TEST
7{-# LANGUAGE FlexibleInstances #-}
8#endif
6----------------------------------------------------------------------------- 9-----------------------------------------------------------------------------
7-- | 10-- |
8-- Module : Server 11-- Module : Server
@@ -57,6 +60,9 @@ import Network.BSD
57 ( getProtocolNumber 60 ( getProtocolNumber
58 ) 61 )
59import Debug.Trace 62import Debug.Trace
63import Data.Time.Clock (UTCTime,getCurrentTime)
64import Data.Time.Format (formatTime)
65import System.Locale (defaultTimeLocale)
60 66
61todo = error "unimplemented" 67todo = error "unimplemented"
62 68
@@ -154,10 +160,15 @@ data ConnectionEvent b
154 | RequiresPing 160 | RequiresPing
155 -- ^ 'pingInterval' miliseconds of idle was experienced 161 -- ^ 'pingInterval' miliseconds of idle was experienced
156 162
157{- 163#ifdef TEST
164instance Show (IO a) where show _ = "<IO action>"
165instance Show (STM a) where show _ = "<STM action>"
166instance Eq (ByteString -> IO Bool) where (==) _ _ = True
167instance Eq (IO (Maybe ByteString)) where (==) _ _ = True
168instance Eq (STM Bool) where (==) _ _ = True
158deriving instance Show b => Show (ConnectionEvent b) 169deriving instance Show b => Show (ConnectionEvent b)
159deriving instance Eq b => Eq (ConnectionEvent b) 170deriving instance Eq b => Eq (ConnectionEvent b)
160-} 171#endif
161 172
162-- | This object accepts commands and signals events and maintains 173-- | This object accepts commands and signals events and maintains
163-- the list of currently listening ports and established connections. 174-- the list of currently listening ports and established connections.
@@ -267,7 +278,7 @@ server = do
267 doit server (Connect addr params) = liftIO $ do 278 doit server (Connect addr params) = liftIO $ do
268 void . forkIO $ do 279 void . forkIO $ do
269 proto <- getProtocolNumber "tcp" 280 proto <- getProtocolNumber "tcp"
270 sock <- bracketOnError 281 bracketOnError
271 (socket (socketFamily addr) Stream proto) 282 (socket (socketFamily addr) Stream proto)
272 (\sock -> do -- only done if there's an error 283 (\sock -> do -- only done if there's an error
273 -- Weird hack: puting the would-be peer address 284 -- Weird hack: puting the would-be peer address
@@ -277,8 +288,8 @@ server = do
277 atomically 288 atomically
278 $ writeTChan (serverEvent server) 289 $ writeTChan (serverEvent server)
279 $ (conkey,ConnectFailure addr)) 290 $ (conkey,ConnectFailure addr))
280 $ \sock -> do connect sock addr 291 $ \sock -> do
281 return sock 292 connect sock addr
282 me <- getSocketName sock 293 me <- getSocketName sock
283 conkey <- makeConnKey params (sock,me) 294 conkey <- makeConnKey params (sock,me)
284 h <- socketToHandle sock ReadWriteMode 295 h <- socketToHandle sock ReadWriteMode
@@ -370,21 +381,22 @@ newConnection server params conkey h inout = do
370 kont <- updateConMap conkey new what 381 kont <- updateConMap conkey new what
371 putTMVar started () 382 putTMVar started ()
372 return kont 383 return kont
373 {- 384#ifdef TEST
374 -- enable this for 'Got' events 385 -- enable this for 'Got' events
375 forkIO $ do -- inout==In || duplex params then forkIO $ do 386 forkIO $ do -- inout==In || duplex params then forkIO $ do
376 -- warn $ "waiting read thread: " <> bshow (conkey,inout) 387 warn $ "waiting gots thread: " <> bshow (conkey,inout)
377 atomically $ takeTMVar started 388 atomically $ takeTMVar started
378 -- pingBump pinglogic -- start the ping timer 389 -- pingBump pinglogic -- start the ping timer
379 fix $ \loop -> do 390 fix $ \loop -> do
380 -- warn $ "read thread: " <> bshow (conkey,inout) 391 -- warn $ "read thread: " <> bshow (conkey,inout)
381 mb <- threadsRead new 392 mb <- threadsRead new
382 -- pingBump pinglogic 393 -- pingBump pinglogic
383 -- warn $ "got: " <> bshow (mb,(conkey,inout)) 394 warn $ "got: " <> bshow (mb,(conkey,inout))
384 maybe (return ()) 395 maybe (return ())
385 (atomically . forward >=> const loop) 396 (atomically . forward >=> const loop)
386 mb 397 mb
387 -} 398 warn $ "quit-gots: " <> bshow (conkey,inout)
399#endif
388 return () 400 return ()
389 where 401 where
390 402
@@ -408,9 +420,23 @@ newConnection server params conkey h inout = do
408 $ Map.delete conkey 420 $ Map.delete conkey
409 -- warn $ "fin-EOF "<>bshow conkey 421 -- warn $ "fin-EOF "<>bshow conkey
410 422
411 sendPing PingTimeOut = do atomically (connClose newCon) 423 sendPing PingTimeOut = do
412 eof 424 {-
425 let me = connPingTimer newCon
426 utc <- getCurrentTime
427 let utc' = formatTime defaultTimeLocale "%s" utc
428 warn $ "TIMEOUT " <> bshow utc' <> " " <> bshow (pingIdle me, pingTimeOut me)
429 -}
430 atomically (connClose newCon)
431 eof
432
413 sendPing PingIdle = do 433 sendPing PingIdle = do
434 {-
435 let me = connPingTimer newCon
436 utc <- getCurrentTime
437 let utc' = formatTime defaultTimeLocale "%s" utc
438 warn $ "IDLE" <> bshow utc' <> " " <> bshow (pingIdle me, pingTimeOut me)
439 -}
414 atomically $ announce (conkey,RequiresPing) 440 atomically $ announce (conkey,RequiresPing)
415 handleEOF conkey mvar newCon 441 handleEOF conkey mvar newCon
416 442
@@ -701,7 +727,20 @@ pingBump me = do
701 (1000*pingIdle me,PingIdle) 727 (1000*pingIdle me,PingIdle)
702 takeTMVar (pingStarted me) 728 takeTMVar (pingStarted me)
703 when b $ throwTo (pingThread me) $ ErrorCall "" 729 when b $ throwTo (pingThread me) $ ErrorCall ""
730 {-
731 utc <- getCurrentTime
732 let utc' = formatTime defaultTimeLocale "%s" utc
733 warn $ "BUMP " <> bshow utc' <> " " <> bshow (pingIdle me, pingTimeOut me)
734 -}
704 atomically $ putTMVar (pingStarted me) b 735 atomically $ putTMVar (pingStarted me) b
736{-
737pingBump me = do
738 b <- atomically $ do
739 when (pingIdle me /= 0) $
740 putTMVar (pingDelay me) (1000*pingIdle me,PingIdle)
741 readTMVar (pingStarted me)
742 when b $ throwTo (pingThread me) $ ErrorCall ""
743-}
705 744
706pingWait :: PingMachine -> STM PingEvent 745pingWait :: PingMachine -> STM PingEvent
707pingWait me = do 746pingWait me = do