<commit_before>-- Copyright:  <NAME>, 2011
-- License:    BSD

{-
    ZMQ ABNF
    connection  = greeting content
    greeting    = anonymous / identity
    anonymous   = %x01 final
    identity    = length final (%x01-ff) *OCTET
    
    message     = *more-frame final-frame
    more-frame  = length more body
    final-frame = length final body
    length      = OCTET / (%xFF 8OCTET)
    more        = %x01
    final       = %x00
    body        = *OCTET
-}

module ZMQHS.Connection
where
import ZMQHS.Frame      as X
import ZMQHS.Message    as X
import ZMQHS.ConnSpec   as X
-- I want type sigs to be more general. How can I get rid of this?
import GHC.Int
import Control.Applicative hiding (empty)
import Control.Monad.IO.Class
import qualified Network.Socket as S
import qualified Data.Binary.Put as P
import           Data.Conduit
import           Data.Conduit.Attoparsec
import           Data.Conduit.Network
import           Data.ByteString (ByteString)

data Connection m = Connection (IO (m Message)) (IO (Sink ByteString IO ()))

source :: ConnSpec -> Identity -> IO (Source IO ByteString)
source connspec@(servaddr,servport,socktype) id = do
  addrinfos <- S.getAddrInfo (Just S.defaultHints) (Just servaddr) (Just servport)
  let servinfo = head addrinfos
  sock <- S.socket (S.addrFamily servinfo) socktype S.defaultProtocol
  S.connect sock (S.addrAddress servinfo)
  return $ sourceSocket sock

sink :: ConnSpec -> Identity -> IO (Sink ByteString IO ())
sink connspec@(servaddr,servport,socktype) id = do
  addrinfos <- S.getAddrInfo (Just S.defaultHints) (Just servaddr) (Just servport)
  let servaddrinfo = head addrinfos
  sock <- S.socket (S.addrFamily servaddrinfo) socktype S.defaultProtocol
  S.bindSocket sock (S.addrAddress servaddrinfo)
  S.listen sock 1
  return $ sinkSocket sock

connect :: (IO (Source IO ByteString)) -> IO Message
connect source_promise = do
  source <- source_promise
  ($$) source (sinkParser getMessage)

connspec = case spec "tcp://0.0.0.0:7890" of
  Just a -> a
  Nothing -> error "no way that didn't work"

do_connect = do
  src <- source connspec Anonymous
  putStrLn "connecting... "
  src $$ sinkParser getMessage
  putStrLn "connected!"<commit_msg>Get rid of dumb local variable name<commit_after>-- Copyright:  <NAME>, 2011
-- License:    BSD

{-
    ZMQ ABNF
    connection  = greeting content
    greeting    = anonymous / identity
    anonymous   = %x01 final
    identity    = length final (%x01-ff) *OCTET
    
    message     = *more-frame final-frame
    more-frame  = length more body
    final-frame = length final body
    length      = OCTET / (%xFF 8OCTET)
    more        = %x01
    final       = %x00
    body        = *OCTET
-}

module ZMQHS.Connection
where
import ZMQHS.Frame      as X
import ZMQHS.Message    as X
import ZMQHS.ConnSpec   as X
-- I want type sigs to be more general. How can I get rid of this?
import GHC.Int
import Control.Applicative hiding (empty)
import Control.Monad.IO.Class
import qualified Network.Socket as S
import qualified Data.Binary.Put as P
import           Data.Conduit
import           Data.Conduit.Attoparsec
import           Data.Conduit.Network
import           Data.ByteString (ByteString)

data Connection m = Connection (IO (m Message)) (IO (Sink ByteString IO ()))

client :: (IO (Source IO ByteString)) -> IO Message
client source_promise = do
  source <- source_promise
  ($$) source (sinkParser getMessage)

sink :: ConnSpec -> Identity -> IO (Sink ByteString IO ())
sink connspec@(servaddr,servport,socktype) id = do
  addrinfos <- S.getAddrInfo (Just S.defaultHints) (Just servaddr) (Just servport)
  let servaddrinfo = head addrinfos
  sock <- S.socket (S.addrFamily servaddrinfo) socktype S.defaultProtocol
  S.bindSocket sock (S.addrAddress servaddrinfo)
  S.listen sock 1
  return $ sinkSocket sock

connspec = case spec "tcp://0.0.0.0:7890" of
  Just a -> a
  Nothing -> error "no way that didn't work"

source :: ConnSpec -> Identity -> IO (Source IO ByteString)
source connspec@(servaddr,servport,socktype) id = do
  addrinfos <- S.getAddrInfo (Just S.defaultHints) (Just servaddr) (Just servport)
  let servinfo = head addrinfos
  sock <- S.socket (S.addrFamily servinfo) socktype S.defaultProtocol
  S.connect sock (S.addrAddress servinfo)
  return $ sourceSocket sock

do_connect = do
  src <- source connspec Anonymous
  putStrLn "connecting... "
  src $$+ sinkParser getMessage
  putStrLn "connected!"
{'fraction_non_alphanumeric': 0.09353166553977912, 'fraction_numerical': 0.009916610322289835, 'mean_word_length': 3.097876269621422, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6111985', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1399, 'n_words': 463}