Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs related to shell requests #49

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/gw.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ julia> shell = agentforservice(gw, "org.arl.fjage.shell.Services.SHELL")
shell
julia> shell.language
"Groovy"
julia> request(gw, ShellExecReq(recipient=shell, cmd="ps"))
julia> request(gw, ShellExecReq(recipient=shell, command="ps"))
AGREE
julia> request(shell, ShellExecReq(cmd="ps"))
julia> request(shell, ShellExecReq(command="ps"))
AGREE
julia> shell << ShellExecReq(cmd="ps")
julia> shell << ShellExecReq(command="ps")
AGREE
julia> close(gw)
```
Expand Down
4 changes: 1 addition & 3 deletions docs/src/msg.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ message classes with fields with the same name as the keys in the message.
Message types are defined using the `@message` macro. For example:
```julia
@message "org.arl.fjage.shell.ShellExecReq" struct ShellExecReq
cmd::Union{String,Nothing} = nothing
script::Union{String,Nothing} = nothing
args::Vector{String} = String[]
command::Union{String,Nothing} = nothing
ans::Bool = false
end
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dependency>
<groupId>com.github.org-arl</groupId>
<artifactId>fjage</artifactId>
<version>1.12.2</version>
<version>1.15.1</version>
</dependency>
</dependencies>
</project>
4 changes: 1 addition & 3 deletions src/const.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ end

"Shell command execution request message."
@message "org.arl.fjage.shell.ShellExecReq" struct ShellExecReq
cmd::Union{String,Nothing} = nothing
script::Union{String,Nothing} = nothing
args::Vector{String} = String[]
command::Union{String,Nothing} = nothing
ans::Bool = false
end
18 changes: 14 additions & 4 deletions src/msg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ the performative is assumed to be REQUEST, and for all other messages, INFORM.

```julia-repl
julia> @message "org.arl.fjage.shell.MyShellExecReq" struct MyShellExecReq
cmd::String
command::String
end
julia> req = MyShellExecReq(cmd="ps")
MyShellExecReq:REQUEST[cmd:"ps"]
julia> req = MyShellExecReq(command="ps")
MyShellExecReq:REQUEST[command:"ps"]
```
"""
macro message(classname, perf, sdef)
Expand Down Expand Up @@ -320,8 +320,18 @@ Base.length(s::GenericMessage) = fieldcount(typeof(s)) - 1 + length(s.__data__)

function Base.iterate(s::GenericMessage)
f = keys(s)
fiter = iterate(f)
fiter === nothing && return nothing
v = values(s)
(f[1] => v[1], (f[2:end], v[2:end]))
(fiter[1] => first(v), (fiter[2], 2))
end

function Base.iterate(s::GenericMessage, (fstate, vndx))
f = keys(s)
fiter = iterate(f, fstate)
fiter === nothing && return nothing
v = values(s)
(fiter[1] => v[vndx], (fiter[2], vndx+1))
end

"""
Expand Down
34 changes: 17 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ try
channel = Channel()
@async put!(channel, receive(gw, 1000))
yield()
send(gw, ShellExecReq(recipient=shell, cmd="1+2"))
send(gw, ShellExecReq(recipient=shell, command="1+2"))
rsp = take!(channel)
@test typeof(rsp) <: Message
@test rsp.performative == Performative.AGREE
Expand All @@ -82,7 +82,7 @@ try

@testset "send & receive (gw, nonblocking)" begin
flush(gw)
send(gw, ShellExecReq(recipient=shell, cmd="1+2"))
send(gw, ShellExecReq(recipient=shell, command="1+2"))
rsp = nothing
for _ = 1:10
rsp = receive(gw)
Expand All @@ -100,29 +100,29 @@ try

@testset "send & receive (aid)" begin
flush(gw)
send(shell, ShellExecReq(cmd="1+2"))
send(shell, ShellExecReq(command="1+2"))
rsp = receive(gw, 1000)
@test typeof(rsp) <: Message
@test rsp.performative == Performative.AGREE
end

