27

I'm trying to do a simple Ping Pong using Haskell and Thrift. However, it does only one repetition and then it gets stuck. I assume that the problem is in the (in)correct usage of Thrift rather than in Haskell. Probably something is not flushed correctly. Is there anyone with experience with Thrift who could help me making an educated guess on how to fix this?

Server:

echorequest :: TXT
echorequest = TXT {
    f_TXT_anytxt = Just "Ping"
    }

echoreply :: TXT
echoreply = TXT {
    f_TXT_anytxt = Just "Pong"
    }

serverFunc :: a -> (BinaryProtocol Handle, BinaryProtocol Handle)
              -> IO Bool
serverFunc a (h1,h2) = do
  let t1 = getTransport h1
  dat <- read_TXT h1
-- the following two lines are only for debugging
  putStrLn "Recieved data:"
  print dat
  write_TXT h1 echoreply
  tFlush t1
-- the following line is for debugging
  putStrLn "Data written"

  return False


main :: IO ()
main = do
   runBasicServer () serverFunc 4390
   putStrLn "Server stopped"

Client:

main :: IO ()
main = do
  h <- connectTo "127.0.0.1" $ PortNumber 4390
  let proto = BinaryProtocol h
  putStrLn "Client started"
  let tryOnePing c i = do
      write_TXT proto echorequest
      putStrLn "ping sent"
      tFlush h
      w <- read_TXT proto
      putStrLn "pong received"
      return $ if w == echoreply then c+1 else c
  c <- foldM tryOnePing 0 [0 .. 1000]
  tClose h
  print (c, 10000 - c)
hammar
  • 134,089
  • 17
  • 290
  • 377
J Fritsch
  • 3,278
  • 15
  • 38
  • 3
    I don't think your code fragments are enough for anyone to help you. How is `read_TXT` implemented? If it uses something like `hGetContents` then that's your problem - the print won't know when all the contents have been received and will block till EOF. That is probably why closing the connection and opening a new one helps. – Thomas M. DuBuisson Nov 24 '11 at 18:18
  • @ThomasM.DuBuisson It is the Thrift read function. In this case to read one struct. – J Fritsch Nov 29 '11 at 14:58

1 Answers1

1

Your problem is you're returning False from serverFunc. Haskell will loop until you return false (by your code, only once)

http://hackage.haskell.org/packages/archive/thrift/0.6.0/doc/html/src/Thrift-Server.html#line-65

Andras Gyomrey
  • 1,552
  • 11
  • 33