2 -- generic AMQP publisher
3 import Control.Concurrent (threadDelay)
4 import qualified Control.Exception as X
5 import Control.Monad (forever)
6 import qualified Data.ByteString.Lazy.Char8 as BL
7 #if MIN_VERSION_hinotify(0,3,10)
8 import qualified Data.ByteString.Char8 as BS
10 import Data.List (isSuffixOf)
12 import qualified Data.Text as T
14 import Data.Time.Clock.POSIX
15 import Data.Version (showVersion)
16 import Data.Word (Word64)
19 import Network.AMQP.Types
20 import Network.AMQP.Utils.Connection
21 import Network.AMQP.Utils.Helpers
22 import Network.AMQP.Utils.Options
23 import Paths_amqp_utils (version)
24 import System.Environment
26 import qualified System.Posix.Files as F
31 args <- getArgs >>= parseargs "agitprop"
32 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
33 printparam' "routing key" $ rKey args
34 isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
36 then printparam' "hotfolder" $ inputFile args
37 else printparam' "input file" $
40 then " (line-by-line)"
42 (conn, chan) <- connect args
43 printparam' "confirm mode" $ show $ confirm args
46 confirmSelect chan False
47 addConfirmationListener chan confirmCallback
49 let publishOneMsg = publishOneMsg' chan args
53 inotify <- initINotify
58 #if MIN_VERSION_hinotify(0,3,10)
59 (BS.pack (inputFile args))
63 (handleEvent publishOneMsg (suffix args) (inputFile args))
64 hr $ "BEGIN watching " ++ (inputFile args)
65 _ <- forever $ threadDelay 1000000
67 hr $ "END watching " ++ (inputFile args)
70 messageFile <- BL.readFile (inputFile args)
72 then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
73 else publishOneMsg (Just (inputFile args)) messageFile
75 (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
76 -- all done. wait and close.
78 then waitForConfirms chan >>= (printparam' "confirmed") . show
81 hr "connection closed"
83 -- | The handler for publisher confirms
84 confirmCallback :: (Word64, Bool, AckType) -> IO ()
85 confirmCallback (deliveryTag, isAll, ackType) =
88 ((show deliveryTag) ++
94 -- | Hotfolder event handler
96 (Maybe String -> BL.ByteString -> IO ())
101 -- just handle closewrite and movedin events
102 #if MIN_VERSION_hinotify(0,3,10)
103 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ (BS.unpack x))
104 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ (BS.unpack x))
106 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ x)
107 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ x)
109 handleEvent _ _ _ _ = return ()
111 -- | Hotfolder file handler
113 (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
114 handleFile _ _ ('.':_) = return () -- ignore hidden files
115 handleFile f s@(_:_) x =
116 if any (flip isSuffixOf x) s
117 then handleFile f [] x
121 (BL.readFile x >>= f (Just x))
123 printparam' "exception in handleFile" $
124 show (exception :: X.SomeException))
126 -- | Publish one message with our settings
127 publishOneMsg' :: Channel -> Args -> Maybe String -> BL.ByteString -> IO ()
128 publishOneMsg' c a fn f = do
129 printparam "sending" fn
130 (mtype, mencoding) <-
131 if (magic a) && isJust fn
133 m <- magicOpen [MagicMimeType]
135 t <- magicFile m (fromJust fn)
136 magicSetFlags m [MagicMimeEncoding]
137 e <- magicFile m (fromJust fn)
138 return (Just (T.pack t), Just (T.pack e))
139 else return ((contenttype a), (contentencoding a))
140 now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
144 (T.pack $ currentExchange a)
148 , msgDeliveryMode = persistent a
149 , msgTimestamp = Just now
151 , msgType = msgtype a
152 , msgUserID = userid a
153 , msgApplicationID = appid a
154 , msgClusterID = clusterid a
155 , msgContentType = mtype
156 , msgContentEncoding = mencoding
157 , msgReplyTo = replyto a
158 , msgPriority = prio a
159 , msgCorrelationID = corrid a
160 , msgExpiration = msgexp a
161 , msgHeaders = substheader (fnheader a) fn $ msgheader a
163 printparam "sent" $ fmap show r
166 [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
167 substheader (s:r) (Just fname) old =
168 substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
169 substheader _ _ old = old