UDP sockets.

bare-udp

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (bare-udp uri & {:as opts})

Type signature:
  • ((U nil String+ Uri) ⨯ Any) → IAcquirableFactory

Returns connectionless UDP resource factory with given uri and opts set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.resource.udp :refer [bare-udp datagram]]
            [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.repl :refer [pst]]))

;; UDP broadcast
(with-io-scope
  (let [r (acquire! (bare-udp nil
                              :local-port 43214
                              :broadcast? true
                              :batch-size 100))]
    (println! (config r))

    (future
      (println! "monitor enter")
      (io!
       (try
         (vec (map #(println! "received " %) (read! r)))
         (catch java.nio.channels.AsynchronousCloseException e
           (println! "monitor exit"))
         (catch java.lang.Exception e
           (println! "exception in monitor " (pst e))))))

    (write! r [(datagram "192.168.1.255" 43214 [1 2 3 4 5 6])])

    (sleep 1000)))
;; {:uri #uri "bare-udp://0.0.0.0:43214?bs=100&broadcast=true" :protocol-family :inet :local-address nil :local-port 43214 :batch-size 100 :reuse? nil :broadcast? true :tos nil :in-buffer-size nil :out-buffer-size nil :multicast-if nil :multicast-ttl nil :multicast-loop true :payload-fn #<udp$payload__GT_array_coll dunaj.resource.udp$payload__GT_array_coll@406bf9b6> :selector-provider #<EPollSelectorProvider sun.nio.ch.EPollSelectorProvider@421c10c> :non-blocking? false}
;; monitor enter
;; received  {:address 192.168.1.101 :port 43214 :payload (1 2 3 4 5 6)}
;; monitor exit
;;=> nil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.resource.udp :refer [bare-udp datagram]]
            [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.repl :refer [pst]]))

;; non-blocking UDP broadcast
(with-io-scope
  (let [r (acquire! (bare-udp nil
                              :local-port 43214
                              :broadcast? true
                              :non-blocking? true
                              :batch-size 100))]
    (println! (config r))

    (thread
     (io! (let [x (read! r)
                ir (atom [])]
            (loop [r (reduce #(do (alter! ir conj %2)
                                  (inc %)) 0 x)]
                (cond (postponed? r)
                      (do (println! "got postponed" @r @ir)
                          (reset! ir [])
                          (sleep 500) ;; better to use selector here
                          (recur (unsafe-advance! r)))
                      :else
                      (println! "got final" r @ir))))))

    (write! r [(datagram "192.168.1.255" 43214 [0 1 2])])
    (write! r [(datagram "192.168.1.255" 43214 [3 4 5])])
    (sleep 1000)
    (write! r [(datagram "192.168.1.255" 43214 [6 7 8 9 10])])

    (sleep 2000)))
