pub struct Graph { /* private fields */ }
Expand description
A graph is a lightweight connection to libmapper’s distributed graph. You can use a graph to create maps and query the state of the graph.
Implementations§
Source§impl Graph
impl Graph
Sourcepub fn create() -> Graph
pub fn create() -> Graph
Examples found in repository?
7fn main() {
8 let graph = Graph::create();
9 let mut count = 0;
10
11 // Have to subscribe to all devices first in order to discover devices we don't own!
12 graph.subscribe(None, &[mpr_type::MPR_DEV]);
13
14 loop {
15
16 graph.poll_and_block(Duration::from_millis(10));
17 let list = graph.get_devices();
18
19 if list.len() != 0 {
20 for dev in list {
21 println!("Found device: {:?}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
22 }
23 break;
24 }
25
26 println!("Loop {}", count);
27 count += 1;
28 }
29
30}
More examples
10fn main() {
11 let graph = Graph::create();
12 let mut count = 0;
13
14 // Have to subscribe to all devices first in order to discover devices we don't own!
15 graph.subscribe(None, &[mpr_type::MPR_DEV, mpr_type::MPR_SIG]);
16
17 loop {
18
19 graph.poll_and_block(Duration::from_millis(100));
20 let list = graph.get_devices();
21
22 if list.len() != 0 {
23 for dev in list {
24 println!("Device: {}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
25 let signals = dev.get_signals(libmapper_rs::constants::mpr_dir::MPR_DIR_ANY);
26 for sig in signals {
27 println!("\tSignal: {}", sig.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
28 println!("\t\tType: {:?}", sig.get_property::<mpr_type>(mpr_prop::MPR_PROP_TYPE).unwrap());
29 println!("\t\tVector Length: {:?}", sig.get_vector_length());
30 println!("\t\tMin: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MIN));
31 println!("\t\tMax: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MAX));
32 println!("\t\tUnit: {:?}", sig.get_property_str(mpr_prop::MPR_PROP_UNIT));
33 println!("\t\tNum instances: {:?}", sig.get_property::<i32>(mpr_prop::MPR_PROP_NUM_INST).unwrap());
34 println!("");
35 }
36 }
37 break;
38 }
39
40 println!("Loop {}", count);
41 count += 1;
42 }
43
44}
6fn main() {
7 let graph = Arc::new(Graph::create());
8 let counter: Arc<AtomicU64> = Arc::new(AtomicU64::new(0));
9
10 let mut threads = Vec::<JoinHandle<()>>::new();
11
12 {
13 // spawn a thread to poll the graph
14 let graph = Arc::clone(&graph);
15 let thread = std::thread::spawn(move || {
16 loop {
17 graph.poll();
18 }
19 });
20 threads.push(thread);
21 }
22
23 let mut num_workers = 10;
24 let arg = env::args().skip(1).next();
25 if arg.is_some() {
26 num_workers = arg.unwrap().parse::<i32>().unwrap_or(10);
27 }
28
29 // spawn n threads creating then deleting devices at random
30 for _ in 0..num_workers {
31 let graph = graph.clone();
32 let id_counter = counter.clone();
33 let thread = std::thread::spawn(move || {
34 let name = format!("rust_{:?}", thread::current().id());
35 loop {
36 let _dev = libmapper_rs::device::Device::create_from_graph(&name, &graph);
37 loop {
38 _dev.poll();
39 if _dev.is_ready() {
40 break;
41 }
42 }
43 let signal = _dev.create_signal::<f32>("test_sig", libmapper_rs::constants::mpr_dir::MPR_DIR_OUT);
44 thread::sleep(std::time::Duration::from_millis(id_counter.fetch_add(1, Ordering::SeqCst) as u64));
45 drop(signal);
46 drop(_dev);
47 }
48 });
49 threads.push(thread);
50 }
51
52 for thread in threads {
53 thread.join().unwrap();
54 }
55}
Source§impl Graph
impl Graph
Sourcepub fn poll(&self)
pub fn poll(&self)
Poll the graph without blocking
Examples found in repository?
6fn main() {
7 let graph = Arc::new(Graph::create());
8 let counter: Arc<AtomicU64> = Arc::new(AtomicU64::new(0));
9
10 let mut threads = Vec::<JoinHandle<()>>::new();
11
12 {
13 // spawn a thread to poll the graph
14 let graph = Arc::clone(&graph);
15 let thread = std::thread::spawn(move || {
16 loop {
17 graph.poll();
18 }
19 });
20 threads.push(thread);
21 }
22
23 let mut num_workers = 10;
24 let arg = env::args().skip(1).next();
25 if arg.is_some() {
26 num_workers = arg.unwrap().parse::<i32>().unwrap_or(10);
27 }
28
29 // spawn n threads creating then deleting devices at random
30 for _ in 0..num_workers {
31 let graph = graph.clone();
32 let id_counter = counter.clone();
33 let thread = std::thread::spawn(move || {
34 let name = format!("rust_{:?}", thread::current().id());
35 loop {
36 let _dev = libmapper_rs::device::Device::create_from_graph(&name, &graph);
37 loop {
38 _dev.poll();
39 if _dev.is_ready() {
40 break;
41 }
42 }
43 let signal = _dev.create_signal::<f32>("test_sig", libmapper_rs::constants::mpr_dir::MPR_DIR_OUT);
44 thread::sleep(std::time::Duration::from_millis(id_counter.fetch_add(1, Ordering::SeqCst) as u64));
45 drop(signal);
46 drop(_dev);
47 }
48 });
49 threads.push(thread);
50 }
51
52 for thread in threads {
53 thread.join().unwrap();
54 }
55}
Sourcepub fn poll_and_block(&self, time: Duration)
pub fn poll_and_block(&self, time: Duration)
Poll the graph and block for the specified amount of time
Use this instead of sleeping in a loop
Examples found in repository?
7fn main() {
8 let graph = Graph::create();
9 let mut count = 0;
10
11 // Have to subscribe to all devices first in order to discover devices we don't own!
12 graph.subscribe(None, &[mpr_type::MPR_DEV]);
13
14 loop {
15
16 graph.poll_and_block(Duration::from_millis(10));
17 let list = graph.get_devices();
18
19 if list.len() != 0 {
20 for dev in list {
21 println!("Found device: {:?}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
22 }
23 break;
24 }
25
26 println!("Loop {}", count);
27 count += 1;
28 }
29
30}
More examples
10fn main() {
11 let graph = Graph::create();
12 let mut count = 0;
13
14 // Have to subscribe to all devices first in order to discover devices we don't own!
15 graph.subscribe(None, &[mpr_type::MPR_DEV, mpr_type::MPR_SIG]);
16
17 loop {
18
19 graph.poll_and_block(Duration::from_millis(100));
20 let list = graph.get_devices();
21
22 if list.len() != 0 {
23 for dev in list {
24 println!("Device: {}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
25 let signals = dev.get_signals(libmapper_rs::constants::mpr_dir::MPR_DIR_ANY);
26 for sig in signals {
27 println!("\tSignal: {}", sig.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
28 println!("\t\tType: {:?}", sig.get_property::<mpr_type>(mpr_prop::MPR_PROP_TYPE).unwrap());
29 println!("\t\tVector Length: {:?}", sig.get_vector_length());
30 println!("\t\tMin: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MIN));
31 println!("\t\tMax: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MAX));
32 println!("\t\tUnit: {:?}", sig.get_property_str(mpr_prop::MPR_PROP_UNIT));
33 println!("\t\tNum instances: {:?}", sig.get_property::<i32>(mpr_prop::MPR_PROP_NUM_INST).unwrap());
34 println!("");
35 }
36 }
37 break;
38 }
39
40 println!("Loop {}", count);
41 count += 1;
42 }
43
44}
Sourcepub fn subscribe(&self, device: Option<Device<'_>>, types: &[mpr_type])
pub fn subscribe(&self, device: Option<Device<'_>>, types: &[mpr_type])
Tells the graph to subscribe to receive updates for the specified device and types.
If the device is None
, the graph will automatically subscribe to all devices as they become visible.
types
allows filtering the objects of interest. For example, to only listen for information about signals, use [mpr_type::MPR_SIG]
.
This function must be called before functions like get_devices will return any results.
Examples found in repository?
7fn main() {
8 let graph = Graph::create();
9 let mut count = 0;
10
11 // Have to subscribe to all devices first in order to discover devices we don't own!
12 graph.subscribe(None, &[mpr_type::MPR_DEV]);
13
14 loop {
15
16 graph.poll_and_block(Duration::from_millis(10));
17 let list = graph.get_devices();
18
19 if list.len() != 0 {
20 for dev in list {
21 println!("Found device: {:?}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
22 }
23 break;
24 }
25
26 println!("Loop {}", count);
27 count += 1;
28 }
29
30}
More examples
10fn main() {
11 let graph = Graph::create();
12 let mut count = 0;
13
14 // Have to subscribe to all devices first in order to discover devices we don't own!
15 graph.subscribe(None, &[mpr_type::MPR_DEV, mpr_type::MPR_SIG]);
16
17 loop {
18
19 graph.poll_and_block(Duration::from_millis(100));
20 let list = graph.get_devices();
21
22 if list.len() != 0 {
23 for dev in list {
24 println!("Device: {}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
25 let signals = dev.get_signals(libmapper_rs::constants::mpr_dir::MPR_DIR_ANY);
26 for sig in signals {
27 println!("\tSignal: {}", sig.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
28 println!("\t\tType: {:?}", sig.get_property::<mpr_type>(mpr_prop::MPR_PROP_TYPE).unwrap());
29 println!("\t\tVector Length: {:?}", sig.get_vector_length());
30 println!("\t\tMin: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MIN));
31 println!("\t\tMax: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MAX));
32 println!("\t\tUnit: {:?}", sig.get_property_str(mpr_prop::MPR_PROP_UNIT));
33 println!("\t\tNum instances: {:?}", sig.get_property::<i32>(mpr_prop::MPR_PROP_NUM_INST).unwrap());
34 println!("");
35 }
36 }
37 break;
38 }
39
40 println!("Loop {}", count);
41 count += 1;
42 }
43
44}
Sourcepub fn get_devices<'a>(&'a self) -> Vec<Device<'a>>
pub fn get_devices<'a>(&'a self) -> Vec<Device<'a>>
Get all devices currently visible to the graph.
If subscribe has not been called, this function will not be able to see any devices (except those owned by this graph via Device::create_from_graph
).
Examples found in repository?
7fn main() {
8 let graph = Graph::create();
9 let mut count = 0;
10
11 // Have to subscribe to all devices first in order to discover devices we don't own!
12 graph.subscribe(None, &[mpr_type::MPR_DEV]);
13
14 loop {
15
16 graph.poll_and_block(Duration::from_millis(10));
17 let list = graph.get_devices();
18
19 if list.len() != 0 {
20 for dev in list {
21 println!("Found device: {:?}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
22 }
23 break;
24 }
25
26 println!("Loop {}", count);
27 count += 1;
28 }
29
30}
More examples
10fn main() {
11 let graph = Graph::create();
12 let mut count = 0;
13
14 // Have to subscribe to all devices first in order to discover devices we don't own!
15 graph.subscribe(None, &[mpr_type::MPR_DEV, mpr_type::MPR_SIG]);
16
17 loop {
18
19 graph.poll_and_block(Duration::from_millis(100));
20 let list = graph.get_devices();
21
22 if list.len() != 0 {
23 for dev in list {
24 println!("Device: {}", dev.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
25 let signals = dev.get_signals(libmapper_rs::constants::mpr_dir::MPR_DIR_ANY);
26 for sig in signals {
27 println!("\tSignal: {}", sig.get_property_str(mpr_prop::MPR_PROP_NAME).unwrap());
28 println!("\t\tType: {:?}", sig.get_property::<mpr_type>(mpr_prop::MPR_PROP_TYPE).unwrap());
29 println!("\t\tVector Length: {:?}", sig.get_vector_length());
30 println!("\t\tMin: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MIN));
31 println!("\t\tMax: {:?}", sig.get_property::<f32>(mpr_prop::MPR_PROP_MAX));
32 println!("\t\tUnit: {:?}", sig.get_property_str(mpr_prop::MPR_PROP_UNIT));
33 println!("\t\tNum instances: {:?}", sig.get_property::<i32>(mpr_prop::MPR_PROP_NUM_INST).unwrap());
34 println!("");
35 }
36 }
37 break;
38 }
39
40 println!("Loop {}", count);
41 count += 1;
42 }
43
44}