-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server: separate into couple of modules
- Loading branch information
Showing
5 changed files
with
78 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{-# LANGUAGE UnicodeSyntax #-} | ||
module Server.Client (clientProcessing) where | ||
|
||
import Control.Applicative ((<$>)) | ||
import Control.Concurrent | ||
import Control.Exception | ||
import Control.Monad (forever) | ||
import System.IO | ||
|
||
import Hach.Types | ||
import Server.Message | ||
import Server.Storage | ||
|
||
readC ∷ Chan (Int, S2C) → Handle → IO () | ||
readC ch h = hPrint h =<< snd <$> readChan ch | ||
|
||
clientProcessing ∷ Storage → Chan (Int, S2C) → Handle → Int → IO () | ||
clientProcessing storage ch h cId = do | ||
ch' ← dupChan ch | ||
forkIO $ handle_ $ forever $ readC ch' h | ||
forever $ do | ||
m ← hGetLine h | ||
maybeNick ← getNick storage cId | ||
case maybeNick of | ||
Just nick → do | ||
go nick $ read m | ||
putStrLn m | ||
where go ∷ Nick → C2S → IO () | ||
go n (CMessage t) = writeChan ch' (cId, SMessage n t) | ||
go n (CAction t) = writeChan ch' (cId, SAction n t) | ||
go _ (CSetNick t) = do | ||
nickExists ← doesNickExist storage t | ||
if nickExists | ||
then hPrint h $ existedNickM t | ||
else do writeChan ch' (cId, settedNickM nick t) | ||
putNick storage cId t | ||
Nothing → do | ||
go $ read m | ||
putStrLn m | ||
where go ∷ C2S → IO () | ||
go (CMessage _) = hPrint h undefinedNickM | ||
go (CAction _) = hPrint h undefinedNickM | ||
go (CSetNick t) = do | ||
nickExists ← doesNickExist storage t | ||
if nickExists | ||
then do hPrint h $ existedNickM t | ||
else do writeChan ch' (cId, connectedClientM t) | ||
putNick storage cId t | ||
where handle_ = handle $ \(SomeException e) → print e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{-# LANGUAGE UnicodeSyntax #-} | ||
module Server.Message where | ||
|
||
import Hach.Types | ||
|
||
connectedClientM ∷ Nick → S2C | ||
connectedClientM nick = SSystem $ nick ++ " is connected." | ||
|
||
existedNickM ∷ Nick → S2C | ||
existedNickM nick = SSystem $ "Nickname " ++ nick ++ " is already in use." | ||
|
||
leftClientM ∷ Nick → S2C | ||
leftClientM nick = SSystem $ nick ++ " has left conversation." | ||
|
||
settedNickM ∷ Nick → Nick → S2C | ||
settedNickM nickFrom nickTo = SSetNick nickFrom $ "is know as " ++ nickTo ++ "." | ||
|
||
undefinedNickM ∷ S2C | ||
undefinedNickM = SSystem "To join a chat please set another nick with /nick command." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters