Busses

A Bus is actually is a bunch of links. Reading from and writing into data is performed as in the case of Links.

Construction of Bus

A Bus is constructed by specifying its element type T, the number of links nlinks and the buffer length of its links.

Jusdl.Connections.BusType
Bus(links::AbstractVector{L}) where L<:Link

Constructs a Bus consisting of links links.

Bus(dtype::Type{T}, nlinks::Int, ln::Int=64) where T

Constructs a Bus consisting of links of length nlinks. T is element type of links. ln is the buffer length of links.

Bus(nlinks::Int=1, ln::Int=64)

Constructs a Bus consisting of links of length nlinks. Float64 is element type of links. ln is the buffer length of links.

Example

julia> Bus([Link() for i = 1 : 3])
Bus(nlinks:3, eltype:Link{Float64}, isreadable:false, iswritable:false)

julia> Bus(Int, 5, 10)
Bus(nlinks:5, eltype:Link{Int64}, isreadable:false, iswritable:false)

julia> Bus()
Bus(nlinks:1, eltype:Link{Float64}, isreadable:false, iswritable:false)
source

Connection and Disconnection of Busses

The Busses can be connected to and disconnected from each other. When connected any data written to the master bus is also written all slave busses. See the following example.

Let us connect two busses and connect them together.


julia> b1 = Bus(2, 5)  # Bus with `2` links with buffer length of `5`.
2-element Bus{Link{Float64}}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b2 = Bus(2, 5)  # Bus with `2` links with buffer length of `5`.
2-element Bus{Link{Float64}}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> connect(b1, b2)

Here, b1 is the master bus and b2 is the slave bus. That is, data written to b1 is also written into b2.

julia> t1 = @async while true
           val = take!(b1)
           all(val .=== NaN) && break
           println("Took " * string(val))
       end
Task (runnable) @0x00007f70295b7340

julia> t2 = @async while true
           val = take!(b2)
           all(val .=== NaN) && break
           println("Took " * string(val))
       end
Task (runnable) @0x00007f70295b7820

julia> put!(b1, [5., 10.]);
Took [5.0, 10.0]

Took [5.0, 10.0]
julia> [b1[i].buffer.data for i = 1 : 2]
2-element Array{Array{Float64,1},1}:
 [5.0, 0.0, 0.0, 0.0, 0.0]
 [10.0, 0.0, 0.0, 0.0, 0.0]

julia> [b2[i].buffer.data for i = 1 : 2]
2-element Array{Array{Float64,1},1}:
 [5.0, 0.0, 0.0, 0.0, 0.0]
 [10.0, 0.0, 0.0, 0.0, 0.0]

Note that the data [5, 10] written to b1 is also written b2 since they are connected.

The Busses connected to each other can be disconnected. When disconnected, the data written to master is not written to slaves

julia> disconnect(b1, b2)

julia> isconnected(b1, b2)
false

Data Flow through Busses

Data flow through the Busses is very similar to the case in Links. See Data Flow through Links for information about data flow through Links. Runnable tasks must be bound to the links of the busses for data flow through the Bus. Again, put! and take! functions are used to write data from a Bus and read from data from a Bus.

Base.put!Method
put!(bus::Bus, vals)

Puts vals to bus. Each item in vals is putted to the links of the bus.

Warning

The bus must be writable to be read. That is, there must be a runnable tasks bound to links of the bus that reads data from bus.

Example

julia> bus = Bus();

julia> t = @async while true 
       val = take!(bus)
       all(val .=== NaN) && break 
       println("Took " * string(val))
       end;

julia> put!(bus, [1.])
Took [1.0]
1-element Array{Float64,1}:
 1.0

julia> put!(bus, [NaN])
1-element Array{Float64,1}:
 NaN
source
Base.take!Method
take!(bus::Bus)

Takes an element from bus. Each link of the bus is a read and a vector containing the results is returned.

Warning

The bus must be readable to be read. That is, there must be a runnable tasks bound to links of the bus that writes data to bus.

Example

julia> b = Bus()
Bus(nlinks:1, eltype:Link{Float64}, isreadable:false, iswritable:false)

julia> t = @async for val in 1 : 5 
       put!(b, [val])
       end;

julia> take!(b)
1-element Array{Float64,1}:
 1.0

julia> take!(b)
1-element Array{Float64,1}:
 2.0
source

Any data written to a Bus is recorded into the buffers of its links.

@repl writing_to_busses
using Jusdl # hide
b = Bus(2, 5);
t = @async while true
    val = take!(b)
    all(val .=== NaN) && break
    println("Took " * string(val))
end
put!(b, 1.);
b[1].buffer.data

Indexing and Iteration of Busses

Busses can be indexed similarly to the arrays in Julia. When indexed, the corresponding link of the bus is returned.

julia> using Jusdl # hide

julia> b = Bus(3)
3-element Bus{Link{Float64}}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b[1]
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b[end]
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b[:]
3-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b[1] = Link()
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> b[1:2] = [Link(), Link()]
2-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

The iteration of Busses in a loop is also possible. When iterated, the links of the Bus is returned.

julia> using Jusdl # hide

julia> bus = Bus(3)
3-element Bus{Link{Float64}}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> for link in bus
           @show link
       end
link = Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
link = Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
link = Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

Full API

Base.closeMethod
close(bus::Bus)

Closes bus. When closed, no more data flow is possible for bus.

source
Jusdl.Connections.launchMethod
launch(bus::Bus, valrange::AbstractVector)

Launches every links of bus with every item of valrange. See [launch(link:Link, valrange)(@ref)]

source
Base.getindexMethod
getindex(bus::Bus, idx::Vararg{Int, N}) where N

Returns elements from bus at index idx. Same as bus[idx].

Example

julia> bus = Bus(3);

julia> bus[1]
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> bus[end]
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> bus[:]
3-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
source
Base.setindex!Method
setindex!(bus::Bus, item, idx::Vararg{Int, N}) where N

Sets item to bus at index idx. Same as bus[idx] = item.

Example

julia> bus = Bus(3);

julia> bus[1] = Link()
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> bus[end] = Link(5)
Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)

julia> bus[1:2] = [Link(), Link()]
2-element Array{Link{Float64},1}:
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
 Link(state:open, eltype:Float64, hasmaster:false, numslaves:0, isreadable:false, iswritable:false)
source