Skip to content

Commit

Permalink
Merge pull request xively#16 from paulbellamy/byte-array-will-payload
Browse files Browse the repository at this point in the history
parse will payload as byte array not string
  • Loading branch information
paulbellamy committed Dec 17, 2013
2 parents e5ab289 + 1dcda50 commit e720737
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@ In the project.clj file at the top level of your project, add clj-mqtt as a depe
```clj
(defproject my-project "1.0.0"
:dependencies [[org.clojure/clojure "1.5.1"]
[clj-mqtt "0.4.0-alpha"]])
[clj-mqtt "0.4.1-alpha"]])
```

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject clj-mqtt "0.4.0-alpha"
(defproject clj-mqtt "0.4.1-alpha"

:description "Clojure MQTT Codec for Netty"

Expand Down
11 changes: 10 additions & 1 deletion src/mqtt/decoding_utils.clj
Expand Up @@ -52,10 +52,19 @@
[in & kvs]
(do-parse-flags (parse-unsigned-byte in) {} 8 kvs))

(defn parse-short-prefixed-bytes
"Decode a short, then as many bytes as the short says."
[^ByteBuf in]
(let [len (int (parse-unsigned-short in))]
(assert-readable-bytes in len)
(let [bs (byte-array len)]
(.readBytes in bs)
bs)))

(defn parse-string
"Decode a utf-8 encoded string. Strings are preceeded by 2 bytes describing
the length of the remaining content."
[^ByteBuf in]
(let [len (int (parse-unsigned-short in))]
(assert-readable-bytes in len)
(.toString (.readBytes in len) (Charset/forName "UTF-8"))))
(.toString (.readSlice in len) (Charset/forName "UTF-8"))))
2 changes: 1 addition & 1 deletion src/mqtt/packets/connect.clj
Expand Up @@ -103,7 +103,7 @@
(defn- decode-will-payload
[{:keys [has-will] :as packet} in]
(if has-will
(assoc packet :will-payload (parse-string in))
(assoc packet :will-payload (parse-short-prefixed-bytes in))
packet))

(defn- decode-username
Expand Down
4 changes: 2 additions & 2 deletions test/mqtt/packets/connect_test.clj
Expand Up @@ -207,7 +207,7 @@
(is (= "topic" (:will-topic decoded))))

(testing "should set the payload of the will packet"
(is (= "hello" (:will-payload decoded))))))
(is (= "hello" (String. (:will-payload decoded)))))))

(testing "when parsing a Connect packet with a username and password"
(let [decoder (make-decoder)
Expand Down Expand Up @@ -375,7 +375,7 @@
(is (= "will_topic" (:will-topic decoded))))

(testing "should set the will payload"
(is (= "will_payload" (:will-payload decoded))))
(is (= "will_payload" (String. (:will-payload decoded)))))

(testing "should have a username"
(is (= "user0123456789" (:username decoded))))
Expand Down

0 comments on commit e720737

Please sign in to comment.