Permalink
Browse files

Merge pull request #4 from ghoseb/master

Minor improvements.
  • Loading branch information...
2 parents a3a7a1f + 0c7fab2 commit 54788e98c78c3c2f34a4b0eb55adb664ec22e238 @niclasmeier niclasmeier committed Jan 11, 2013
Showing with 110 additions and 171 deletions.
  1. +7 −11 .gitignore
  2. +33 −41 src/herolabs/apns/feedback.clj
  3. +7 −15 src/herolabs/apns/message.clj
  4. +22 −47 src/herolabs/apns/protocol.clj
  5. +29 −33 src/herolabs/apns/push.clj
  6. +12 −24 src/herolabs/apns/ssl.clj
View
@@ -1,14 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
pom.xml
-*jar
-*class
-*iml
-/lib/
-/classes/
-/newrelic/logs/
+*.jar
+*.class
.lein-deps-sum
.lein-failures
-/.idea/
-*.iml
-.project
-.classpath
-.settings/
+.lein-plugins
@@ -14,25 +14,23 @@
[java.util.concurrent Executors ExecutorService ThreadFactory]
[java.util.concurrent LinkedBlockingQueue TimeUnit]
[java.net InetSocketAddress]
- [javax.net.ssl SSLContext]
- )
- )
+ [javax.net.ssl SSLContext]))
(def ^:private default-thread-pool* (atom nil))
(defn default-thread-pool []
(or @default-thread-pool*
- (swap! default-thread-pool*
- (fn [_] (Executors/newCachedThreadPool
- (let [number (atom 1)
- sm (System/getSecurityManager)
- group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
- (reify ThreadFactory
- (newThread [_ r] (let [t (Thread. group r (str "apns-feedback-" (swap! number inc)) 0)
- t (if (.isDaemon t) (.setDaemon t false) t)
- t (if (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY) t)]
- t)))))))))
+ (swap! default-thread-pool*
+ (fn [_] (Executors/newCachedThreadPool
+ (let [number (atom 1)
+ sm (System/getSecurityManager)
+ group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
+ (reify ThreadFactory
+ (newThread [_ r] (let [t (Thread. group r (str "apns-feedback-" (swap! number inc)) 0)
+ t (if (.isDaemon t) (.setDaemon t false) t)
+ t (if (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY) t)]
+ t)))))))))
(def ^:private timer* (ref nil))
@@ -45,19 +43,17 @@
(channelConnected [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(debug "channelConnected")
(let [ssl-handler (-> ctx
- (.getPipeline)
- (.get SslHandler))]
- (.handshake ssl-handler)
- ))
+ (.getPipeline)
+ (.get SslHandler))]
+ (.handshake ssl-handler)))
(messageReceived [^ChannelHandlerContext ctx ^MessageEvent event]
(debug "messageReceived - " (.getMessage event))
(.put queue (.getMessage event)))
(exceptionCaught [^ChannelHandlerContext ctx ^ExceptionEvent event]
(debug (.getCause event) "exceptionCaught")
(-> event
- (.getChannel)
- (.close))
- )
+ (.getChannel)
+ (.close)))
(channelClosed [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(debug "channelClosed"))))
@@ -68,46 +64,42 @@
(reify
ChannelPipelineFactory
(getPipeline [this]
- (doto (Channels/pipeline)
- (.addLast "ssl" (SslHandler. ssl-engine))
- (.addLast "decoder" (feedback-decoder))
- (.addLast "timeout" (ReadTimeoutHandler. (timer) (int (if time-out time-out 300))))
- (.addLast "handler" handler)))))
+ (doto (Channels/pipeline)
+ (.addLast "ssl" (SslHandler. ssl-engine))
+ (.addLast "decoder" (feedback-decoder))
+ (.addLast "timeout" (ReadTimeoutHandler. (timer) (int (if time-out time-out 300))))
+ (.addLast "handler" handler)))))
(defn- connect [^InetSocketAddress address ^SSLContext ssl-context time-out queue boss-executor worker-executor]
"creates a netty Channel to connect to the server."
(try
(let [engine (ssl-engine ssl-context :use-client-mode true)
pipeline-factory (create-pipeline-factory engine (handler queue) time-out)
bootstrap (doto (-> (NioClientSocketChannelFactory. boss-executor worker-executor)
- (ClientBootstrap.))
- (.setOption "connectTimeoutMillis" 5000)
- (.setPipelineFactory pipeline-factory))
+ (ClientBootstrap.))
+ (.setOption "connectTimeoutMillis" 5000)
+ (.setPipelineFactory pipeline-factory))
future (.connect bootstrap address)
channel (-> future
- (.awaitUninterruptibly)
- (.getChannel)
- )]
+ (.awaitUninterruptibly)
+ (.getChannel))]
(if (.isSuccess future)
channel
(do
(.releaseExternalResources bootstrap)
- nil
- )
- )
- )
+ nil)))
(catch java.lang.Exception e
(warn e "Error"))))
(defn- read-feedback [queue channel]
"Internal function to create a lazy-seq returning the data from the feedback service"
(lazy-seq
- (if-let [next (.poll queue 10 TimeUnit/SECONDS)]
- (cons next (read-feedback queue channel))
- (when (.isConnected channel)
- (.close channel)
- nil))))
+ (if-let [next (.poll queue 10 TimeUnit/SECONDS)]
+ (cons next (read-feedback queue channel))
+ (when (.isConnected channel)
+ (.close channel)
+ nil))))
(defn feedback [^InetSocketAddress address ^SSLContext ssl-context & {:keys [time-out boss-executor worker-executor]
:or {time-out 300
@@ -120,4 +112,4 @@
(defn dev-address [] (InetSocketAddress. "feedback.sandbox.push.apple.com" 2196))
-(defn prod-address [] (InetSocketAddress. "feedback.push.apple.com" 2196))
+(defn prod-address [] (InetSocketAddress. "feedback.push.apple.com" 2196))
@@ -4,41 +4,33 @@
(defn with-badge [message number] (assoc-in message [:aps :badge ] number))
(defn with-sound [message sound]
- (assoc-in message [:aps :sound ] (name sound))
- )
+ (assoc-in message [:aps :sound ] (name sound)))
(defn with-standard-alert [message body]
- (assoc-in message [:aps :alert ] body)
- )
+ (assoc-in message [:aps :alert ] body))
(defn with-action-loc-key [message key]
(if key
(assoc-in message [:aps :alert :action-loc-key ] key)
- message)
- )
+ message))
(defn with-loc-key [message key]
(if key
(assoc-in message [:aps :alert :loc-key ] key)
- message)
- )
+ message))
(defn with-loc-args [message args]
(if-not (empty? args)
(assoc-in message [:aps :alert :loc-args ] (if (sequential? args) args (list args)))
- message)
- )
+ message))
(defn with-payload [message payload]
(if-not (empty? payload)
(merge (dissoc payload :aps) message)
- message)
- )
+ message))
(defn with-alert-body [message body]
(if key
(assoc-in message [:aps :alert :body ] body)
- message)
- )
-
+ message))
@@ -4,8 +4,7 @@
[org.jboss.netty.buffer ChannelBuffer ChannelBuffers]
[java.nio ByteOrder]
[org.apache.commons.codec.binary Hex]
- [java.util.concurrent.atomic AtomicInteger]
- ))
+ [java.util.concurrent.atomic AtomicInteger]))
;; some constants
(def ^:private standard-head (byte-array 1 (byte 0)))
@@ -19,39 +18,32 @@
(byte 5) :invalid-token-size
(byte 6) :invalid-topic-size
(byte 7) :invalid-payload-size
- (byte 8) :invalid-token
- })
+ (byte 8) :invalid-token})
(def ^:dynamic *coercions* json/*coercions*)
(defn- serialize [msg]
"Serializes the map into a JSON representation"
(binding [json/*coercions* *coercions*]
- (json/generate-string msg)
- )
- )
+ (json/generate-string msg)))
(defn- dynamic-buffer [len]
"Creates a dynamic buffer."
- (ChannelBuffers/dynamicBuffer ByteOrder/BIG_ENDIAN len)
- )
+ (ChannelBuffers/dynamicBuffer ByteOrder/BIG_ENDIAN len))
(defn- encode-message [^String device-token msg]
"Encodes a message into the standard APNS protocol format
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html"
(let [token (.decode hex-codec device-token)
serialized (serialize msg)
buffer (doto (dynamic-buffer (+ 1 2 (count token) 2 (count serialized)))
- (.writeBytes standard-head)
- (.writeShort (int (count token)))
- (.writeBytes token)
- (.writeShort (int (count serialized)))
- (.writeBytes (.getBytes serialized)))
- ]
- buffer
- )
- )
+ (.writeBytes standard-head)
+ (.writeShort (int (count token)))
+ (.writeBytes token)
+ (.writeShort (int (count serialized)))
+ (.writeBytes (.getBytes serialized)))]
+ buffer))
@@ -64,17 +56,14 @@
expires (get m :expires Integer/MAX_VALUE)
serialized (serialize msg)
buffer (doto (dynamic-buffer (+ 1 4 4 2 (count token) 2 (count serialized)))
- (.writeBytes enhanced-head)
- (.writeInt id)
- (.writeInt (int expires))
- (.writeShort (int (count token)))
- (.writeBytes token)
- (.writeShort (int (count serialized)))
- (.writeBytes (.getBytes serialized)))
- ]
- buffer
- )
- )
+ (.writeBytes enhanced-head)
+ (.writeInt id)
+ (.writeInt (int expires))
+ (.writeShort (int (count token)))
+ (.writeBytes token)
+ (.writeShort (int (count serialized)))
+ (.writeBytes (.getBytes serialized)))]
+ buffer))
@@ -87,14 +76,8 @@
(if-let [device-token (get (meta msg) :device-token )]
(if (= :enhanced (get (meta msg) :format ))
(encode-enhanced-message id-gen device-token msg)
- (encode-message device-token msg)
- )
- (throw (IllegalArgumentException. "Message must contain a :device-token as meta."))
- )
- )
- )
- )
- )
+ (encode-message device-token msg))
+ (throw (IllegalArgumentException. "Message must contain a :device-token as meta.")))))))
(defn decoder []
@@ -104,11 +87,7 @@
(let [command (.readByte msg)
status (.readByte msg)
id (.readInt msg)]
- {:status (get status-dictionary status :unknown ) :id id}
- )
- )
- )
- )
+ {:status (get status-dictionary status :unknown ) :id id}))))
(defn feedback-decoder []
"Creates an decoder for the APNS protocol."
@@ -119,8 +98,4 @@
token-bytes (byte-array token-len)]
(.readBytes msg token-bytes)
(let [token (Hex/encodeHexString token-bytes)]
- [token time]
- ))
- )
- )
- )
+ [token time])))))
Oops, something went wrong.

0 comments on commit 54788e9

Please sign in to comment.