How to explain the difference between those 2 creations of broadcast streams? - stream

Depending how I create the broadcast stream/streamController the result is not the same.
With StreamController.broadcast():
final sc = StreamController.broadcast();
final stream = sc.stream;
print(stream.isBroadcast);
stream.length.then(print);
stream.length.then(print);
sc..add('')..close();
stream.length.then(print);
stream.length.then(print);
I get the output:
true
c0
d0
a1
b1
With StreamController.stream.asBroadcastStream():
final sc = StreamController();
final stream = sc.stream.asBroadcastStream();
print(stream.isBroadcast);
stream.length.then(print);
stream.length.then(print);
sc..add('')..close();
stream.length.then(print);
stream.length.then(print);
I get the output:
true
a1
b1
c1
d1
How to explain the differences?
(I made this test on DartPad SDK 2.0.0)

Related

What is the dart equivalent of RandomAccessFile readLong, readInt in Java

What would be the equivalent of Java RandomAccessFile readLong
in Dart RandomAccessFile ?
Updated answer:
var b = await file.read(8);
var longVal = b.buffer.asByteData().getUint64(0);
or
var b = await file.read(4);
var longVal = b.buffer.asByteData().getUint32(0);
readLong reads a signed 64-bit integer from this file.
This method reads eight bytes from the file, starting at the current file pointer.
If the bytes read, in order, are:
b1, b2, b3, b4, b5, b6, b7, and b8, where:
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
then the result is equal to:
var b = await file.read(8); //Uint8List
var longVal = (b[0] << 56) + (b[1] << 48) + (b[2] << 40) + (b[3] << 32) + (b[4] << 24) + (b[5] << 16) + (b[6] << 8) + b[7];

Packets are not getting received at other interface

I have a script which consists of 2 switches 2 hosts which are in different subnet connected to a single router. When I send packets from 1 host to the other packets are not getting received at another interface, I am using Wireshark at the other interface for the confirmation but no packets have arrived. Attaching the code and screenshot of the topology. Any help regarding this will be great.
The topology is like this
H1----S1-----R------S2------H2
from mininet.net import Mininet
from mininet.node import Controller, RemoteController,OVSKernelSwitch, UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink
def topology():
net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )
# Add hosts and switches
h1 = net.addHost( 'h1', ip="10.0.1.10/24", mac="00:00:00:00:00:01" )
h2 = net.addHost( 'h2', ip="10.0.2.10/24", mac="00:00:00:00:00:02" )
r1 = net.addHost( 'r1')
s1 = net.addSwitch( 's1')
s2 = net.addSwitch( 's2')
c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )
net.addLink( r1, s1 )
net.addLink( r1, s2 )
net.addLink( h1, s1 )
net.addLink( h2, s2 )
net.build()
c0.start()
s1.start( [c0] )
s2.start( [c0] )
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ifconfig r1-eth0 hw ether 00:00:00:00:01:01")
r1.cmd("ifconfig r1-eth1 hw ether 00:00:00:00:01:02")
r1.cmd("ip addr add 10.0.1.1/24 brd + dev r1-eth0")
r1.cmd("ip addr add 10.0.2.1/24 brd + dev r1-eth1")
r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
h1.cmd("ip route add default via 10.0.1.1")
h2.cmd("ip route add default via 10.0.2.1")
s1.cmd("ovs-ofctl add-flow s1 priority=1,arp,actions=flood")
s1.cmd("ovs-ofctl add-flow s1 priority=65535,ip,dl_dst=00:00:00:00:01:01,actions=output:1")
s1.cmd("ovs-ofctl add-flow s1 priority=10,ip,nw_dst=10.0.1.0/24,actions=output:2")
s2.cmd("ovs-ofctl add-flow s2 priority=1,arp,actions=flood")
s2.cmd("ovs-ofctl add-flow s2 priority=65535,ip,dl_dst=00:00:00:00:01:02,actions=output:1")
s2.cmd("ovs-ofctl add-flow s2 priority=10,ip,nw_dst=10.0.2.0/24,actions=output:2")
print "*** Running CLI"
CLI( net )
print "*** Stopping network"
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
topology()

Lua Musical Notes to Numbers

I need to convert some musical note inputs representing a chord to numbers above it's root note 0 using Lua.
So from the midi data we get the notes of a C13 Chord
input: C, E, G, A#, D, F, A
as the root note 0 is C we start on the C note,
below we have 2 octaves of a piano keyboard, 12 notes on each where chords are played
0C 1C# 2D 3D# 4E 5F 6F# 7G 8G# 9A 10A# 11B 12C 13C# 14D 15D# 16E 17F 18F# 19G 20G# 21A 22A# 23B
so C is the root note 0
D,F,A are played on the next octave
result: 0,4,7,10,14,17,21
so if we have a D chord
input: D,F#,A
D is the root note 0
all notes played on the first octave
0D 1D# 2E 3F 4F# 5G 6G# 7A 8A# 9B 10C 11C# 12D 13D# 14E 15F 16F# 17G 18G# 19A 20A# 21B 22C 23C#
result: 0,4,7
G#m7#9 Chord
input: G#,B,D#,F#,B
0G# 1A 2A# 3B 4C 5C# 6D 7D# 8E 9F 10F# 11G 12G# 13A 14A# 15B 16C 17C# 18D 19D# 20E 21F 22F# 23G
result: 0,3,7,10,15
Something like this may work:
local function notes2nums(input)
local map = {A = 9, ["A#"] = 10, B = 11, C = 0, ["C#"] = 1, D = 2, ["D#"] = 3, E = 4, F = 5, ["F#"] = 6, G = 7, ["G#"] = 8}
local base, prev
return (input:gsub("([^,]+)", function(note)
local num = map[note] or error(("Unexpected note value '%s'"):format(note))
base = base or num
num = num - base
if prev and num < prev then num = num + 12 end
prev = num
return tostring(num)
end))
end
print(notes2nums("D,F#,A"))
print(notes2nums("C,E,G,A#,D,F,A"))
print(notes2nums("G#,B,D#,F#,B"))
This prints:
0,4,7
0,4,7,10,14,17,21
0,3,7,10,15