;; {:uri #uri "bare-udp://0.0.0.0:43214?bs=100&nb=true&broadcast=true" :protocol-family :inet :local-address nil :local-port 43214 :batch-size 100 :reuse? nil :broadcast? true :tos nil :in-buffer-size nil :out-buffer-size nil :multicast-if nil :multicast-ttl nil :multicast-loop true :payload-fn #<udp$payload__GT_array_coll dunaj.resource.udp$payload__GT_array_coll@406bf9b6> :selector-provider #<EPollSelectorProvider sun.nio.ch.EPollSelectorProvider@421c10c> :non-blocking? true}
;; got postponed  2 [{:address 192.168.1.101 :port 43214 :payload (0 1 2)} {:address 192.168.1.101 :port 43214 :payload (3 4 5)}]
;; got postponed  2 []
;; got postponed  3 [{:address 192.168.1.101 :port 43214 :payload (6 7 8 9 10)}]
;; got postponed  3 []
;; got postponed  3 []
;; got postponed  3 []
;; got final  3 []
;;=> nil

bare-udp-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

A connectionless UDP resource factory. Current options are:

  • :uri - resource URI

  • :protocol-family - :inet (default) or :inet6

  • :local-address - nil or string

  • :local-port - nil or integer

  • :batch-size - batch size, low level

  • :reuse? - reuse address?. boolean

  • :broadcast? - udp broadcast?, boolean, default false

  • :tos - tos

  • :in-buffer-size - nil or integer

  • :out-buffer-size - nil or integer

  • :multicast-if - multicast interface

  • :multicast-ttl - multicast ttl

  • :multicast-loop - multicast loopback, boolean, default true

  • :payload-fn - takes received payload as a batch and produces reducible collection that has no connection to given batch.

  • :selector-provider - host selector provider

  • :non-blocking? - use non-blocking mode?, boolean

block!

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (block! multicast address)

Type signature:
  • (IMulticast ⨯ String+) → nil

Blocks given address for multicast resource. Resource must not be source specific.

See also: unblock!, multicast

datagram

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (datagram address port payload)

Type signature:
  • ((Maybe String+) ⨯ (Maybe Integer+) ⨯ (U AnyBatch IRed)) → UdpDatagram

Returns a new datagram containing given address, port and payload.

See also: bare-udp

multicast

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (multicast uri & {:as opts})

Type signature:
  • ((U nil String+ Uri) ⨯ Any) → IAcquirableFactory

Returns multicast UDP resource factory with given uri and opts set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.resource.udp :refer [multicast]]
            [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.repl :refer [pst]]))

(with-io-scope
  (let [r (acquire! (multicast nil
                               :local-port 43214
                               :multicast-group "224.0.0.104"
                               :network-interface
                               (java.net.NetworkInterface/getByName "eth0")))]
    (println! (config r))
    (future
      (println! "monitor enter")
      (io!
       (try
         (dored [x (read! r)] (println! "received" x))
         (catch java.nio.channels.AsynchronousCloseException e
           (println! "monitor exit"))
         (catch java.lang.Exception e
           (println! "exception in monitor " (pst e))))))

    (write! r [[-1 -2 -3 -4]])
    (write! r [(datagram "224.0.0.104" 43214 [1 2 3 4 5])])

    (sleep 200)))
;; {:uri #uri "multicast://eth0:43214@224.0.0.104" :protocol-family :inet :multicast-group 224.0.0.104 :sources [] :network-interface #<NetworkInterface name:eth0 (eth0)> :local-port 43214 :batch-size nil :tos nil :in-buffer-size nil :out-buffer-size nil :multicast-if nil :multicast-ttl nil :multicast-loop true :payload-fn #<udp$payload__GT_array_coll dunaj.resource.udp$payload__GT_array_coll@406bf9b6> :selector-provider #<EPollSelectorProvider sun.nio.ch.EPollSelectorProvider@421c10c> :non-blocking? false}
;; monitor enter
;; received {:address 192.168.1.101 :port 43214 :payload (-1 -2 -3 -4)}
;; received {:address 192.168.1.101 :port 43214 :payload (1 2 3 4 5)}
;; monitor exit
;;=> nil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.concurrent.thread :refer [sleep]]))