@testset "request (gw)" begin
flush(gw)
rsp = request(gw, ShellExecReq(recipient=shell, cmd="1+2"))
rsp = request(gw, ShellExecReq(recipient=shell, command="1+2"))
@test typeof(rsp) <: Message
@test rsp.performative == Performative.AGREE
end

@testset "request (aid)" begin
flush(gw)
rsp = request(shell, ShellExecReq(cmd="1+2"))
rsp = request(shell, ShellExecReq(command="1+2"))
@test typeof(rsp) <: Message
@test rsp.performative == Performative.AGREE
end

@testset "<< (aid, +)" begin
flush(gw)
rsp = shell << ShellExecReq(cmd="1+2")
rsp = shell << ShellExecReq(command="1+2")
@test typeof(rsp) <: Message
@test rsp.performative == Performative.AGREE
end
Expand All @@ -134,13 +134,13 @@ try
end

@testset "<< (aid, -)" begin
rsp = dummy << ShellExecReq(cmd="1+2")
rsp = dummy << ShellExecReq(command="1+2")
@test rsp == nothing
end

@testset "flush" begin
flush(gw)
send(gw, ShellExecReq(recipient=shell, cmd="1+2"))
send(gw, ShellExecReq(recipient=shell, command="1+2"))
sleep(1)
flush(gw)
rsp = receive(gw, 1000)
Expand All @@ -155,15 +155,15 @@ try

@testset "subscribe (-)" begin
flush(gw)
send(ntf, ShellExecReq(cmd="1+2"))
send(ntf, ShellExecReq(command="1+2"))
msg = receive(gw, 1000)
@test msg == nothing
end

@testset "subscribe (+)" begin
flush(gw)
subscribe(gw, ntf)
send(ntf, ShellExecReq(cmd="1+2+3"))
send(ntf, ShellExecReq(command="1+2+3"))
msg = receive(gw, 1000)
@test typeof(msg) <: ShellExecReq
end
Expand All @@ -173,7 +173,7 @@ try
channel = Channel()
@async put!(channel, receive(gw, ShellExecReq, 1000))
yield()
send(ntf, ShellExecReq(cmd="1+2"))
send(ntf, ShellExecReq(command="1+2"))
msg = take!(channel)
@test typeof(msg) <: ShellExecReq
# make sure response is removed
Expand All @@ -183,7 +183,7 @@ try

@testset "receive (filt, +, nonblocking)" begin
flush(gw)
send(ntf, ShellExecReq(cmd="1+2"))
send(ntf, ShellExecReq(command="1+2"))
msg = nothing
for _ = 1:10
msg = receive(gw, ShellExecReq)
Expand All @@ -203,15 +203,15 @@ try

@testset "receive (filt, -)" begin
flush(gw)
send(ntf, ShellExecReq(cmd="1+2"))
send(ntf, ShellExecReq(command="1+2"))
msg = receive(gw, UnknownReq, 1000)
@test msg == nothing
end

@testset "unsubscribe" begin
unsubscribe(gw, ntf)
flush(gw)
send(ntf, ShellExecReq(cmd="1+2"))
send(ntf, ShellExecReq(command="1+2"))
msg = receive(gw, 1000)
@test msg == nothing
end
Expand All @@ -236,7 +236,7 @@ try
f = x as double[]
}
}"
shell << ShellExecReq(cmd=code)
shell << ShellExecReq(command=code)
sleep(2)
a = agent(gw, "a")

Expand Down Expand Up @@ -266,11 +266,11 @@ try
@testset "reconnect" begin
flush(gw)
close(gw.sock[])
send(gw, ShellExecReq(recipient=shell, cmd="1+2"))
send(gw, ShellExecReq(recipient=shell, command="1+2"))
rsp = receive(gw, 1000)
@test rsp === nothing
for i ∈ 1:10
send(gw, ShellExecReq(recipient=shell, cmd="1+2"))
send(gw, ShellExecReq(recipient=shell, command="1+2"))
rsp = receive(gw, 1000)
rsp === nothing || break
sleep(1.0)
Expand Down
Loading