Skip to content

Commit 43b3103

Browse files
authored
Merge pull request #52 from JuliaRobotics/tk/store-channel-name
Store channel name in SubscriptionOptions
2 parents fc5b211 + c16de4d commit 43b3103

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/core.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function decode end
88
struct SubscriptionOptions{T, F}
99
msgtype::Type{T}
1010
handler::F
11+
channel::String
1112
end
1213

1314
struct Subscription{T <: SubscriptionOptions}
@@ -135,23 +136,31 @@ struct RecvBuf
135136
lcm::Ptr{Cvoid}
136137
end
137138

138-
function onresponse(rbuf::RecvBuf, channelbytes::Ptr{UInt8}, opts::SubscriptionOptions{T}) where T
139-
channel = unsafe_string(channelbytes)
139+
function check_channel_name(channelptr::Ptr{UInt8}, opts::SubscriptionOptions)
140+
i = 1
141+
while (byte = unsafe_load(channelptr, i)) != 0x00
142+
byte == codeunit(opts.channel, i) || error("Mismatch between received channel name and subscription channel name")
143+
i += 1
144+
end
145+
end
146+
147+
function onresponse(rbuf::RecvBuf, channelptr::Ptr{UInt8}, opts::SubscriptionOptions{T}) where T
148+
check_channel_name(channelptr, opts)
140149
msgdata = unsafe_wrap(Vector{UInt8}, rbuf.data, rbuf.data_size)
141150
if T === Nothing
142-
opts.handler(channel, msgdata)
151+
opts.handler(opts.channel, msgdata)
143152
else
144153
msg = decode(msgdata, opts.msgtype)
145-
opts.handler(channel, msg)
154+
opts.handler(opts.channel, msg)
146155
end
147156
return nothing
148157
end
149158

150-
function subscribe(lcm::LCM, channel::String, options::T) where T <: SubscriptionOptions
159+
function subscribe(lcm::LCM, options::T) where T <: SubscriptionOptions
151160
csubscription = ccall((:lcm_subscribe, liblcm), Ptr{Cvoid},
152161
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{Cvoid}, Ptr{Cvoid}),
153162
lcm,
154-
channel,
163+
options.channel,
155164
@cfunction(onresponse, Cvoid, (Ref{RecvBuf}, Ptr{UInt8}, Ref{T})),
156165
Ref(options))
157166
sub = Subscription(options, csubscription)
@@ -160,7 +169,7 @@ function subscribe(lcm::LCM, channel::String, options::T) where T <: Subscriptio
160169
end
161170

162171
function subscribe(lcm::LCM, channel::String, handler, msgtype=Nothing)
163-
subscribe(lcm, channel, SubscriptionOptions(msgtype, handler))
172+
subscribe(lcm, SubscriptionOptions(msgtype, handler, channel))
164173
end
165174

166175
function unsubscribe(lcm::LCM, subscription::Subscription)

src/readlog.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function handle(lcmlog::LCMLog)::Bool
102102
end
103103

104104
function subscribe(lcmlog::LCMLog, channel::S, callback::F, msgtype=Nothing) where {S <: AbstractString, F <: Function}
105-
opts = LCMCore.LCMCore.SubscriptionOptions(msgtype, callback)
105+
opts = SubscriptionOptions(msgtype, callback, channel)
106106
lcmlog.subscriptions[channel] = opts
107107
nothing
108108
end

0 commit comments

Comments
 (0)