What is hexa MIDI message for G2, D♯3, D3 in iOS?

I want to send MIDI notes using HEXA package.
G2, D♯3, D3
using coreMIDI in swift
please See below Midi Packet For above mention Note.
For G2
var packet1:MIDIPacket = MIDIPacket()
packet1.timeStamp = 0
packet1.length = 3
packet1.data.0 = 0x90 // Note On event channel 1
packet1.data.1 = 0x37 // Note C3
packet1.data.2 = 0x7F // Velocity
For D#3
just Update
packet1.data.1 = 0x3F
For D3
just Update
packet1.data.1 = 0x3E
For More Codes, Open this file MidiOutpout In Midi Monitor Application

Pairs in lua programming

I have two set of values defined:
local A1 = {100, 200, 300, 400}
local A2 = {500, 600, 700, 800}
I want to iterate a loop assigning values for another variable B1 and B2 as pairs from A1 and A2 as follows:
B1 = 100 and B2 = 500 (first iteration)
B1 =200 and B2 = 600 (second iteration)
B1 = 300 and B2 = 700 (third iteration)
B1=400 and B2 = 800 (fourth iteration)
I tried to use ipairs as follows:
for i, f1 in ipairs(A1) do
for j, f2 in ipairs(A2) do
B1 = f1
B2 = f2
end
end
but this gave me
B1 = 100 and B2 = 500 (first iteration)
B1 =100 and B2 = 600 (second iteration)
B1 = 100 and B2 = 700 (third iteration)
B1=100 and B2 = 800 (fourth iteration)
B1 = 200 and B2 = 500 (fifth iteration)
B1 =200 and B2 = 600 (sixth iteration)
B1 =200 and B2 = 700 (seventh iteration)
....
...
...
so on...
can anyone help me to code in the right way?
You can easily do this with a numerical loop:
for i = 1, 4 do
local a, b = A1[i], B1[i]
--- use them
end
How you go about determining the number of iterations you'll need is the tricky part. If the sizes are variant, but each table is the same length as the others you can instead use the length operator (#A1).
Alternatively, you might want a function that returns the largest length of a given set of tables.
local function max_table_len (...)
local tabs = { ... }
local len = 0
for i = 1, #tabs do
local l = #tabs[i]
if l > len then
len = l
end
end
return len
end
And maybe even a helper function to get each value.
local function get_from_tables (index, ...)
local values = { ... }
local len = #values
for i = 1, len do
values[i] = values[i][index]
end
return table.unpack(values, 1, len)
end
Ending up with something like:
for index = 1, max_table_len(A1, B1) do
local a, b = get_from_tables(index, A1, B1)
end
You can build on the ipairs example from Programming in Lua. For instance this version iterates over 2 sequences in parallel:
-- iterator function
local function iter_ipairs2(tablePair, i)
i = i + 1
local v1 = tablePair[1][i]
local v2 = tablePair[2][i]
-- if you use 'and' here the iteration stops after finishing
-- the shortest sequence. If you use 'or' the iteration
-- will stop after it finishes the longest sequence.
if v1 and v2 then
return i, v1, v2
end
end
-- this is the function you'll call from your other code:
local function ipairs2(t1, t2)
return iter_ipairs2, {t1, t2}, 0
end
-- usage:
local A1 = {100, 200, 300, 400, 500}
local A2 = {500, 600, 700, 800}
for i, v1, v2 in ipairs2(A1, A2) do
print(i, v1, v2)
end
The previous answers are more detailed and provide a more general and better answer.
This one is for someone very new to Lua. Not only does it show two loops, it reinforces that there is usually more than one way to get where you want to go.
local A1 = {100, 200, 300, 400}
local A2 = {500, 600, 700, 800}
print("simplest answer:")
-- doesn't use ipairs and assumes A1 and A2 are the same size
for i = 1, #A1 do
B1 = A1[i]
B2 = A2[i]
print(B1, B2, "(iteration #"..i..")")
end
print()
print("answer that uses ipairs:")
-- again, assumes A1 and A2 are the same size
for i, v in ipairs(A1) do
B1 = A1[i] -- i steps through A1 and A2
B2 = A2[i] -- this works because A1 and A2 are same size
print(B1, B2, "(iteration #"..i..")")
end
Gives this output:
simplest answer:
100 500 (iteration #1)
200 600 (iteration #2)
300 700 (iteration #3)
400 800 (iteration #4)
answer that uses ipairs:
100 500 (iteration #1)
200 600 (iteration #2)
300 700 (iteration #3)
400 800 (iteration #4)

Resources