2 -- generic AMQP publisher
3 import Control.Concurrent
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
32 args <- getArgs >>= parseargs 'a'
33 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
34 printparam' "routing key" $ rKey args
35 isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
37 then printparam' "hotfolder" $ inputFile args
38 else printparam' "input file" $
41 then " (line-by-line)"
43 (conn, chan) <- connect args
44 addChannelExceptionHandler chan (X.throwTo tid)
45 printparam' "confirm mode" $ show $ confirm args
48 confirmSelect chan False
49 addConfirmationListener chan confirmCallback
51 let publishOneMsg = publishOneMsg' chan args
55 inotify <- initINotify
60 #if MIN_VERSION_hinotify(0,3,10)
61 (BS.pack (inputFile args))
65 (handleEvent publishOneMsg (suffix args) (inputFile args))
66 hr $ "BEGIN watching " ++ (inputFile args)
67 _ <- forever $ threadDelay 1000000
69 hr $ "END watching " ++ (inputFile args)
72 messageFile <- BL.readFile (inputFile args)
74 then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
75 else publishOneMsg (Just (inputFile args)) messageFile
77 (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
78 -- all done. wait and close.
80 then waitForConfirms chan >>= (printparam' "confirmed") . show
83 hr "connection closed"
85 -- | The handler for publisher confirms
86 confirmCallback :: (Word64, Bool, AckType) -> IO ()
87 confirmCallback (deliveryTag, isAll, ackType) =
90 ((show deliveryTag) ++
96 -- | Hotfolder event handler
98 (Maybe String -> BL.ByteString -> IO ())
103 -- just handle closewrite and movedin events
104 #if MIN_VERSION_hinotify(0,3,10)
105 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ (BS.unpack x))
106 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ (BS.unpack x))
108 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ x)
109 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ x)
111 handleEvent _ _ _ _ = return ()
113 -- | Hotfolder file handler
115 (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
116 handleFile _ _ ('.':_) = return () -- ignore hidden files
117 handleFile f s@(_:_) x =
118 if any (flip isSuffixOf x) s
119 then handleFile f [] x
123 (BL.readFile x >>= f (Just x))
125 printparam' "exception in handleFile" $
126 show (exception :: X.SomeException))
128 -- | Publish one message with our settings
129 publishOneMsg' :: Channel -> Args -> Maybe String -> BL.ByteString -> IO ()
130 publishOneMsg' c a fn f = do
131 printparam "sending" fn
132 (mtype, mencoding) <-
133 if (magic a) && isJust fn
135 m <- magicOpen [MagicMimeType]
137 t <- magicFile m (fromJust fn)
138 magicSetFlags m [MagicMimeEncoding]
139 e <- magicFile m (fromJust fn)
140 return (Just (T.pack t), Just (T.pack e))
141 else return ((contenttype a), (contentencoding a))
142 now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
146 (T.pack $ currentExchange a)
150 , msgDeliveryMode = persistent a
151 , msgTimestamp = Just now
153 , msgType = msgtype a
154 , msgUserID = userid a
155 , msgApplicationID = appid a
156 , msgClusterID = clusterid a
157 , msgContentType = mtype
158 , msgContentEncoding = mencoding
159 , msgReplyTo = replyto a
160 , msgPriority = prio a
161 , msgCorrelationID = corrid a
162 , msgExpiration = msgexp a
163 , msgHeaders = substheader (fnheader a) fn $ msgheader a
165 printparam "sent" $ fmap show r
168 [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
169 substheader (s:r) (Just fname) old =
170 substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
171 substheader _ _ old = old