Skip to content

Commit

Permalink
fix that unregister-to doesn't send the REGISTER message with 'Expire…
Browse files Browse the repository at this point in the history
…s: 0'
  • Loading branch information
Ruiyun committed Aug 6, 2012
1 parent 817ada0 commit dd3b740
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/cljain/dum.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
:on-failure (fn [& {:keys [response]}] (println \"Oops!\" (.getStatusCode response))) :on-failure (fn [& {:keys [response]}] (println \"Oops!\" (.getStatusCode response)))
:on-timeout (fn [_] (println \"Timeout, try it later.\"))) :on-timeout (fn [_] (println \"Timeout, try it later.\")))
Remember, if you want send REGISTER to sip registry, please use the 'register-to' function, that will Remember, if you want send REGISTER to sip registry, please use the 'register-to!' function, that will
help you to deal the automatic rigister refresh: help you to deal the automatic rigister refresh:
(register-to (addr/address \"sip:the-registry\") 3600 (register-to! (addr/address \"sip:the-registry\") 3600
:on-success #(prn \"Register success.\") :on-success #(prn \"Register success.\")
:on-failure #(prn \"Register failed.\") :on-failure #(prn \"Register failed.\")
:on-refreshed #(prn \"Refreshed fine.\") :on-refreshed #(prn \"Refreshed fine.\")
Expand Down Expand Up @@ -218,11 +218,16 @@
:private true} :private true}
register-ctx-map (atom {})) register-ctx-map (atom {}))


(defn register-to (defn- reg-req-for-refresh [registry-address]
(doto (.clone (get-in @register-ctx-map [registry-address :request ]))
(msg/inc-sequence-number!)
(.removeHeader "Via")))

(defn register-to!
"Send REGISTER sip message to target registry server, and auto refresh register before "Send REGISTER sip message to target registry server, and auto refresh register before
expired. expired.
Notice: please call 'global-set-account' before you call 'register-to'. in this version, Notice: please call 'global-set-account' before you call 'register-to!'. in this version,
use dynamic binding form to bind *current-account* can not work for auto-refresh." use dynamic binding form to bind *current-account* can not work for auto-refresh."
{:added "0.3.0"} {:added "0.3.0"}
[registry-address expires-seconds & {:keys [on-success on-failure on-refreshed on-refresh-failed]}] [registry-address expires-seconds & {:keys [on-success on-failure on-refreshed on-refresh-failed]}]
Expand All @@ -240,16 +245,15 @@
:more-headers [expires-header] :more-headers [expires-header]
:on-success (fn [& {:keys [transaction]}] :on-success (fn [& {:keys [transaction]}]
(swap! register-ctx-map assoc registry-address (swap! register-ctx-map assoc registry-address
{:timer (timer/run-task! #(let [request (.clone (get-in @register-ctx-map [registry-address :request ])) {:timer (timer/run-task!
request (msg/inc-sequence-number! request) #(let [request (reg-req-for-refresh registry-address)
request (doto request (.removeHeader "Via")) transaction (core/new-client-transcation! request)]
transaction (core/new-client-transcation! request)] (.setApplicationData transaction
(.setApplicationData transaction {:on-success (fn [& _] (and on-refreshed (on-refreshed)))
{:on-success (fn [& _] (and on-refreshed (on-refreshed))) :on-failure (fn [& _] (and on-refresh-failed (on-refresh-failed)))
:on-failure (fn [& _] (and on-refresh-failed (on-refresh-failed))) :on-timeout (fn [& _] (and on-refresh-failed (on-refresh-failed)))})
:on-timeout (fn [& _] (and on-refresh-failed (on-refresh-failed)))}) (trans/send-request! transaction)
(trans/send-request! transaction) (swap! register-ctx-map assoc-in [registry-address :request ] request))
(swap! register-ctx-map assoc-in [registry-address :request ] request))
:by (timer/timer "Register-Refresh") :by (timer/timer "Register-Refresh")
:delay safer-interval-milliseconds :delay safer-interval-milliseconds
:period safer-interval-milliseconds :period safer-interval-milliseconds
Expand All @@ -259,11 +263,14 @@
:on-failure (fn [& _] (and on-failure (on-failure))) :on-failure (fn [& _] (and on-failure (on-failure)))
:on-timeout (fn [& _] (and on-failure (on-failure))))))) :on-timeout (fn [& _] (and on-failure (on-failure)))))))


(defn unregister-to (defn unregister-to!
"Send REGISTER sip message with expires 0 for unregister. "Send REGISTER sip message with expires 0 for unregister.
And the auto-refresh timer will be canceled." And the auto-refresh timer will be canceled."
{:added "0.3.0"} {:added "0.3.0"}
[registry-address] [registry-address]
(let [refresh-timer (get-in @register-ctx-map [registry-address :timer])] (let [refresh-timer (get-in @register-ctx-map [registry-address :timer])]
(and refresh-timer (timer/cancel! refresh-timer)) (and refresh-timer (timer/cancel! refresh-timer))
(let [request (reg-req-for-refresh registry-address)]
(.setHeader request (header/expires 0))
(trans/send-request! (core/new-client-transcation! request)))
(swap! register-ctx-map dissoc registry-address))) (swap! register-ctx-map dissoc registry-address)))
2 changes: 1 addition & 1 deletion test/cljain/test/register.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(sip/set-listener! (dum-listener)) (sip/set-listener! (dum-listener))
(sip/start!) (sip/start!)


(register-to (addr/address "sip:localhost") 40 (register-to! (addr/address "sip:localhost") 40
:on-success #(prn "success") :on-success #(prn "success")
:on-failure #(prn "failure") :on-failure #(prn "failure")
:on-refreshed #(prn "refreshed") :on-refreshed #(prn "refreshed")
Expand Down

0 comments on commit dd3b740

Please sign in to comment.