<commit_before>module Pakej.Client
  ( client
  , repl
  ) where

import           Control.Monad
import           Control.Exception (bracket)
import           Data.String (fromString)
import qualified Data.Text.Lazy as Text
import qualified Data.Text.Lazy.IO as Text
import           Network
import           System.Exit (exitFailure)
import           System.IO (Handle, hClose, hFlush, hPutStrLn, stdout, stderr)
import           System.Timeout (timeout)
import           Text.Printf (printf)

import           Pakej.Communication


client :: HostName -> PortID -> Request -> IO ()
client n p command = do
  res <- timeout (5 * second) $ do
    h <- connectTo n p
    send h command
    recv h
  case res of
    Nothing ->
      exitFailure
    Just (Left _) ->
      exitFailure
    Just (Right (DQuery response)) ->
      Text.putStrLn response
    Just (Right (DStatus commands)) ->
      Text.putStrLn (Text.unwords commands)

second :: Int
second = 1000000

repl :: HostName -> PortID -> IO ()
repl host port = forever $ do
  prompt msg
  raw <- getLine
  case parseCommand raw of
    Just command ->
      connect host port $ \h -> do
        send h command
        res <- timeout (5 * second) (recv h)
        case res of
          Nothing ->
            hPutStrLn stderr "*** Pakej did not respond"
          Just (Left e) ->
            hPutStrLn stderr ("*** Pakej responded with gibberish: " ++ e)
          Just (Right (DQuery response)) ->
            Text.putStrLn response
          Just (Right (DStatus commands)) ->
            Text.putStrLn (Text.unwords commands)
    Nothing ->
      hPutStrLn stderr ("*** Unknown command: `" ++ raw ++ "'")
 where msg = printf "pakej %s:%s >>> " host (prettyPort port)

-- | Pretty print the port to use in prompt message
prettyPort :: PortID -> String
prettyPort (PortNumber n) = show n
prettyPort (Service s)    = s
prettyPort (UnixSocket s) = s

connect :: HostName -> PortID -> (Handle -> IO ()) -> IO ()
connect n p = bracket (connectTo n p) hClose

prompt :: String ->  IO ()
prompt m = do
  putStr m
  hFlush stdout

parseCommand :: String -> Maybe Request
parseCommand raw
  | (":", "stat") <- span (== ':') raw = Just CStatus
  | [query]       <- words raw         = Just (CQuery (fromString query))
  | otherwise = Nothing
<commit_msg>Timeout applies to the whole exchange with Pakej<commit_after>module Pakej.Client
  ( client
  , repl
  ) where

import           Control.Monad
import           Control.Exception (bracket)
import           Data.String (fromString)
import qualified Data.Text.Lazy as Text
import qualified Data.Text.Lazy.IO as Text
import           Network
import           System.Exit (exitFailure)
import           System.IO (Handle, hClose, hFlush, hPutStrLn, stdout, stderr)
import           System.Timeout (timeout)
import           Text.Printf (printf)

import           Pakej.Communication


client :: HostName -> PortID -> Request -> IO ()
client n p command = do
  res <- timeout (5 * second) $ do
    h <- connectTo n p
    send h command
    recv h
  case res of
    Nothing ->
      exitFailure
    Just (Left _) ->
      exitFailure
    Just (Right (DQuery response)) ->
      Text.putStrLn response
    Just (Right (DStatus commands)) ->
      Text.putStrLn (Text.unwords commands)

second :: Int
second = 1000000

repl :: HostName -> PortID -> IO ()
repl host port = forever $ do
  prompt msg
  raw <- getLine
  case parseCommand raw of
    Just command -> do
      res <- timeout (5 * second) . connect host port $ \h -> do
        send h command
        recv h
      case res of
        Nothing ->
          hPutStrLn stderr "*** Pakej did not respond"
        Just (Left e) ->
          hPutStrLn stderr ("*** Pakej responded with gibberish: " ++ e)
        Just (Right (DQuery response)) ->
          Text.putStrLn response
        Just (Right (DStatus commands)) ->
          Text.putStrLn (Text.unwords commands)
    Nothing ->
      hPutStrLn stderr ("*** Unknown command: `" ++ raw ++ "'")
 where msg = printf "pakej %s:%s >>> " host (prettyPort port)

-- | Pretty print the port to use in prompt message
prettyPort :: PortID -> String
prettyPort (PortNumber n) = show n
prettyPort (Service s)    = s
prettyPort (UnixSocket s) = s

connect :: HostName -> PortID -> (Handle -> IO a) -> IO a
connect n p = bracket (connectTo n p) hClose

prompt :: String ->  IO ()
prompt m = do
  putStr m
  hFlush stdout

parseCommand :: String -> Maybe Request
parseCommand raw
  | (":", "stat") <- span (== ':') raw = Just CStatus
  | [query]       <- words raw         = Just (CQuery (fromString query))
  | otherwise = Nothing

{'fraction_non_alphanumeric': 0.10757575757575757, 'fraction_numerical': 0.003896103896103896, 'mean_word_length': 2.2983583154889367, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1160036', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1359, 'n_words': 494}