src/Comms.jl

Wed, 11 Dec 2019 18:45:34 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 11 Dec 2019 18:45:34 +0200
changeset 5
015025cf2a50
child 6
a5c1eb932d19
permissions
-rw-r--r--

Add Channel comms helper module

5
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #########################################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Helpers for communication via channels
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #########################################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 module Comms
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 __precompile__()
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 ##############
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 # Our exports
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 ##############
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 export process_channel,
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 put_onlylatest!
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 ####################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 # Channel iteration
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 ####################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 function process_channel(fn, rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 while true
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 d=take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 # Take only the latest image to visualise
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 while isready(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 d=take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 # We're done if we were fed nothing
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 if isnothing(d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 break
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 try
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 fn(d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 catch ex
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 error("Exception in process_channel handler. Terminating.\n$(ex)")
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 throw(ex)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 #############################################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 # Ensure only latest data is in a Channel(1)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 #############################################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 function put_onlylatest!(rc, d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 while isready(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 put!(rc, d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 end # Module

mercurial