diff options
author | James Crayne <jim.crayne@gmail.com> | 2015-06-22 21:51:59 -0400 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2015-06-22 21:51:59 -0400 |
commit | a4592a399181aa27bde2ff954eb1077735474566 (patch) | |
tree | b383eeca3c78996c3c311491f332a36d7a56dab7 /kikid.hs | |
parent | ceec3fd413272bb47336ef2f7af42e1e44c9cc97 (diff) |
Serialization, refactoring...
Diffstat (limited to 'kikid.hs')
-rw-r--r-- | kikid.hs | 18 |
1 files changed, 9 insertions, 9 deletions
@@ -1,13 +1,12 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | {-# LANGUAGE DoAndIfThenElse #-} | ||
2 | 3 | ||
3 | import System.Posix.Daemonize | 4 | import System.Posix.Daemonize |
4 | import Control.Concurrent | 5 | import Control.Concurrent |
5 | import System.Posix.Syslog | 6 | import System.Posix.Syslog |
6 | import System.Posix.Signals | 7 | import System.Posix.Signals |
7 | import System.Posix.User (getEffectiveUserID) | 8 | import System.Posix.User (getEffectiveUserID) |
8 | import KikiD.PortServer | 9 | |
9 | import KikiD.Multiplex | ||
10 | import KikiD.Message | ||
11 | import Control.Concurrent.STM | 10 | import Control.Concurrent.STM |
12 | import Control.Concurrent.STM.TBMQueue | 11 | import Control.Concurrent.STM.TBMQueue |
13 | import Control.Concurrent.Async | 12 | import Control.Concurrent.Async |
@@ -20,7 +19,12 @@ import qualified Data.ByteString.Char8 as B | |||
20 | import qualified Data.Map as M | 19 | import qualified Data.Map as M |
21 | import qualified Data.Bytes.Serial as Bytes | 20 | import qualified Data.Bytes.Serial as Bytes |
22 | import qualified Data.Bytes.Get as Bytes | 21 | import qualified Data.Bytes.Get as Bytes |
23 | --import qualified Data.Bytes.Put as Bytes | 22 | import qualified Data.Bytes.Put as Bytes |
23 | |||
24 | import KikiD.PortServer | ||
25 | import KikiD.Multiplex | ||
26 | import KikiD.Message | ||
27 | import KikiD.ClientState | ||
24 | 28 | ||
25 | -- TODO: Set this in config file | 29 | -- TODO: Set this in config file |
26 | port = 9800 | 30 | port = 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 | ||
70 | data ClientState = CState {csQueue :: TBMQueue KikiDMessage} | ||
71 | type ClientID = ThreadId | ||
72 | threadIdToClient = id | ||
73 | |||
74 | addOpenConnections newchans currentClients = whileM_ (atomically . fmap not $ isClosedTBMQueue newchans) $ do | 74 | addOpenConnections 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 | |||
99 | handleMessage hdl outq = do | 99 | handleMessage 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) |