From 838956b18de63c6a5b3e7bdcf9063c0b90a6ccf6 Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 09:37:53 -0400 Subject: [PATCH 1/8] examples file --- examples/LCMproto.jl | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 examples/LCMproto.jl diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl new file mode 100644 index 0000000..8fa6188 --- /dev/null +++ b/examples/LCMproto.jl @@ -0,0 +1,99 @@ +# This is LCMproto.jl +# https://github.com/Netflix/SimianArmy/wiki/Chaos-Monkey + +addprocs(1) +@everywhere begin +using ProtoBuf +using LCMCore +using JSON + +import LCMCore: encode, decode + + +type Nested + more::Float64 +end + +abstract DoubleWammy{T} + +type MyMessageType{T} <: DoubleWammy{T} # a Julia composite type + intval::Int # probably generated from protoc + strval::ASCIIString + len::Int64 + data::Vector{UInt8} + # ndd::Nested + dd::T + MyMessageType() = new() + MyMessageType(i,s,l,d,o) = new(i,s,l,d,o) +end +# Dict{ASCIIString,Vector{Float64}} + + +# MyMessageType(10, "hello world") + +function encode{T}(msg::MyMessageType{T}) + iob = PipeBuffer() + js = JSON.json(msg.dd) + writeproto(iob, MyMessageType{ASCIIString}(msg.intval, msg.strval, msg.len, msg.data, js) ) + enc = takebuf_array(iob) + return enc +end + +function decode{T <: Dict}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) + iob = PipeBuffer(data) + rmsg = readproto(iob, MyMessageType{ASCIIString}()) # read it back into another instance + js = JSON.parse(rmsg.dd) + MyMessageType{T}(rmsg.intval, rmsg.strval, rmsg.len, rmsg.data, js) +end + + +function typed_callback{T}(channel::AbstractString, msg::MyMessageType{T}) + @show channel + @show typeof(msg) +end + + +typealias MDD Dict{ASCIIString,Vector{Float64}} +end + +@everywhere function listento(MYCHAN) + lc = LCM() + subscribe(lc, MYCHAN, typed_callback, MyMessageType{MDD}) + + flag = Bool[true] + @async begin + while flag[1] + @time handle(lc) + end + end +end + + +r = @spawn listento("MY_CHANNEL") + +lc = LCM() + +for i in 1:1000 + dd = MDD("$i" => randn(100)) + mymsg = MyMessageType{MDD}(i,"hello world",1000000,Array(UInt8,1000000),dd) + # nn = Nested(100.0-i, Dict{AbstractString,Vector{Float64}}("$i" => randn(10))) + # mymsg = MyMessageType(i,"hello world",100,Array(UInt8,100),nn) + @time publish(lc, "MY_CHANNEL", mymsg) + sleep(0.0001) +end + + +flag[1] = false + + + +# Some testing +i = 10 +dd = MDD("$i" => randn(10)) +# nn = Nested(100.0-i, dd) +mymsg = MyMessageType{MDD}(i,"hello world",100,Array(UInt8,100),dd) + + +enc = encode(mymsg) + +md = decode(enc, MyMessageType{MDD}) From 12da7794e502dc2587ea2c8aff2f6222b7b6f165 Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 11:13:45 -0400 Subject: [PATCH 2/8] improve example --- examples/LCMproto.jl | 108 +++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl index 8fa6188..fd4640e 100644 --- a/examples/LCMproto.jl +++ b/examples/LCMproto.jl @@ -2,39 +2,39 @@ # https://github.com/Netflix/SimianArmy/wiki/Chaos-Monkey addprocs(1) + @everywhere begin -using ProtoBuf using LCMCore +using ProtoBuf using JSON import LCMCore: encode, decode +typealias MDD Dict{ASCIIString,Vector{UInt8}} + type Nested more::Float64 + data::Vector{UInt8} + Nested() = new() + Nested(a,b) = new(a,b) end -abstract DoubleWammy{T} - -type MyMessageType{T} <: DoubleWammy{T} # a Julia composite type +type MyMessageType{T} # a Julia composite type intval::Int # probably generated from protoc strval::ASCIIString len::Int64 - data::Vector{UInt8} - # ndd::Nested + data::Vector{Float64} + ndd::Nested dd::T MyMessageType() = new() - MyMessageType(i,s,l,d,o) = new(i,s,l,d,o) + MyMessageType{T}(i,s,l,d,nd,o::T) = new(i,s,l,d,nd,o) end -# Dict{ASCIIString,Vector{Float64}} - - -# MyMessageType(10, "hello world") function encode{T}(msg::MyMessageType{T}) iob = PipeBuffer() js = JSON.json(msg.dd) - writeproto(iob, MyMessageType{ASCIIString}(msg.intval, msg.strval, msg.len, msg.data, js) ) + writeproto(iob, MyMessageType{ASCIIString}(msg.intval, msg.strval, msg.len, msg.data, msg.ndd, js) ) enc = takebuf_array(iob) return enc end @@ -43,57 +43,85 @@ function decode{T <: Dict}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) iob = PipeBuffer(data) rmsg = readproto(iob, MyMessageType{ASCIIString}()) # read it back into another instance js = JSON.parse(rmsg.dd) - MyMessageType{T}(rmsg.intval, rmsg.strval, rmsg.len, rmsg.data, js) + MyMessageType{T}(rmsg.intval, rmsg.strval, rmsg.len, rmsg.data, rmsg.ndd, js) end function typed_callback{T}(channel::AbstractString, msg::MyMessageType{T}) - @show channel - @show typeof(msg) + @show channel + @show typeof(msg) + nothing end -typealias MDD Dict{ASCIIString,Vector{Float64}} -end - -@everywhere function listento(MYCHAN) +function listento(MYCHAN, flag::Vector{Bool}) lc = LCM() subscribe(lc, MYCHAN, typed_callback, MyMessageType{MDD}) - flag = Bool[true] - @async begin while flag[1] - @time handle(lc) - end + @time handle(lc) end end +end #@everywhere + + + +function testencdec() + i = 10 + dd = MDD("$i" => Array(UInt8,100)) + nn = Nested(100.0-i, Array(UInt8,1000000)) + mymsg = MyMessageType{MDD}(i,"hello world",10,randn(100),nn,dd) + enc = encode(mymsg) + md = decode(enc, MyMessageType{MDD}) + norm(mymsg.data-md.data) < 1e-10 +end + + +testencdec() -r = @spawn listento("MY_CHANNEL") -lc = LCM() -for i in 1:1000 - dd = MDD("$i" => randn(100)) - mymsg = MyMessageType{MDD}(i,"hello world",1000000,Array(UInt8,1000000),dd) - # nn = Nested(100.0-i, Dict{AbstractString,Vector{Float64}}("$i" => randn(10))) - # mymsg = MyMessageType(i,"hello world",100,Array(UInt8,100),nn) - @time publish(lc, "MY_CHANNEL", mymsg) - sleep(0.0001) +function publishmanymsgs(channel::AbstractString; iter::Int=100) + lc = LCM() + + for i in 1:iter + dd = MDD("$i" => Array(UInt8,10)) + nn = Nested(100.0-i, Array(UInt8,1000000)) + mymsg = MyMessageType{MDD}(i,"hello world",7,randn(100),nn,dd) + @time publish(lc, channel, mymsg) + sleep(0.0001) + end + nothing end -flag[1] = false +channel = "MY_CHANNEL" + + +# Run listener on separate process +loopflag = Bool[true] +r = @spawn listento(channel, loopflag) # or @spawn for co-routine using loopflag + +# sender +publishmanymsgs(channel, iter=999) + +# to stop while loop +loopflag[1] = false +publishmanymsgs(channel, iter=1) + + + + + + + + + -# Some testing -i = 10 -dd = MDD("$i" => randn(10)) -# nn = Nested(100.0-i, dd) -mymsg = MyMessageType{MDD}(i,"hello world",100,Array(UInt8,100),dd) -enc = encode(mymsg) -md = decode(enc, MyMessageType{MDD}) +# From 1cd07cefcaf4614cd24fd0d110b6a82d8d4cfa4b Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 11:22:22 -0400 Subject: [PATCH 3/8] typo --- examples/LCMproto.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl index fd4640e..2ce0fe7 100644 --- a/examples/LCMproto.jl +++ b/examples/LCMproto.jl @@ -101,7 +101,7 @@ channel = "MY_CHANNEL" # Run listener on separate process loopflag = Bool[true] -r = @spawn listento(channel, loopflag) # or @spawn for co-routine using loopflag +r = @spawn listento(channel, loopflag) # or @async for co-routine using loopflag # sender publishmanymsgs(channel, iter=999) From 822a31ec90d3bef1860448c756c981dadf624d36 Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 11:24:32 -0400 Subject: [PATCH 4/8] unnecessary constraint --- examples/LCMproto.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl index 2ce0fe7..8d92f0f 100644 --- a/examples/LCMproto.jl +++ b/examples/LCMproto.jl @@ -39,7 +39,7 @@ function encode{T}(msg::MyMessageType{T}) return enc end -function decode{T <: Dict}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) +function decode{T}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) iob = PipeBuffer(data) rmsg = readproto(iob, MyMessageType{ASCIIString}()) # read it back into another instance js = JSON.parse(rmsg.dd) From 8ec438b5e8c9d7031ce13ef33d1c2897dde2e84f Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 11:29:05 -0400 Subject: [PATCH 5/8] and better init compiling --- examples/LCMproto.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl index 8d92f0f..0d157eb 100644 --- a/examples/LCMproto.jl +++ b/examples/LCMproto.jl @@ -63,10 +63,6 @@ function listento(MYCHAN, flag::Vector{Bool}) end end -end #@everywhere - - - function testencdec() i = 10 dd = MDD("$i" => Array(UInt8,100)) @@ -77,8 +73,11 @@ function testencdec() norm(mymsg.data-md.data) < 1e-10 end +end #@everywhere + +# compile code everywhere +@everywhere testencdec() -testencdec() From 79dc3801d4f138c97b1a6418ff0094b874915192 Mon Sep 17 00:00:00 2001 From: dehann Date: Thu, 23 Mar 2017 12:10:35 -0400 Subject: [PATCH 6/8] adding forward backward compatibility example --- examples/LCMproto.jl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl index 0d157eb..95743bc 100644 --- a/examples/LCMproto.jl +++ b/examples/LCMproto.jl @@ -21,7 +21,7 @@ type Nested end type MyMessageType{T} # a Julia composite type - intval::Int # probably generated from protoc + intval::Int # probably generated from protoc strval::ASCIIString len::Int64 data::Vector{Float64} @@ -31,6 +31,20 @@ type MyMessageType{T} # a Julia composite type MyMessageType{T}(i,s,l,d,nd,o::T) = new(i,s,l,d,nd,o) end +# Record a log and now try subscribing to that log with a modified type +# type MyMessageType{T} <: ForwBackCompatability{T} # a Julia composite type +# intval::Int # probably generated from protoc +# strval::ASCIIString +# len::Int64 +# data::Vector{Float64} +# ndd::Nested +# dd::T +# oops::Vector{Float64} +# MyMessageType() = new() +# MyMessageType{T}(i,s,l,d,nd,o::T, +# b::Vector{Float64}=Float64[] ) = new(i,s,l,d,nd,o,b) +# end + function encode{T}(msg::MyMessageType{T}) iob = PipeBuffer() js = JSON.json(msg.dd) From 066e81dbbe613441e0c5f82dedc574e8262d5625 Mon Sep 17 00:00:00 2001 From: dehann Date: Sun, 21 Jan 2018 21:46:39 -0500 Subject: [PATCH 7/8] removed example file --- examples/LCMproto.jl | 140 ------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100644 examples/LCMproto.jl diff --git a/examples/LCMproto.jl b/examples/LCMproto.jl deleted file mode 100644 index 95743bc..0000000 --- a/examples/LCMproto.jl +++ /dev/null @@ -1,140 +0,0 @@ -# This is LCMproto.jl -# https://github.com/Netflix/SimianArmy/wiki/Chaos-Monkey - -addprocs(1) - -@everywhere begin -using LCMCore -using ProtoBuf -using JSON - -import LCMCore: encode, decode - - -typealias MDD Dict{ASCIIString,Vector{UInt8}} - -type Nested - more::Float64 - data::Vector{UInt8} - Nested() = new() - Nested(a,b) = new(a,b) -end - -type MyMessageType{T} # a Julia composite type - intval::Int # probably generated from protoc - strval::ASCIIString - len::Int64 - data::Vector{Float64} - ndd::Nested - dd::T - MyMessageType() = new() - MyMessageType{T}(i,s,l,d,nd,o::T) = new(i,s,l,d,nd,o) -end - -# Record a log and now try subscribing to that log with a modified type -# type MyMessageType{T} <: ForwBackCompatability{T} # a Julia composite type -# intval::Int # probably generated from protoc -# strval::ASCIIString -# len::Int64 -# data::Vector{Float64} -# ndd::Nested -# dd::T -# oops::Vector{Float64} -# MyMessageType() = new() -# MyMessageType{T}(i,s,l,d,nd,o::T, -# b::Vector{Float64}=Float64[] ) = new(i,s,l,d,nd,o,b) -# end - -function encode{T}(msg::MyMessageType{T}) - iob = PipeBuffer() - js = JSON.json(msg.dd) - writeproto(iob, MyMessageType{ASCIIString}(msg.intval, msg.strval, msg.len, msg.data, msg.ndd, js) ) - enc = takebuf_array(iob) - return enc -end - -function decode{T}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) - iob = PipeBuffer(data) - rmsg = readproto(iob, MyMessageType{ASCIIString}()) # read it back into another instance - js = JSON.parse(rmsg.dd) - MyMessageType{T}(rmsg.intval, rmsg.strval, rmsg.len, rmsg.data, rmsg.ndd, js) -end - - -function typed_callback{T}(channel::AbstractString, msg::MyMessageType{T}) - @show channel - @show typeof(msg) - nothing -end - - -function listento(MYCHAN, flag::Vector{Bool}) - lc = LCM() - subscribe(lc, MYCHAN, typed_callback, MyMessageType{MDD}) - - while flag[1] - @time handle(lc) - end -end - -function testencdec() - i = 10 - dd = MDD("$i" => Array(UInt8,100)) - nn = Nested(100.0-i, Array(UInt8,1000000)) - mymsg = MyMessageType{MDD}(i,"hello world",10,randn(100),nn,dd) - enc = encode(mymsg) - md = decode(enc, MyMessageType{MDD}) - norm(mymsg.data-md.data) < 1e-10 -end - -end #@everywhere - -# compile code everywhere -@everywhere testencdec() - - - - -function publishmanymsgs(channel::AbstractString; iter::Int=100) - lc = LCM() - - for i in 1:iter - dd = MDD("$i" => Array(UInt8,10)) - nn = Nested(100.0-i, Array(UInt8,1000000)) - mymsg = MyMessageType{MDD}(i,"hello world",7,randn(100),nn,dd) - @time publish(lc, channel, mymsg) - sleep(0.0001) - end - nothing -end - - -channel = "MY_CHANNEL" - - -# Run listener on separate process -loopflag = Bool[true] -r = @spawn listento(channel, loopflag) # or @async for co-routine using loopflag - -# sender -publishmanymsgs(channel, iter=999) - -# to stop while loop -loopflag[1] = false -publishmanymsgs(channel, iter=1) - - - - - - - - - - - - - - - -# From 8c35626a5bddfbe4909df0dc8fe856bca8cab7cc Mon Sep 17 00:00:00 2001 From: dehann Date: Sun, 21 Jan 2018 21:49:38 -0500 Subject: [PATCH 8/8] add own serialization example --- example/LCMproto.jl | 140 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 example/LCMproto.jl diff --git a/example/LCMproto.jl b/example/LCMproto.jl new file mode 100644 index 0000000..95743bc --- /dev/null +++ b/example/LCMproto.jl @@ -0,0 +1,140 @@ +# This is LCMproto.jl +# https://github.com/Netflix/SimianArmy/wiki/Chaos-Monkey + +addprocs(1) + +@everywhere begin +using LCMCore +using ProtoBuf +using JSON + +import LCMCore: encode, decode + + +typealias MDD Dict{ASCIIString,Vector{UInt8}} + +type Nested + more::Float64 + data::Vector{UInt8} + Nested() = new() + Nested(a,b) = new(a,b) +end + +type MyMessageType{T} # a Julia composite type + intval::Int # probably generated from protoc + strval::ASCIIString + len::Int64 + data::Vector{Float64} + ndd::Nested + dd::T + MyMessageType() = new() + MyMessageType{T}(i,s,l,d,nd,o::T) = new(i,s,l,d,nd,o) +end + +# Record a log and now try subscribing to that log with a modified type +# type MyMessageType{T} <: ForwBackCompatability{T} # a Julia composite type +# intval::Int # probably generated from protoc +# strval::ASCIIString +# len::Int64 +# data::Vector{Float64} +# ndd::Nested +# dd::T +# oops::Vector{Float64} +# MyMessageType() = new() +# MyMessageType{T}(i,s,l,d,nd,o::T, +# b::Vector{Float64}=Float64[] ) = new(i,s,l,d,nd,o,b) +# end + +function encode{T}(msg::MyMessageType{T}) + iob = PipeBuffer() + js = JSON.json(msg.dd) + writeproto(iob, MyMessageType{ASCIIString}(msg.intval, msg.strval, msg.len, msg.data, msg.ndd, js) ) + enc = takebuf_array(iob) + return enc +end + +function decode{T}(data::Vector{UInt8}, ::Type{MyMessageType{T}}) + iob = PipeBuffer(data) + rmsg = readproto(iob, MyMessageType{ASCIIString}()) # read it back into another instance + js = JSON.parse(rmsg.dd) + MyMessageType{T}(rmsg.intval, rmsg.strval, rmsg.len, rmsg.data, rmsg.ndd, js) +end + + +function typed_callback{T}(channel::AbstractString, msg::MyMessageType{T}) + @show channel + @show typeof(msg) + nothing +end + + +function listento(MYCHAN, flag::Vector{Bool}) + lc = LCM() + subscribe(lc, MYCHAN, typed_callback, MyMessageType{MDD}) + + while flag[1] + @time handle(lc) + end +end + +function testencdec() + i = 10 + dd = MDD("$i" => Array(UInt8,100)) + nn = Nested(100.0-i, Array(UInt8,1000000)) + mymsg = MyMessageType{MDD}(i,"hello world",10,randn(100),nn,dd) + enc = encode(mymsg) + md = decode(enc, MyMessageType{MDD}) + norm(mymsg.data-md.data) < 1e-10 +end + +end #@everywhere + +# compile code everywhere +@everywhere testencdec() + + + + +function publishmanymsgs(channel::AbstractString; iter::Int=100) + lc = LCM() + + for i in 1:iter + dd = MDD("$i" => Array(UInt8,10)) + nn = Nested(100.0-i, Array(UInt8,1000000)) + mymsg = MyMessageType{MDD}(i,"hello world",7,randn(100),nn,dd) + @time publish(lc, channel, mymsg) + sleep(0.0001) + end + nothing +end + + +channel = "MY_CHANNEL" + + +# Run listener on separate process +loopflag = Bool[true] +r = @spawn listento(channel, loopflag) # or @async for co-routine using loopflag + +# sender +publishmanymsgs(channel, iter=999) + +# to stop while loop +loopflag[1] = false +publishmanymsgs(channel, iter=1) + + + + + + + + + + + + + + + +#