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
27 import qualified System.Posix.Files as F
33 args <- getArgs >>= parseargs 'a'
34 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
35 printparam' "routing key" $ rKey args
36 printparam' "exchange" $ currentExchange args
38 if inputFile args == "-"
40 else F.getFileStatus (inputFile args) >>= return . F.isDirectory
42 then printparam' "hotfolder" $ inputFile args
43 else printparam' "input file" $
46 then " (line-by-line)"
48 (conn, chan) <- connect args
49 addChannelExceptionHandler chan (X.throwTo tid)
50 printparam' "confirm mode" $ show $ confirm args
53 confirmSelect chan False
54 addConfirmationListener chan confirmCallback
56 let publishOneMsg = publishOneMsg' chan args
60 inotify <- initINotify
65 #if MIN_VERSION_hinotify(0,3,10)
66 (BS.pack (inputFile args))
70 (handleEvent publishOneMsg (suffix args) (inputFile args))
71 hr $ "BEGIN watching " ++ (inputFile args)
72 _ <- forever $ threadDelay 1000000
74 hr $ "END watching " ++ (inputFile args)
78 if inputFile args == "-"
80 else BL.readFile (inputFile args)
82 then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
83 else publishOneMsg (Just (inputFile args)) messageFile
86 -- all done. wait and close.
88 then waitForConfirms chan >>= (printparam' "confirmed") . show
90 X.catch (closeConnection conn) exceptionHandler
92 -- | A handler for clean exit
93 exceptionHandler :: AMQPException -> IO ()
94 exceptionHandler (ChannelClosedException Normal txt) = printparam' "exit" txt >> exitWith ExitSuccess
95 exceptionHandler (ConnectionClosedException Normal txt) = printparam' "exit" txt >> exitWith ExitSuccess
96 exceptionHandler x = printparam' "exception" (show x) >> exitWith (ExitFailure 1)
98 -- | The handler for publisher confirms
99 confirmCallback :: (Word64, Bool, AckType) -> IO ()
100 confirmCallback (deliveryTag, isAll, ackType) =
103 ((show deliveryTag) ++
109 -- | Hotfolder event handler
111 (Maybe String -> BL.ByteString -> IO ())
116 -- just handle closewrite and movedin events
117 #if MIN_VERSION_hinotify(0,3,10)
118 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ (BS.unpack x))
119 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ (BS.unpack x))
121 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ x)
122 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ x)
124 handleEvent _ _ _ _ = return ()
126 -- | Hotfolder file handler
128 (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
129 handleFile _ _ ('.':_) = return () -- ignore hidden files
130 handleFile f s@(_:_) x =
131 if any (flip isSuffixOf x) s
132 then handleFile f [] x
136 (BL.readFile x >>= f (Just x))
138 printparam' "exception in handleFile" $
139 show (exception :: X.SomeException))
141 -- | Publish one message with our settings
142 publishOneMsg' :: Channel -> Args -> Maybe String -> BL.ByteString -> IO ()
143 publishOneMsg' c a fn f = do
144 printparam "sending" fn
145 (mtype, mencoding) <-
146 if (magic a) && isJust fn
148 m <- magicOpen [MagicMimeType]
150 t <- magicFile m (fromJust fn)
151 magicSetFlags m [MagicMimeEncoding]
152 e <- magicFile m (fromJust fn)
153 return (Just (T.pack t), Just (T.pack e))
154 else return ((contenttype a), (contentencoding a))
155 now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
159 (T.pack $ currentExchange a)
163 , msgDeliveryMode = persistent a
164 , msgTimestamp = Just now
166 , msgType = msgtype a
167 , msgUserID = userid a
168 , msgApplicationID = appid a
169 , msgClusterID = clusterid a
170 , msgContentType = mtype
171 , msgContentEncoding = mencoding
172 , msgReplyTo = replyto a
173 , msgPriority = prio a
174 , msgCorrelationID = corrid a
175 , msgExpiration = msgexp a
176 , msgHeaders = substheader (fnheader a) fn $ msgheader a
178 printparam "sent" $ fmap show r
181 [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
182 substheader (s:r) (Just fname) old =
183 substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
184 substheader _ _ old = old