Skip to content

Commit

Permalink
Merge pull request #13 from rdeits/handle-timeout
Browse files Browse the repository at this point in the history
add optional timeout to handle()
  • Loading branch information
rdeits authored Jan 9, 2017
2 parents f74bb6a + daf2d2b commit e9aa77b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ notifications:
branches:
only:
- master
matrix:
allow_failures:
- julia: nightly

after_success:
- julia -e 'cd(Pkg.dir("PyLCM")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
26 changes: 24 additions & 2 deletions src/PyLCM.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
VERSION >= v"0.4.0-dev+6521" && __precompile__()
__precompile__()

module PyLCM

using Base.Dates: Period, Millisecond
using PyCall
export LCM, publish, subscribe, handle, @pyimport

Expand All @@ -25,8 +26,29 @@ function subscribe(lc::LCM, channel::AbstractString, handler::Function, msg_type
lc.lcm_obj[:subscribe](channel, pyeval("lambda chan, data, handler=h, msg_type=t: handler(chan, msg_type.decode(data))", h=handler, t=msg_type))
end

"Wait for and dispatch the next incoming message"
function handle(lc::LCM)
pycall(lc.lcm_obj[:handle], PyObject)
pycall(lc.lcm_obj[:handle], PyObject)
true
end

"""
handle(lc, timeout)
Wait for and dispatch the next incoming message, with a timeout expressed
as any Base.Dates.Period type. For example:
handle(lc, Millisecond(10))
or
handle(lc, Second(1))
Returns true if a message was handled, false if the function timed out.
"""
function handle(lc::LCM, timeout::Period)
timeout_ms = convert(Int, convert(Millisecond, timeout))
convert(Bool, pycall(lc.lcm_obj[:handle_timeout], PyObject, timeout_ms))
end

function __init__()
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

subscribe(lc, "TEST", handle_msg, pylcm_test_message.multidimensional_array_t)
publish(lc, "TEST", msg)
handle(lc)
@test handle(lc)
@test got_message

global decoded_message = false
Expand All @@ -54,5 +54,5 @@ end

subscribe(lc, "TEST_DECODE", decode_and_handle_msg)
publish(lc, "TEST_DECODE", msg)
handle(lc)
@test handle(lc, Dates.Second(1))
@test decoded_message

0 comments on commit e9aa77b

Please sign in to comment.