(with-io-scope
  (let [factory (resource "multicast://eth0:43214@224.0.0.104")
        r (acquire! (assoc factory :non-blocking? true))
        sel (acquire! (selector))
        prf #(reduce conj [] (read! (:resource %)))
        rf #(println! "got" %2 "\ndatagrams" @(prf %2))]
    (register! sel r [:read])
    (thread (io! (loop [x (reduce rf nil (read! sel))]
                   (if (postponed? x)
                     (do (println! "ready" (select sel))
                         (recur (unsafe-advance! x)))
                     (println! "end")))))

    (write! r [[1 2 3] [4 5 6]])
    (sleep 100)
    (write! r [[7 8 9]])
    (sleep 500)))
;; ready 1
;; got {:ready (:read) :resource #<MulticastResource dunaj.resource.udp.MulticastResource@7c560cb5>}
;; datagrams [{:address 192.168.1.101 :port 43214 :payload (1 2 3)} {:address 192.168.1.101 :port 43214 :payload (4 5 6)}]
;; ready 1
;; got {:ready (:read) :resource #<MulticastResource dunaj.resource.udp.MulticastResource@7c560cb5>}
;; datagrams [{:address 192.168.1.101 :port 43214 :payload (7 8 9)}]
;; ready 0
;; end
;;=>nil

multicast-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

A resource factory for multicast. Current options are:

  • :uri - resource URI

  • :protocol-family - :inet (default) or :inet6

  • :multicast-group - string

  • :sources - nil or collection of sources

  • :network-interface - host network interface

  • :local-port - nil or integer

  • :batch-size - batch size, low level

  • :tos - tos

  • :in-buffer-size - nil or integer

  • :out-buffer-size - nil or integer

  • :multicast-if - multicast interface

  • :multicast-ttl - multicast ttl

  • :multicast-loop - multicast loopback, boolean, default true

  • :payload-fn - takes received payload as a batch and produces reducible collection that has no connection to given batch.

  • :selector-provider - host selector provider

  • :non-blocking? - use non-blocking mode?, boolean

udp

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (udp uri & {:as opts})

Type signature:
  • ((U nil String+ Uri) ⨯ Any) → IAcquirableFactory

Returns UDP resource factory with given uri and opts set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.resource.udp :refer [udp]]
            [dunaj.concurrent.thread :refer [sleep]]
            [dunaj.repl :refer [pst]]))

(with-io-scope
  (let [r (acquire! (udp nil
                         :local-port 43214
                         :remote-port 43214
                         :remote-address "localhost"))]
    (println! (config r))
    (future
      (io!
       (try
         (dored [x (read! r)] (println! "received" x))
         (catch java.nio.channels.AsynchronousCloseException e
           (println! "monitor exit"))
         (catch java.lang.Exception e
           (println!
            "exception in monitor " (pst e))))))

    (write! r [[-1 -2 -3 -4]])
    (write! r [[1 2 3 4 5]])

    (sleep 200)))
;; {:uri #uri "udp://0.0.0.0:43214@localhost:43214" :protocol-family :inet :remote-address localhost :remote-port 43214 :local-address nil :local-port 43214 :batch-size nil :reuse? nil :broadcast? false :tos nil :in-buffer-size nil :out-buffer-size nil :multicast-if nil :multicast-ttl nil :multicast-loop true :payload-fn #<udp$payload__GT_array_coll dunaj.resource.udp$payload__GT_array_coll@406bf9b6> :selector-provider #<EPollSelectorProvider sun.nio.ch.EPollSelectorProvider@421c10c> :non-blocking? false}
;; received (-1 -2 -3 -4)
;; received (1 2 3 4 5)
;; monitor exit
;;=> nil

udp-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

An UDP resource factory. Current options are:

  • :uri - resource URI

  • :protocol-family - :inet (default) or :inet6

  • :remote-address - nil or string

  • :remote-port - nil or integer

  • :local-address - nil or string

  • :local-port - nil or integer

  • :batch-size - batch size, low level

  • :reuse? - reuse address?. boolean

  • :broadcast? - udp broadcast?, boolean, default false

  • :tos - tos

  • :in-buffer-size - nil or integer

  • :out-buffer-size - nil or integer

  • :multicast-if - multicast interface

  • :multicast-ttl - multicast ttl

  • :multicast-loop - multicast loopback, boolean, default true

  • :payload-fn - takes received payload as a batch and produces reducible collection that has no connection to given batch.

  • :selector-provider - host selector provider

  • :non-blocking? - use non-blocking mode?, boolean

unblock!

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (unblock! multicast address)

Type signature:
  • (IMulticast ⨯ String+) → nil

Unblocks given address from multicast resource. Throws if address is not blocked.

See also: block!, multicast