-- |
-- Module: Network.Greskell.WebSocket.Connection.Settings
-- Description: Settings for making Connection
-- Maintainer: Toshio Ito <debug.ito@gmail.com>
--
--
module Network.Greskell.WebSocket.Connection.Settings
    ( -- * Settings
      Settings
    , defSettings
    , defJSONSettings
      -- ** accessor functions
    , codec
    , endpointPath
    , onGeneralException
    , responseTimeout
    , concurrency
    , requestQueueSize
    ) where

import           Data.Greskell.GraphSON                     (FromGraphSON)

import           Network.Greskell.WebSocket.Codec           (Codec)
import           Network.Greskell.WebSocket.Codec.JSON      (jsonCodec)
import           Network.Greskell.WebSocket.Connection.Type (GeneralException)

import           System.IO                                  (hPutStrLn, stderr)

-- | 'Settings' for making connection to Gremlin Server.
--
-- You can get the default 'Settings' by 'defSettings' function, and
-- customize its fields by accessor functions.
--
-- Type @s@ is the body of Response.
data Settings s
  = Settings
      { forall s. Settings s -> Codec s
codec              :: !(Codec s)
        -- ^ codec for the connection.
      , forall s. Settings s -> String
endpointPath       :: !String
        -- ^ Path of the WebSocket endpoint. Default: \"/gremlin\"
      , forall s. Settings s -> GeneralException -> IO ()
onGeneralException :: !(GeneralException -> IO ())
        -- ^ An exception handler for 'GeneralException'. This exception
        -- is not fatal, so the connection survives after this handler is
        -- called. You don't have to re-throw the exception. Default:
        -- print the exception to stderr.
      , forall s. Settings s -> Int
responseTimeout    :: !Int
        -- ^ Time out (in seconds) for responses. It is the maximum time
        -- for which the connection waits for a response to complete after
        -- it sends a request. If the response consists of more than one
        -- ResponseMessages, the timeout applies to the last of the
        -- ResponseMessages. Default: 60
      , forall s. Settings s -> Int
concurrency        :: !Int
        -- ^ Maximum concurrent requests the connection can make to the
        -- server. If the client tries to make more concurrent requests
        -- than this value, later requests are queued in the connection or
        -- the client may be blocked. Default: 4
      , forall s. Settings s -> Int
requestQueueSize   :: !Int
        -- ^ Size of the internal queue of requests. Usually you don't
        -- need to customize the field. See also 'concurrency'. Default:
        -- 8.
      }

defSettings :: Codec s -> Settings s
defSettings :: forall s. Codec s -> Settings s
defSettings Codec s
c = Settings
                { codec :: Codec s
codec = Codec s
c,
                  endpointPath :: String
endpointPath = String
"/gremlin",
                  onGeneralException :: GeneralException -> IO ()
onGeneralException = \GeneralException
e -> Handle -> String -> IO ()
hPutStrLn Handle
stderr forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show GeneralException
e,
                  responseTimeout :: Int
responseTimeout = Int
60,
                  concurrency :: Int
concurrency = Int
4,
                  requestQueueSize :: Int
requestQueueSize = Int
8
                }

-- | 'defSettings' with 'jsonCodec'.
defJSONSettings :: FromGraphSON s => Settings s
defJSONSettings :: forall s. FromGraphSON s => Settings s
defJSONSettings = forall s. Codec s -> Settings s
defSettings forall s. FromGraphSON s => Codec s
jsonCodec