Minor refactoring

pull/1/head
Abhinav Sarkar 2015-09-10 15:55:54 +05:30
parent affa18de55
commit d749c5d06a
5 changed files with 13 additions and 19 deletions

View File

@ -19,7 +19,6 @@ library
Link.Types,
Link.Protocol,
Link.Client
other-modules: Link.Util
default-extensions: OverloadedStrings, RecordWildCards, ScopedTypeVariables,
BangPatterns, TupleSections, NamedFieldPuns
build-depends: base >= 4.7 && < 5,

View File

@ -5,16 +5,15 @@ import Control.Concurrent hiding (forkFinally)
import Control.Exception hiding (handle)
import Control.Monad (void, forever, when, unless, forM_)
import Data.Time (getCurrentTime, diffUTCTime)
import System.IO (hGetLine)
import System.IO (hGetLine, Handle)
import System.Timeout (timeout)
import Text.Printf (printf)
import Text.Printf (printf, hPrintf)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Link.Protocol
import Link.Types
import Link.Util
forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
forkFinally action fun =
@ -30,6 +29,9 @@ sendMessageIO client = atomically . sendMessage client
tellMessage :: Channel -> Message -> STM ()
tellMessage Channel {..} = writeTChan channelChan
printToHandle :: Handle -> String -> IO ()
printToHandle handle = hPrintf handle "%s\n"
sendResponse :: Client -> Message -> IO ()
sendResponse Client {..} = printToHandle clientHandle . formatMessage

View File

@ -19,13 +19,14 @@ parseCommand command = case words command of
formatMessage :: Message -> String
formatMessage (MsgReply user msg) = printf "MSG %s %s" (userName user) msg
formatMessage (TellReply channelName user msg) =
printf "TELL %s %s %s" channelName (userName user) msg
formatMessage (NameInUse name) = printf "NAMEINUSE %s" name
formatMessage (Connected name) = printf "CONNECTED %s" name
formatMessage Ping = "PING"
formatMessage (NoSuchUser name) = printf "NOSUCHUSER %s" name
formatMessage (Joined channelName user) = printf "JOINED %s %s" channelName (userName user)
formatMessage (Leaved channelName user) = printf "LEFT %s %s" channelName (userName user)
formatMessage (TellReply channelName user msg) =
printf "TELL %s %s %s" channelName (userName user) msg
formatMessage (NamesReply channelName users) =
printf "NAMES %s %s" channelName . unwords . map userName . Set.toList $ users
formatMessage msg = error $ printf "Cannot format message: %s" (show msg)

View File

@ -13,7 +13,6 @@ import qualified Data.Map.Strict as Map
import Link.Client
import Link.Protocol
import Link.Types
import Link.Util
runServer :: Int -> IO ()
runServer port = withSocketsDo $ do

View File

@ -1,7 +0,0 @@
module Link.Util where
import System.IO (Handle)
import Text.Printf (hPrintf)
printToHandle :: Handle -> String -> IO ()
printToHandle handle = hPrintf handle "%s\n"