summaryrefslogtreecommitdiff
path: root/kikid.hs
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2015-06-22 21:51:59 -0400
committerJames Crayne <jim.crayne@gmail.com>2015-06-22 21:51:59 -0400
commita4592a399181aa27bde2ff954eb1077735474566 (patch)
treeb383eeca3c78996c3c311491f332a36d7a56dab7 /kikid.hs
parentceec3fd413272bb47336ef2f7af42e1e44c9cc97 (diff)
Serialization, refactoring...
Diffstat (limited to 'kikid.hs')
-rw-r--r--kikid.hs18
1 files changed, 9 insertions, 9 deletions
diff --git a/kikid.hs b/kikid.hs
index 04e06d3..059a5df 100644
--- a/kikid.hs
+++ b/kikid.hs
@@ -1,13 +1,12 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE DoAndIfThenElse #-}
2 3
3import System.Posix.Daemonize 4import System.Posix.Daemonize
4import Control.Concurrent 5import Control.Concurrent
5import System.Posix.Syslog 6import System.Posix.Syslog
6import System.Posix.Signals 7import System.Posix.Signals
7import System.Posix.User (getEffectiveUserID) 8import System.Posix.User (getEffectiveUserID)
8import KikiD.PortServer 9
9import KikiD.Multiplex
10import KikiD.Message
11import Control.Concurrent.STM 10import Control.Concurrent.STM
12import Control.Concurrent.STM.TBMQueue 11import Control.Concurrent.STM.TBMQueue
13import Control.Concurrent.Async 12import Control.Concurrent.Async
@@ -20,7 +19,12 @@ import qualified Data.ByteString.Char8 as B
20import qualified Data.Map as M 19import qualified Data.Map as M
21import qualified Data.Bytes.Serial as Bytes 20import qualified Data.Bytes.Serial as Bytes
22import qualified Data.Bytes.Get as Bytes 21import qualified Data.Bytes.Get as Bytes
23--import qualified Data.Bytes.Put as Bytes 22import qualified Data.Bytes.Put as Bytes
23
24import KikiD.PortServer
25import KikiD.Multiplex
26import KikiD.Message
27import KikiD.ClientState
24 28
25-- TODO: Set this in config file 29-- TODO: Set this in config file
26port = 9800 30port = 9800
@@ -67,10 +71,6 @@ kikidMain _ = do
67 atomically $ closeTBMQueue newchans 71 atomically $ closeTBMQueue newchans
68 atomically $ closeTBMQueue incomming 72 atomically $ closeTBMQueue incomming
69 73
70data ClientState = CState {csQueue :: TBMQueue KikiDMessage}
71type ClientID = ThreadId
72threadIdToClient = id
73
74addOpenConnections newchans currentClients = whileM_ (atomically . fmap not $ isClosedTBMQueue newchans) $ do 74addOpenConnections newchans currentClients = whileM_ (atomically . fmap not $ isClosedTBMQueue newchans) $ do
75 cliMap <- atomically $ readTVar currentClients :: IO (M.Map ClientID ClientState) 75 cliMap <- atomically $ readTVar currentClients :: IO (M.Map ClientID ClientState)
76 whileM_ (atomically . fmap not $ isEmptyTBMQueue newchans) $ do 76 whileM_ (atomically . fmap not $ isEmptyTBMQueue newchans) $ do
@@ -99,7 +99,7 @@ purgeClosedConnections quit currentClients = whileM_ (fmap not quit) $ do
99handleMessage hdl outq = do 99handleMessage hdl outq = do
100 line <- B.hGetLine hdl 100 line <- B.hGetLine hdl
101 tid <- myThreadId 101 tid <- myThreadId
102 case (Bytes.runGetS Bytes.deserialize (B.snoc line '\n') :: Either String KikiDMessage) of 102 case (Bytes.runGetS Bytes.deserialize line :: Either String KikiDMessage) of
103 Right msg -> do 103 Right msg -> do
104 syslog Notice ("Message decoded on thread=" <> show tid) 104 syslog Notice ("Message decoded on thread=" <> show tid)
105 syslog Notice ("Message: " <> show msg) 105 syslog Notice ("Message: " <> show msg)