Skip to content

Commit

Permalink
Use BoundedChannel to communicate
Browse files Browse the repository at this point in the history
  • Loading branch information
shterrett committed Nov 14, 2018
1 parent 2315b69 commit 12a910e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 61 deletions.
48 changes: 30 additions & 18 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
{-# LANGUAGE BangPatterns #-}

module Main where

import Control.Concurrent.Async (race_)
import Control.Concurrent.Async (concurrently)
import Control.Concurrent.BoundedChan ( BoundedChan
, readChan
, writeChan
, newBoundedChan
)
import Control.Monad (forM_)
import GHC.Conc.Sync (atomically, STM)
import Options.Applicative (execParser)
import Args
import Itunes
import Actions (getAction)
import Channel
import Debug.Trace

main :: IO ()
main = execParser app >>= run

run :: Args -> IO ()
run args = let
action = getAction (actionType args)
channel = newChannel 500 -- wild ass guess
channel = newBoundedChan 500 -- wild ass guess
transform = mkTransform (target args) (attributes args)
in
(race_ (readSources channel transform [source args])
(doAction channel action)) >>
putStrLn "finished"
in
channel >>= (\c ->
(concurrently (readSources transform [source args] c)
(doAction action c))) >>
putStrLn "finished"

readSources :: STM (Channel Copy) -> Transform -> [Directory] -> IO ()
readSources _ _ [] = return ()
readSources chan t (d:ds) = do
readSources :: Transform ->
[Directory] ->
BoundedChan (Maybe Copy) ->
IO ()
readSources _ [] chan = writeChan chan Nothing
readSources t (d:ds) chan = do
(dirs, copies) <- handleDirectory d t
atomically $ enqueueCopies chan copies
readSources chan t (ds ++ dirs)

enqueueCopies :: STM (Channel Copy) -> [Copy] -> STM ()
enqueueCopies chan cs = chan >>= writeChan cs
forM_ (Just <$> copies) (writeChan chan)
readSources t (ds ++ dirs) chan

doAction :: STM (Channel Copy) -> Action -> IO ()
doAction chan action =
atomically (chan >>= readChan) >>= action
doAction :: Action -> BoundedChan (Maybe Copy) -> IO ()
doAction action chan = do
copy <- readChan chan
case copy of
(Just c) -> action c >> doAction action chan
Nothing -> return ()
2 changes: 1 addition & 1 deletion escape-from-itunes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ library
exposed-modules: Itunes
, Args
, Attributes
, Channel
, Actions
build-depends: base >= 4.7 && < 5
, idiii == 0.1.3.3
Expand All @@ -38,6 +37,7 @@ executable escape-from-itunes
, escape-from-itunes
, optparse-applicative == 0.14.3.0
, async == 2.2.1
, BoundedChan == 1.0.3.0
default-language: Haskell2010

test-suite escape-from-itunes-test
Expand Down
42 changes: 0 additions & 42 deletions src/Channel.hs

This file was deleted.

1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extra-deps: [
, filepath-1.4.2
, stm-2.5.0.0
, async-2.2.1
, BoundedChan-1.0.3.0
]

# Override default flag values for local packages and extra-deps
Expand Down

0 comments on commit 12a910e

Please sign in to comment.