<commit_before>-- |
-- Module      : Vodki.Socket
-- Copyright   : (c) 2012 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module Vodki.Socket (
    -- * Exported Types
      Addr(..)

    -- * Vanilla Sockets
    , openSocket
    , Socket
    , SocketType(..)
    , S.send
    , S.recv
    , bindSocket
    , connect
    , setSocketOption

    -- * Retryable Sockets
    , SocketR
    , openSocketR
    , sendR
    ) where

import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class
import Data.IORef
import Network.Socket

import qualified Data.ByteString.Char8     as BS
import qualified Network.Socket.ByteString as S

data Addr = Addr String Int

instance Read Addr where
    readsPrec _ a = do
        (h, b)   <- lex a
        (":", c) <- lex b
        (p, d)   <- lex c
        return (Addr h $ read p, d)

instance Show Addr where
    show (Addr h p) = h ++ ":" ++ show p

data SocketR = SocketR
    { connAddr :: Addr
    , connType :: SocketType
    , connRef  :: IORef (Socket, SockAddr)
    }

openSocket :: Addr -> SocketType -> IO (Socket, SockAddr)
openSocket (Addr host port) stype = do
    i:_ <- getAddrInfo proto (Just host) (Just $ show port)
    s   <- socket (addrFamily i) stype defaultProtocol
    return (s, addrAddress i)
  where
    proto = case stype of
        Datagram -> Just $ defaultHints { addrFlags = [AI_PASSIVE] }
        _        -> Nothing

openSocketR :: Addr -> SocketType -> IO SocketR
openSocketR addr typ = do
    sa  <- openSocket addr typ
    ref <- newIORef sa
    return $ SocketR addr typ ref

sendR :: SocketR -> BS.ByteString -> IO ()
sendR r@SocketR{..} bstr = retry 3
  where
    delay   = 3
    retry 0 = fail "Out of retries, bitches!"
    retry n = do
        (s, a) <- readIORef connRef<commit_msg>Formatting<commit_after>-- |
-- Module      : Vodki.Socket
-- Copyright   : (c) 2012 <NAME> <<EMAIL>>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module Vodki.Socket (
    -- * Exported Types
      Addr(..)

    -- * Vanilla Sockets
    , openSocket
    , Socket
    , SocketType(..)
    , S.send
    , S.recv
    , bindSocket
    , connect
    , setSocketOption

    -- * Retryable Sockets
    , SocketR
    , openSocketR
    , sendR
    ) where

import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class
import Data.IORef
import Network.Socket

import qualified Data.ByteString.Char8     as BS
import qualified Network.Socket.ByteString as S

data Addr = Addr String Int

instance Read Addr where
    readsPrec _ a = do
        (h, b)   <- lex a
        (":", c) <- lex b
        (p, d)   <- lex c
        return (Addr h $ read p, d)

instance Show Addr where
    show (Addr h p) = h ++ ":" ++ show p

data SocketR = SocketR
    { connAddr :: Addr
    , connType :: SocketType
    , connRef  :: IORef (Socket, SockAddr)
    }

openSocket :: Addr -> SocketType -> IO (Socket, SockAddr)
openSocket (Addr host port) stype = do
    i:_ <- getAddrInfo proto (Just host) (Just $ show port)
    s   <- socket (addrFamily i) stype defaultProtocol
    return (s, addrAddress i)
  where
    proto = case stype of
        Datagram -> Just $ defaultHints { addrFlags = [AI_PASSIVE] }
        _        -> Nothing

openSocketR :: Addr -> SocketType -> IO SocketR
openSocketR addr typ = do
    sa <- openSocket addr typ
    r  <- newIORef sa
    return $ SocketR addr typ r

sendR :: SocketR -> BS.ByteString -> IO ()
sendR r@SocketR{..} bstr = retry 3
  where
    delay   = 3
    retry 0 = fail "Out of retries, bitches!"
    retry n = do
        (s, a) <- readIORef connRef
{'fraction_non_alphanumeric': 0.10900584795321637, 'fraction_numerical': 0.005614035087719298, 'mean_word_length': 2.1464311994113316, 'pattern_counts': {'":': 4, '<': 31, '<?xml version=': 0, '>': 31, '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': '876276', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1381, 'n_words': 469}