src/Comms.jl

Fri, 27 Dec 2019 22:04:49 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 27 Dec 2019 22:04:49 +0200
changeset 7
900a7e18ca01
parent 6
a5c1eb932d19
permissions
-rw-r--r--

optimise

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,
6
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
14 put_onlylatest!,
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
15 put_unless_closed!
5
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 ####################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 # Channel iteration
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
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 function process_channel(fn, rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 while true
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 d=take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 # Take only the latest image to visualise
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 while isready(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 d=take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 # We're done if we were fed nothing
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 if isnothing(d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 break
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 try
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 fn(d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 catch ex
6
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
35 error("Exception in process_channel handler. Terminating.\n")
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
36 rethrow(ex)
5
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 end
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 #############################################
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 # Ensure only latest data is in a Channel(1)
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
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 function put_onlylatest!(rc, d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 while isready(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 take!(rc)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 put!(rc, d)
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 end
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51
6
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
52 ############################################
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
53 # Cracefully return false if channel closed
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
54 ############################################
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
55
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
56 function put_unless_closed!(rc, d)
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
57 try
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
58 put!(rc, d)
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
59 catch ex
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
60 if isa(ex, InvalidStateException) && ex.state==:closed
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
61 return false
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
62 else
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
63 rethrow(ex)
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
64 end
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
65 end
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
66 return true
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
67 end
a5c1eb932d19 put_unless_closed!
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
68
5
015025cf2a50 Add Channel comms helper module
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end # Module

mercurial