java & javascipt reactive robotics and iot 2016 @ jprofessionals
TRANSCRIPT
![Page 1: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/1.jpg)
Trayan [email protected]://iproduct.org
Copyright © 2003-2016 IPT - Intellectual Products & Technologies
December 3, 2016 jProfessionals - Plovdiv
Spring Reactor, Angular 2, RxJS & Reactive Robotics
![Page 2: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/2.jpg)
2
Disclaimer
All information presented in this document and all supplementary materials and programming code represent only my personal opinion and current understanding and has not received any endorsement or approval by IPT - Intellectual Products and Technologies or any third party. It should not be taken as any kind of advice, and should not be used for making any kind of decisions with potential commercial impact.
The information and code presented may be incorrect or incomplete. It is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the author or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the information, materials or code presented or the use or other dealings with this information or programming code.
![Page 3: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/3.jpg)
3
Trademarks
Oracle®, Java™ and JavaScript™ are trademarks or registered trademarks of Oracle and/or its affiliates.
LEGO® is a registered trademark of LEGO® Group. Programs are not affiliated, sponsored or endorsed by LEGO® Education or LEGO® Group.
Raspberry Pi™ is a trademark of Raspberry Pi Foundation.
Other names may be trademarks of their respective owners.
![Page 4: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/4.jpg)
IPT - Intellectual Products & TechnologiesIT Education Evolved
4
Since 2003 we provide trainings and share tech knowledge in Java SE/ EE/ Web/ JS/ ES/ TypeScript/ Node/ Express/ Socket.IO/ NoSQL/ Angular 2/ React / REST SOA:
Java EE6/7, Spring, JSF, Portals/Portlets: Liferay, GateIn
Reactive IoT with Reactor / RxJava / RxJS
Node.js + Express/ hapi + React.js + Redux
Angular 2 + TypeScript + Redux (ngrx)
REST HATEOAS – Distributed Hypermedia APIs
Domain Driven Design, Reactive Microservices and Event Sourcing, Docker/Kubernetes, Big Data
![Page 5: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/5.jpg)
Tales of JAVA Robotics
5
There are several tales to share:
Tale of Robotics, IoT and Complexity
Tale of Common Sense: DDD
Tale of two cities - Imperative and Reactive
Tale of two brave robots: LeJaRo and IPTPI
And some real reactive Java + TypeScript / Angular 2 /
WebSocket code
![Page 6: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/6.jpg)
6
High Performnce Reactive JAVA
Reactive programming. Reactor & Proactor design patterns. Reactive Streams (java.util.concurrent.Flow)
High performance non-blocking asynchronous apps on JVM using Reactor project & RxJava
Disruptor (RingBuffer), Flux & Mono, Processors
End-to-end reactive web applications and services: Reactor IO (REST, WebSocket) + RxJS + Angular 2
Demo - reactive hot event streams processing on Raspberry Pi 2 (ARM v7) based robot IPTPI.
RxJava (not Zen only :) coans for self assessment
![Page 7: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/7.jpg)
Where to Find the Demo Code?
7
IPTPI Reactive Demo is available @ GitHub:
https://github.com/iproduct/jprime-demo
![Page 8: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/8.jpg)
Robots Can Be Complex
8
![Page 9: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/9.jpg)
… Even More Complex
9
Cross-section of many disciplines: mechanical engineering
electrical engineering
computer science
artificial intelligence (AI)
human-computer interaction
sociology & psychology
Picture by Hugo Elias of the Shadow Robot Company - http://www.shadowrobot.com/media/pictures.shtml, CC BY-SA 3.0
![Page 10: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/10.jpg)
Engineering, Science & Art
10
Source: https://commons.wikimedia.org/w/index.php?curid=551256, CC BY-SA 3.0
![Page 11: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/11.jpg)
and How Can We Forget
11
Source: https://commons.wikimedia.org/w/index.php?curid=234900, CC BY-SA 3.0
Source: Korea Institute of Industrial Technology, http://news.naver.com/main/read.nhn?mode=LSD&mid=sec&sid1=102&oid=020&aid=0000371339
![Page 12: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/12.jpg)
Robots: The Most Intelligent Things
12
CC BY 2.0, Source: https://www.flickr.com/photos/wilgengebroed/8249565455/
Radar, GPS, lidar for navigation and obstacle avoidance ( 2007 DARPA Urban Challenge )
![Page 13: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/13.jpg)
The Internet of Things has the potential to change the world, just as the Internet did. Maybe even more so.
Nearly 50 petabytes of data are captured and created by human beings
People have limited time, attention and accuracy
Capturing data about things in the real world in real time
Track and count everything, reduce waste, loss & cost.
Know when things need replacing, repairing or recalling
— Kevin Ashton, 'That 'Internet of Things' Thing', RFID Journal, 2009
Internet of Things (IoT)
![Page 14: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/14.jpg)
There will be nearly 26 billion devices on the Internet of Things by 2020.
[Gartner]
More than 30 billion devices will be wirelessly connected to the Internet of Things by 2020
[ABI Research]
It's expected to be a 19 Trillion USD market [John Chambers, Cisco CEO]
IoT Perspectives
![Page 15: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/15.jpg)
"Basket of remotes" problem – we'll have hundreds of applications to interface with hundreds of devices that don't share protocols for speaking with one another
[Jean-Louis Gassée, Apple initial team, and BeOS co-founder]
Only IPv6 addresses are not enough – IoT devices should be also easily and directly accessible for users and [their] agents
In read/write mode
Preferably using a standard web browser
Even behind firewalls
IoT - Need for Standards
![Page 16: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/16.jpg)
IoT Services Architecture
16
Devices: Hardware + Embedded Software + Firmware
UART/ I2C/ 2G/ 3G/ LTE/ ZigBee/ 6LowPan/ BLE
Aggregation/ Bus: ESB, Message Broker
Device Gateway: Local Coordination and Event Aggregation
M2M: HTTP(/2) / WS / MQTT / CoAPManagement: TR-069 / OMA-DM / OMA LWM2M
HTTP, AMQP
Cloud (Micro)Service Mng. Docker, Kubernetes/
Apache Brooklyn
Web/ Mobile Portal
PaaSDashboard
PaaS API: Event Processing Services, Analytics
![Page 17: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/17.jpg)
Tracking Complexity
17
We need tools to cope with all that complexity inherent in robotics and IoT domains.
Simple solutions are needed – cope with problems through divide and concur on different levels of abstraction:
Domain Driven Design (DDD) – back to basics: domain objects, data and logic.
Described by Eric Evans in his book: Domain Driven Design: Tackling Complexity in the Heart of Software, 2004
![Page 18: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/18.jpg)
Common Sense: DDD
18
Actually DDD require additional efforts (as most other divide and concur modeling approaches :)
Ubiquitous language and Bounded Contexts
DDD Application Layers:
Infrastructure, Domain, Application, Presentation
Hexagonal architecture :
OUTSIDE <-> transformer <-> ( application <-> domain ) [A. Cockburn]
![Page 19: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/19.jpg)
Common Sense: DDD
19
Main concepts:
Entities, value objects and modules
Aggregates and Aggregate Roots [Haywood]:
value < entity < aggregate < module < BC
Repositories, Factories and Services:
application services <-> domain services
Separating interface from implementation
![Page 20: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/20.jpg)
Imperative and Reactive
20
We live in a Connected Universe
... there is hypothesis that all the things in the Universe are intimately connected, and you can not change a bit without changing all.
Action – Reaction principle is the essence of how Universe behaves.
![Page 21: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/21.jpg)
Imperative and Reactive
Reactive Programming: using static or dynamic data flows and propagation of change
Example: a := b + c
Functional Programming: evaluation of mathematical functions, ➢ Avoids changing-state and mutable data, declarative
programming➢ Side effects free => much easier to understand and
predict the program behavior. Example: books.stream().filter(book -> book.getYear() > 2010).forEach( System.out::println )
![Page 22: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/22.jpg)
Functional Reactive (FRP)
22
According to Connal Elliot's (ground-breaking paper @ Conference on Functional Programming, 1997), FRP is:
(a) Denotative (b) Temporally continuous
![Page 24: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/24.jpg)
Reactive Programming
24
Microsoft® opens source polyglot project ReactiveX (Reactive Extensions) [http://reactivex.io]:
Rx = Observables + LINQ + Schedulers :)
Java: RxJava, JavaScript: RxJS, C#: Rx.NET, Scala: RxScala, Clojure: RxClojure, C++: RxCpp, Ruby: Rx.rb, Python: RxPY, Groovy: RxGroovy, JRuby: RxJRuby, Kotlin: RxKotlin ...
Reactive Streams Specification [http://www.reactive-streams.org/] used by
(Spring) Project Reactor [http://projectreactor.io/]
![Page 25: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/25.jpg)
Reactive Streams Spec.
25
Reactive Streams – provides standard for asynchronous stream processing with non-blocking back pressure.
Minimal set of interfaces, methods and protocols for asynchronous data streams
April 30, 2015: has been released version 1.0.0 of Reactive Streams for the JVM (Java API, Specification, TCK and implementation examples)
Java 9: java.util.concurrent.Flow
![Page 26: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/26.jpg)
Reactive Streams Spec.
26
Publisher – provider of potentially unbounded number of sequenced elements, according to Subscriber(s) demand.
Publisher.subscribe(Subscriber) => onSubscribe onNext* (onError | onComplete)?
Subscriber – calls Subscription.request(long) to receive notifications
Subscription – one-to-one Subscriber ↔ Publisher, request data and cancel demand (allow cleanup).
Processor = Subscriber + Publisher
![Page 27: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/27.jpg)
FRP = Async Data Streams
27
FRP is asynchronous data-flow programming using the building blocks of functional programming (e.g. map, reduce, filter) and explicitly modeling time
Used for GUIs, robotics, and music. Example (RxJava): Observable.from(new String[]{"Reactive", "Extensions", "Java"}) .take(2).map(s -> s + " : on " + new Date()) .subscribe(s -> System.out.println(s));Result: Reactive : on Wed Jun 17 21:54:02 GMT+02:00 2015Extensions : on Wed Jun 17 21:54:02 GMT+02:00 2015
![Page 28: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/28.jpg)
28
Performance is about 2 things (Martin Thompson – http://www.infoq.com/articles/low-latency-vp ):– Throughput – units per second, and – Latency – response time
Real-time – time constraint from input to response regardless of system load.
Hard real-time system if this constraint is not honored then a total system failure can occur.
Soft real-time system – low latency response with little deviation in response time
100 nano-seconds to 100 milli-seconds. [Peter Lawrey]
What About High Performance?
![Page 29: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/29.jpg)
29
Mechanical Sympathy – hardware (CPU, cache, memory, IO, Network), operating system, language implementation platform (e.g. JVM), and application level code are working in harmony to minimize the time needed for event (request, message) processing => 10% / 90% principle
Throughput vs. latency – bus vs. car traveling
Throughput ~ System Capacity / Latency
Achieving low latency may mean additional work done by system => lowered System Capacity and Throughput
Horizontal scalability is valuable for high throughput. For low latency, you need simplicity – critical path.
Throughput vs. Latency
![Page 30: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/30.jpg)
30
JVMs can be faster than custom C++ code because of the holistic optimizations that they can apply across an application [Andy Piper].
Developers can take advantage of hardware guarantees through a detailed understanding of:
– Java Memory Model & mapping to underlying hardware
– low latency software system hardware (CPU, cache, memory, IO, Network)
– avoiding lock-contention and garbage collection
– Compre-And-Swap – CAS (java.util.concurrent.atomic)
– lock-free, wait-free techniques – using standard libraries (e.g. the LMAX Disruptor)
High Performance Java
![Page 31: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/31.jpg)
31
CPU Cache – False SharingCore 2 Core NCore 1 ...
Registers
Execution Units
L1 Cache A | | B |
L2 Cache A | | B |
L3 Cache A | | B |
DRAM Memory A | | B |
Registers
Execution Units
L1 Cache A | | B |
L2 Cache A | | B |
![Page 32: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/32.jpg)
32
Low garbage by reusing existing objects + infrequent GC when application not busy – can improve app 2 - 5x
JVM generational GC startegy – ideal for objects living very shortly (garbage collected next minor sweep) or be immortal
Non-blocking, lockless coding or CAS
Critical data structures – direct memory access using DirectByteBuffers or Unsafe => predictable memory layout and cache misses avoidance
Busy waiting – giving the CPU to OS kernel slows program 2-5x => avoid context switches
Amortize the effect of expensive IO - blocking
Low Latency: Things to Remember
![Page 33: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/33.jpg)
33
Parallel tasks can increase your throughput by increasing system capacity – it is GOOD!
But comes together with concurrent access to shared resources => you have to provide mutual exclusion (MutEx) by parallel threads when changing the resources' state (read only access can be shared by multiple threads)
Mutual exclusion can be achieved in several ways:
– synchronized – hardwired in HotSpot JVM, optimized in J^6
– ReentrantLock, ReadWriteLock, StampedLock → java.util.concurrent.locks.*
– Optimistic Locking → tryLock(), CAS
Parallelism & Concurrency
![Page 34: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/34.jpg)
34
Simple problem: incrementing a long value 500 000 000 times.
9 implementations:
‒ SynchronousCounter – while (counter++ < 500000000){}
‒ SingleThreadSynchronizedCounter – 1T using synchronized
‒ TwoThreadsSynchronizedCounter – 2T using synchronized
‒ SingleThreadCASCounter – 1T using AtomicLong
‒ TwoThreadsCASCounter – 2T using AtomicLong
‒ TwoThreadsCASCounterLongAdder – 1T using LongAdder
‒ SingleThreadVolatileCounter – 1T, memory barrier (volatile)
‒ TwoThreadsVolatileCounter – 2T, memory barrier (volatile)
Comparing Concurrent Impl.
![Page 35: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/35.jpg)
35
Test results (on my laptop - quad core Intel [email protected]):
− SynchronousCounter – 190ms
− SingleThreadSynchronizedCounter – 15000 ms
− TwoThreadsSynchronizedCounter – 21000 ms
− SingleThreadCASCounter – 4100 ms
− TwoThreadsCASCounter – 12000 ms
− TwoThreadsCASCounterLongAdder – 12800 ms
− SingleThreadVolatileCounter – 4100 ms
− TwoThreadsVolatileCounter – 20000 ms
Comparing Concurrent Impl.
![Page 36: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/36.jpg)
36
For more complete micro-benchmarking of different Mutex implementations see:
http://blog.takipi.com/java-8-stampedlocks-vs-readwritelocks-and-synchronized/
http://www.slideshare.net/haimyadid/java-8-stamped-lock
Comparing Concurrent Impl.
![Page 37: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/37.jpg)
37
Non-blocking (synchronous) implementation is 2 orders of magnitude better then synchronized
We should try to avoid blocking and especially contended blocking if want to achieve low latency
If blocking is a must we have to prefer CAS and optimistic concurrency over blocking (but have in mind it always depends on concurrent problem at hand and how much contention do we experience – test early, test often, microbenchmarks are unreliable and highly platform dependent – test real application with typical load patterns)
The real question is: HOW is is possible to build concurrency without blocking?
Mutex Comparison => Conclusions
![Page 38: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/38.jpg)
38
Message Driven – asynchronous message-passing allows to establish a boundary between components that ensures loose coupling, isolation, location transparency, and provides the means to delegate errors as messages [Reactive Manifesto].
The main idea is to separate concurrent producer and consumer workers by using message queues.
Message queues can be unbounded or bounded (limited max number of messages)
Unbounded message queues can present memory allocation problem in case the producers outrun the consumers for a long period → OutOfMemoryError
Scalable, Massively Concurrent
![Page 39: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/39.jpg)
39
Queues typically use either linked-lists or arrays for the underlying storage of elements. Linked lists are not „mechanically sympathetic” – there is no predictable caching “stride” (should be less than 2048 bytes in each direction).
Bounded queues often experience write contention on head, tail, and size variables. Even if head and tail separated using CAS, they usually are in the same cache-line.
Queues produce much garbage.
Typical queues conflate a number of different concerns – producer and consumer synchronization and data storage
Queues Disadvantages[http://lmax-exchange.github.com/disruptor/files/Disruptor-1.0.pdf]
![Page 40: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/40.jpg)
40
LMAX Disruptor design pattern separates different concerns in a “mechanically sympathetic” way:
- Storage of items being exchanged
- Producer coordination – claiming the next sequence
- Consumers coordination – notified new item is available
Single Writer principle is employed when writing data in the Ring Buffer from single producer thread only (no contention),
When multiple producers → CAS
Memory pre-allocated – predictable stride, no garbage
LMAX Disruptor (RingBuffer) [http://lmax-exchange.github.com/disruptor/files/Disruptor-1.0.pdf]
![Page 41: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/41.jpg)
41
LMAX Disruptor (RingBuffer) High Performance [http://lmax-exchange.github.com/disruptor/files/Disruptor-
1.0.pdf]
Source: LMAX Disruptor github wiki - https://raw.githubusercontent.com/wiki/LMAX-Exchange/disruptor/images/Models.png LMAX-Exchange Disruptor License @ GitHub: Apache License Version 2.0, January 2004 - http://www.apache.org/licenses/
![Page 42: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/42.jpg)
42
LMAX Disruptor (RingBuffer) High Performance [http://lmax-exchange.github.com/disruptor/files/Disruptor-
1.0.pdf]
Source: LMAX Disruptor @ GitHub - https://github.com/LMAX-Exchange/disruptor/blob/master/docs/Disruptor.docx LMAX-Exchange Disruptor License @ GitHub: Apache License Version 2.0, January 2004 - http://www.apache.org/licenses/
![Page 43: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/43.jpg)
Project Reactor
43
Reactor project allows building high-performance (low latency high throughput) non-blocking asynchronous applications on JVM.
Reactor is designed to be extraordinarily fast and can sustain throughput rates on order of 10's of millions of operations per second.
Reactor has powerful API for declaring data transformations and functional composition.
Makes use of the concept of Mechanical Sympathy built on top of Disruptor / RingBuffer.
![Page 44: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/44.jpg)
Project Reactor
44
Pre-allocation at startup-time Message-passing structures are bounded Using Reactive and Event-Driven Architecture patterns
=> non-blocking end-to-end flows, replies Implement Reactive Streams Specification – efficient
bounded structures requesting no more than capacity Applies above features to IPC and provides non-
blocking IO drivers that are flow-control aware Expose a Functional API – organize their code in a
side-effect free way, which helps you determine you are thread-safe and fault-tolerant
![Page 45: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/45.jpg)
Reactor Projects
45
https://github.com/reactor/reactor, Apache Software License 2.0
IPC – Netty, Kafka, Aeron
![Page 46: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/46.jpg)
Reactor Projects
46
![Page 47: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/47.jpg)
Reactor Flux
47
https://github.com/reactor/reactor-core, Apache Software License 2.0
![Page 48: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/48.jpg)
Reactor Mono
48
https://github.com/reactor/reactor-core, Apache Software License 2.0
![Page 49: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/49.jpg)
Example: Flux.combineLatest()
49
https://projectreactor.io/core/docs/api/, Apache Software License 2.0
![Page 50: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/50.jpg)
Reactor: Hello World
50
public static void main(String... args) throws InterruptedException { EmitterProcessor<String> emitter = EmitterProcessor.create(); BlockingSink<String> sink = emitter.connectSink(); emitter.publishOn(Schedulers.single()) .map(String::toUpperCase) .filter(s → s.startsWith("HELLO")) .delayMillis(1000).subscribe(System.out::println); sink.submit("Hello World!"); // emit - non blocking sink.submit("Goodbye World!"); sink.submit("Hello Trayan!"); Thread.sleep(3000);}
![Page 51: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/51.jpg)
Reactor: Flux Example
51
Flux.fromIterable(getSomeLongList()) .mergeWith(Flux.interval(100)) .doOnNext(serviceA::someObserver) .map(d -> d * 2) .take(3) .onErrorResumeWith(errorHandler::fallback) .doAfterTerminate(serviceM::incrementTerminate) .subscribe(System.out::println);
https://github.com/reactor/reactor-core, Apache Software License 2.0
![Page 52: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/52.jpg)
Reactor Bus: IPTPI Java Robot
52
![Page 53: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/53.jpg)
Disruptor (Ring Buffer) used in Reactor
53
Reactor provides 3 major types of Processors:
EmitterProcessor – using 0 threads (on same thread)
TopicProcessor using – N threads concurrently processing the messages (AND operation)
WorkQueueProcessor – N threads alternatively processing the messages (XOR operation – messages are processed exactly by one thread – load ballancing and work distribution)
![Page 54: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/54.jpg)
54
Meet IPTPI :)
![Page 55: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/55.jpg)
55
Ups...
![Page 56: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/56.jpg)
IPTPI: RPi2 + Ardunio Robot
56
Raspberry Pi 2 (quad-core ARMv7 @ 900MHz) + Arduino Leonardo cloneA-Star 32U4 Micro
Optical encoders (custom), IR optical array, 3D accelerometers, gyros, and compass MinIMU-9 v2
IPTPI is programmed in Java using Pi4J, Reactor, RxJava, Akka
More information about IPTPI: http://robolearn.org/iptpi-robot/
![Page 57: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/57.jpg)
IPTPI: RPi2 + Ardunio Robot
57
3D accelerometers, gyros, and compass MinIMU-9 v2
Pololu DRV8835 Dual Motor Driverfor Raspberry Pi
Arduino Leonardo cloneA-Star 32U4 Micro
USB Stereo Speakers - 5V
LiPo Powebank15000 mAh
![Page 58: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/58.jpg)
IPTPI: RPi2 + Ardunio Robot
58
Raspberry Pi 2 (quad-core ARMv7 @ 900MHz)
IR Optical Sensor QRD1114Array (Line Following)
Adafruit 2.8" PiTFT - Capacitive Touch Screen
![Page 59: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/59.jpg)
59
![Page 60: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/60.jpg)
LeJaRo: Lego® Java Robot
60
Modular – 3 motors (with encoders) – one driving each track, and third for robot clamp.
Three sensors: touch sensor (obstacle avoidance), light color sensor (follow line), IR sensor (remote).
LeJaRo is programmed in Java using LeJOS library.
More information about LeJaRo: http://robolearn.org/lejaro/
Programming examples available @GitHub: https://github.com/iproduct/course-social-robotics/tree/master/motors_demo
LEGO® is a registered trademark of LEGO® Group. Programs of IPT are not affiliated, sponsored or endorsed by LEGO® Education or LEGO® Group.
![Page 61: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/61.jpg)
Tale of Simplicity: DDD
61
![Page 62: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/62.jpg)
IPTPI Reactive Streams
62
EncoderReadings
ArduinoDataFluxion
ArduinoSerialData
PositionFluxion
RobotPositions
CommandMovementSubscriber
RobotWSService(using Reactor)
Angular 2 /TypeScript
MovementCommands
![Page 63: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/63.jpg)
IPTPI: IPTPIDemo I
63
public class IPTPIVDemo { ...public IPTPIDemo() { //receive Arduino data readings ArduinoData = ArduinoFactory.getInstance().createArduinoData();
//calculate robot positions PositionsFlux = PositionFactory.createPositionFlux( arduinoData.getEncoderReadingsFlux()); resentationViews.add( PositionFactory.createPositionPanel(positionsFlux));
//enable sending commands to Arduino ArduinoCommandsSub = ArduinoFactory.getInstance() .createArduinoCommandSubscriber();
/
![Page 64: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/64.jpg)
IPTPI: IPTPIDemo II
64
//Audio player - added @jPrime 2016 Hackergarten audio = AudioFactory.createAudioPlayer();
//wire robot main controller with services movementSub =MovementFactory.createMovementCommandSubscriber( positionsFlux, arduinoData.getLineReadingsFlux()); controller = new RobotController(this::tearDown, movementSub, arduinoCommandsSub, audio);
//create view with controller and delegate material views from query services view = new RobotView("IPTPI Reactive Robotics Demo", controller, presentationViews);
![Page 65: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/65.jpg)
IPTPI: IPTPIDemo III
65
//expose as WS service movementSub2 =MovementFactory.createMovementCommandSubscriber( positionsFlux, arduinoData.getLineReadingsFlux()); positionsService = new RobotWSService( positionsFlux, movementSub2);}
public static void main(String[] args) { // initialize wiringPi library Gpio.wiringPiSetupGpio(); try { IPTPIDemo demo = new IPTPIDemo(); } catch (IOException e) { e.printStackTrace(); }}
![Page 66: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/66.jpg)
IPTPI: ArduinoData I
66
positionsFlux = EmitterProcessor.create();positionsSink = positionsFlux.connectSink();lineFlux = EmitterProcessor.create();lineSink = lineFlux.connectSink();final Serial serial = SerialFactory.createInstance();serial.addListener(new SerialDataEventListener() { private ByteBuffer buffer = ByteBuffer.allocate(1024); public void dataReceived(SerialDataEvent event) { try { ByteBuffer newBuffer = event.getByteBuffer(); buffer.put(newBuffer); buffer.flip(); ... buffer.get(); long timestamp = buffer.getInt(); //get timestamp int encoderL = -buffer.getInt(); //motors mirrored int encoderR = buffer.getInt();
![Page 67: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/67.jpg)
IPTPI: ArduinoData II
67
EncoderReadings readings = new EncoderReadings(encoderR, encoderL, timestamp); emitter.submit(readings); ... buffer.compact(); } catch (Exception e) { e.printStackTrace(); } }});try { serial.open(PORT, 38400);} catch(SerialPortException | IOException ex) { System.out.println(“SERIAL SETUP FAILED:"+ex.getMessage());}
![Page 68: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/68.jpg)
IPTPI: PositionFlux I
68
ReduxPattern!
public PositionsFlux( Flux<EncoderReadings> readingsFlux) { this.encoderReadings = readingsFlux; Flux<EncoderReadings> skip1 = readingsFlux.skip(1); positionsFlux = Flux.zip(readingsFlux, skip1) .map(tupple -> .scan(new Position(0, 0, 0), (last, tupple) -> {
EncoderReadings prev = tupple.getT1();EncoderReadings curr = tupple.getT2();int prevL = prev.getEncoderL();int prevR = prev.getEncoderR();int currL = curr.getEncoderL();int currR = curr.getEncoderR();int sL = currL - prevL;int sR = currR - prevR;double alpha0 = last.getHeading();
![Page 69: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/69.jpg)
IPTPI: PositionFlux II
69
double alpha0 = last.getHeading();if(sR == sL) { return new Position((float)(last.getX() + sL *
ENCODER_STEP_LENGTH * cos(alpha0)), (float)(last.getY()+ sL * ENCODER_STEP_LENGTH *
sin(alpha0)), alpha0, curr.getTimestamp());} else {
… }
}) );}
![Page 70: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/70.jpg)
CommandMovementSubscriber I
70
public class CommandMovementSubscriber extends ConsumerSubscriber<Command<Movement>> { private PositionFluxion positions; public CommandMovementSubscriber(PositionFluxion positions){ this.positions = positions; Gpio.wiringPiSetupGpio(); // initialize wiringPi library Gpio.pinMode(5, Gpio.OUTPUT); // Motor direction pins Gpio.pinMode(6, Gpio.OUTPUT); Gpio.pinMode(12, Gpio.PWM_OUTPUT); // Motor speed pins Gpio.pinMode(13, Gpio.PWM_OUTPUT); Gpio.pwmSetMode(Gpio.PWM_MODE_MS); Gpio.pwmSetRange(MAX_SPEED); Gpio.pwmSetClock(CLOCK_DIVISOR); } @Override public void doNext(Command<Movement> command) { ... }}
![Page 71: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/71.jpg)
CommandMovementSubscriber II
71
private void runMotors(MotorsCommand mc) { //setting motor directions Gpio.digitalWrite(5, mc.getDirR() > 0 ? 1 : 0); Gpio.digitalWrite(6, mc.getDirL() > 0 ? 1 : 0); //setting speed if(mc.getVelocityR()>=0 && mc.getVelocityR() <=MAX_SPEED) Gpio.pwmWrite(12, mc.getVelocityR()); // set speed if(mc.getVelocityL()>=0 && mc.getVelocityL() <=MAX_SPEED) Gpio.pwmWrite(13, mc.getVelocityL()); }}
![Page 72: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/72.jpg)
Reactor IO – NetStreams API
72
http://projectreactor.io/io/docs/reference/, Apache License 2.0
![Page 73: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/73.jpg)
Takeaways: Why Go Reactive?
73
Benefits using Reactive Programming + DDD:
DDD helps to manage complexity in IoT and Robotics - many subsystems = sub-domains
Reactive Streams (Fluxes, Monos) = uni-directional data flows, CQRS, event sourcing, microservices
Reactive Streams can be non-blocking and highly efficient, or can utilize blocking if needed
Naturally implement state management patterns like Redux, allow time travel, replay and data analytics
Clear, declarative data transforms that scale (Map-Reduce, BigData, PaaS)
![Page 74: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/74.jpg)
Takeaways: Why Maybe Not?
74
Cons using Reactive Programming + DDD: DDD requires additional efforts to clearly separate
different (sub) domains – DSL translators, factories...
Reactive Streams utilize functional composition and require entirely different mindset then imperative – feels like learning foreign language
Pure functions and Redux provide much benefits,
but there's always temptation to “do it the old way” :)
Tool support for functional programming in Java is still not perfect (in Eclipse at least :)
![Page 75: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/75.jpg)
Where to Find the Demo Code?
75
IPTPI Reactive Demo is available @ GitHub:
https://github.com/iproduct/jprime-demo
![Page 76: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/76.jpg)
76
Resources: RxMarbles & Rx Coans
RxMarbles: http://rxmarbles.com/
RxJava Koans – Let's try to solve them at:https://github.com/mutexkid/rxjava-koans
RxJS Koans – for those who prefer JavaScript :) https://github.com/Reactive-Extensions/RxJSKoans
![Page 77: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/77.jpg)
Tale of Simplicity: DDD
77
http://robolearn.org/ Let's move!http://iproduct.org/
![Page 78: Java & JavaScipt Reactive Robotics and IoT 2016 @ jProfessionals](https://reader034.vdocuments.us/reader034/viewer/2022052418/586f78f51a28ab10258b6deb/html5/thumbnails/78.jpg)
Thank’s for Your Attention!
78
Trayan Iliev
CEO of IPT – Intellectual Products & Technologies
http://iproduct.org/
http://robolearn.org/
https://github.com/iproduct
https://twitter.com/trayaniliev
https://www.facebook.com/IPT.EACAD
https://plus.google.com/+IproductOrg