Computer Science Canada

ebooks or books required ..

Author:  neokewl [ Fri Jan 05, 2007 2:58 am ]
Post subject:  ebooks or books required ..

Hi all,

I am thinking of developing a simple chat program using a client server architecture using haskell. Currently the books tht i have focus on the fundamentals of the language and the inbuilt functions.

I would like to know if there are libraries for networking operations and for that of gui. Also, I would appreciate if some one could give me a link to a resource that focuses on these aspects.


thnx
neokewl

Author:  haskell [ Fri Jan 05, 2007 3:19 pm ]
Post subject: 

Use Google instead of asking... There aren't many functional programmers, and even less using Haskell.

If you can't find it, then go to the Haskell mailing list on Usenet.

Author:  neokewl [ Fri Jan 05, 2007 4:14 pm ]
Post subject: 

n wot makes u feel tht i havent googled it before shooting a mail across.

I thought this was a functional programming group, n ppl in here should be learning or learnt or serious about functional programming.

So I think it is right for me to put a valid question to a correct group tht i have registered to. If u dont knw the answer then its fine, but it is not courteous of u too shoot the mails like the above as u did.

Author:  Hikaru79 [ Thu Jan 11, 2007 12:08 pm ]
Post subject: 

neokewl @ Fri Jan 05, 2007 5:14 pm wrote:
n wot makes u feel tht i havent googled it before shooting a mail across.


Not to seem rude or anything, but probably the fact that you didn't find anything. For example, here's the second hit when searching for "haskell networking": a tutorial on building an IRC bot in Haskell. I've only skimmed it, but it seems like a good introduction to basic network i/o.

And when I googled "haskell gui", the very top hit was a page with links to two different toolkits here. And the second hit is an actual Wikibook on getting those set up, here

See? Smile

Author:  haskell [ Thu Jan 11, 2007 6:00 pm ]
Post subject:  Re: ebooks or books required ..

Exactly. I found all sorts of things with simple searches.

And you don't exactly make the most sense. With all the spelling errors and such... Maybe you spelled something wrong(just throwing it out there).

Plus this isn't a mailing list. It is a message board. The mailing lists are much more indepth with the "experts" around there that are 100% focused on one particular language.

If you don't want the greatest help you can be given(told to go to a mailing list where the people are 100% dedicated to a language) than fine. I don't care.

Author:  rizzix [ Sun Feb 04, 2007 11:38 pm ]
Post subject:  RE:ebooks or books required ..

Here's a chat server i wrote in haskell long time ago... (It dosen't use any fancy STM.. so it should be easy to follow)

Haskell:
import System.IO
import Network
import Control.Concurrent
import Control.Concurrent.MVar

type Peer = (Handle, HostName, PortNumber)

data Message    = Msg Peer [Peer] String | Notice Peer [Peer] NoticeType
data NoticeType = Connected              | Disconnected

instance Show Message where
    show (Msg (_,h,p) _ str) = "MESSAGE " ++ h ++ ":" >++ show p >++ " " >++ str
    show (Notice (_,h,p) _ Connected) = "CONNECTED " ++ h ++ ":" >++ show p
    show (Notice (_,h,p) _ Disconnected) = "DISCONNECTED " ++ h ++ ":" >++ show p


-- the server's PortNumber
conn_port >:: PortID
conn_port = PortNumber 8090


-- setup server, fork dispatch thread and wait for clients
main >:: IO ()
main = withSocketsDo $ do
    server_sock <- listenOn conn_port
    cont_var    <- newMVar True
    clients_var <- newMVar [] >:: IO (MVar [Peer])
    msg_var     <- newEmptyMVar >:: IO (MVar Message)
    _ <- forkIO $ while cont_var $ dispatchMessages msg_var
    while cont_var $ waitForClients cont_var msg_var server_sock clients_var


-- while cont_var $ do stuff...
while >:: MVar Bool -> IO () -> IO ()
while cont_var process = do
    cont <- readMVar cont_var
    case cont of
        True  -> process >> while cont_var process
        False -> return ()


-- dispatch message in buffer to all clients
dispatchMessages >:: MVar Message -> IO ()
dispatchMessages msg_var = do
    msg <- takeMVar msg_var
    case msg of
        Notice p pz Connected -> p `reveal` pz >> tellAll pz (show msg)
        _                     -> tellAll (peers msg) (show msg)
  where tellAll _  [] = return ()
        tellAll [] _  = return ()
        tellAll ((h,_,_):cs) msg = hPutStrLn h msg >> tellAll cs msg
       
        reveal _  []  = return ()
        reveal c@(h,_,_) (p:pz) = do
            hPutStrLn h $ show $ Notice p (c:pz) Connected
            reveal c pz
       
        peers (Msg _ cs _) = cs
        peers (Notice _ cs _) = cs


-- for each client spawn a new thread and proccess requests
waitForClients >:: MVar Bool -> MVar Message -> Socket -> MVar [Peer] -> IO ()
waitForClients cont_var msg_var server_socket clients_var = do
    c@(h,_,_) <- accept server_socket
    hSetBuffering h LineBuffering
    modifyMVar_ clients_var (registerWithClients c)
    _ <- forkIO $ processClient cont_var msg_var c clients_var
    return ()
    where registerWithClients c cs = do
              putMVar msg_var (Notice c cs Connected)
              return (c:cs)


-- each client is proccessed in an endless loop until "BYE"
processClient >:: MVar Bool -> MVar Message -> Peer -> MVar [Peer] -> IO ()
processClient cont_var msg_var c@(h, host, port) clients_var = do
    cont <- readMVar cont_var
    case cont of
        True -> do
            line <- hGetLine h
            ccs <- readMVar clients_var
            let cs = filter (/=c) ccs
            case line of
                ('B':'Y':'E':_) -> do
                    hClose h
                    modifyMVar_ clients_var (\_ -> return cs)
                    putMVar msg_var (Notice c cs Disconnected)
                    testQuitServer cs
                _  -> do
                    putMVar msg_var (Msg c cs line)
                    processClient cont_var msg_var c clients_var
        _ -> return ()
  where testQuitServer [] = do modifyMVar_ cont_var (\_ -> return False)
                               h <- connectTo "localhost" conn_port
                               hClose h
        testQuitServer _  = return ()


You can telnet to it and test it out Wink

Oh and ignore the '>' character before the '::' in the type definitions.. It is a marker Dan had applied to debug the syntax script.


: