<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Network.N2O where

import Control.Exception
import Data.BERT
import qualified Data.ByteString.Lazy as BL
import Data.Binary
import Data.Monoid ((<>))
import Network.WebSockets as WS
import Control.Concurrent
import Network.N2O.PubSub

eval :: BL.ByteString -> BL.ByteString
eval x = encode $ TupleTerm [AtomTerm "io", NilTerm, showBERT x]

call fun arg = BL.concat [fun,  "('", arg, "');"]

call0 fun = fun <> "()"

application nextMessage handle state pending = do
    connection <- WS.acceptRequest pending
    putStrLn "accepted"
    socketId <- modifyMVar state $ return . subscribe connection
    putStrLn $ "Connected socketId = " ++ show socketId

    (receiveN2O connection >> nextMessage (handle state) connection socketId) `catch` somecatch `catch` iocatch -- `catch` (\e -> print $ "Got exception " ++ show (e::WS.ConnectionException)) `catch` somecatch
    putStrLn $ "Disconnected socketId = " ++ show socketId
    modifyMVar_ state $ return . unsubscribe socketId
    handle state connection socketId [AtomTerm "N2O_DISCONNECT"]

somecatch :: SomeException -> IO ()
somecatch e = print "SomeException" >> print e

iocatch :: IOException -> IO ()
iocatch _ = print "IOException"

simple ip port handle state = WS.runServer ip port $ application nextMessage handle state


nextMessage handle connection socketId = do
    let loop = nextMessage handle connection socketId
    message <- receiveMessage connection
    print "Parsed"
    print message
    handle connection socketId message
    loop

--simpleApp x = application simpleLoop x 

receiveN2O connection = do
    message    <- WS.receiveDataMessage connection<commit_msg>Removed conditional loop concept altogether<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Network.N2O where

import Control.Exception
import Data.BERT
import qualified Data.ByteString.Lazy as BL
import Data.Binary
import Data.Monoid ((<>))
import Network.WebSockets as WS
import Control.Concurrent
import Control.Monad
import Network.N2O.PubSub

eval :: BL.ByteString -> BL.ByteString
eval x = encode $ TupleTerm [AtomTerm "io", NilTerm, showBERT x]

call fun arg = BL.concat [fun,  "('", arg, "');"]

call0 fun = fun <> "()"

application nextMessage handle state pending = do
    connection <- WS.acceptRequest pending
    putStrLn "accepted"
    socketId <- modifyMVar state $ return . subscribe connection
    putStrLn $ "Connected socketId = " ++ show socketId

    (receiveN2O connection >> forever (nextMessage (handle state) connection socketId)) `catch` somecatch `catch` iocatch -- `catch` (\e -> print $ "Got exception " ++ show (e::WS.ConnectionException)) `catch` somecatch
    putStrLn $ "Disconnected socketId = " ++ show socketId
    modifyMVar_ state $ return . unsubscribe socketId
    handle state connection socketId [AtomTerm "N2O_DISCONNECT"]

somecatch :: SomeException -> IO ()
somecatch e = print "SomeException" >> print e

iocatch :: IOException -> IO ()
iocatch _ = print "IOException"

simple ip port handle state = WS.runServer ip port $ application nextMessage handle state

nextMessage handle connection socketId = do
    message <- receiveMessage connection
    print "Parsed"
    print message
    handle connection socketId message

--simpleApp x = application simpleLoop x 

receiveN2O connection = do
    message    <- WS.receiveDataMessage connection
{'fraction_non_alphanumeric': 0.09366715758468336, 'fraction_numerical': 0.0035346097201767305, 'mean_word_length': 4.208588957055214, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '5787802', 'n_tokens_mistral': 978, 'n_tokens_neox': 947, 'n_words': 333}