On C++, Javascript and WebSockets - Alex Fabijanic - CppCon 2015
On C++, Javascript and WebSockets - Alex Fabijanic - CppCon 2015
Alex Fabijanic
[email protected]
@0x00FA
aleks-f
Contents
- POCO
- Remoting
- OSP
- Javascript integration
- WebSockets
- macchina.io
POCO C++ Libraries
MySQL
SQLite
ODBC
> on GitHub since 2012
XML JSON Zip
Application
400+ forks
30-50 clones/day C++ and C Standard Libraries
Bundles
install, resolve, start, stop manage bundle versions
and uninstall bundles and dependencies
Bu
nd
em le
le
t
ag yc
en
M
an C
an
M Life
ag
em
POCO Core Libraries
en
(Foundation, XML, Util, Net)
t
Portable Runtime
Environment
Operating
System Std. C/C++
es
Libraries
Se
API
ic
rv
rv
ice
Se
Re
d
ar
gi
nd
st
ry
a
web server,
St
web- and console-
provide services to other
based management,
bundles and find services
user authentication
provided by other bundles
and authorization,
preferences, etc.
application-specific
functionality and services
OSP & friends
Combining POCO C++ Libraries and V8
> JavaScript is single-threaded and garbage-collected
> POCO is multithreaded
> Make C++ object available to JavaScript
easy for static objects, just provide Wrapper
> Allow JavaScript code to create C++ objects
easy if you don’t care about memory/resource leaks
> Register a callback function called by GC when object is deleted
allows you to properly delete underlying C++ object
> However, V8 does not do callbacks when script ends
wrapped C++ objects won’t be deleted, leaks resulting
> Need to track every C++ object a script creates and clean up
afterwards :-(
Remoting
// TimeService.h
// OR
//@ remote
std::string currentTime() const;
};
RemoteGen.xml
<AppConfig>
<RemoteGen>
<files>
<include>
${POCO_BASE}/RemotingNG/include/Poco/RemotingNG/RemoteObject.h
${POCO_BASE}/RemotingNG/include/Poco/RemotingNG/Proxy.h
${POCO_BASE}/RemotingNG/include/Poco/RemotingNG/Skeleton.h
include/TimeService.h
</include>
</files>
<output>
<mode>server</mode>
<include>include</include>
<src>src</src>
<namespace>Sample</namespace>
<copyright>Copyright (c) 2012</copyright>
</output>
<compiler>
<exec>cl</exec>
<options>
/I "${POCO_BASE}/RemotingNG\Foundation\include"
/I "${POCO_BASE}/RemotingNG\RemotingNG\include"
/nologo
/C
/P
/TP
</options>
</compiler>
</RemoteGen>
</AppConfig>
Other compilers
<compiler id="gcc">
<exec>g++</exec>
<options>
-I${POCO_BASE}/RemotingNG/Foundation/include
-I${POCO_BASE}/RemotingNG/include
-I./include
-E
-C
-o%.i
</options>
</compiler>
<compiler id="clang">
<exec>clang++</exec>
<options>
-I${POCO_BASE}/Foundation/include
-I${POCO_BASE}/RemotingNG/include
-I./include
-E
-C
-xc++
-o%.i
</options>
</compiler>
TimeService.h RemoteGen.xml
RemoteGen
<<registers>>
<<registers>> <<invokes>> <<creates>>
Registers
Skeleton, RemoteObject Registers ProxyFactory
and EventDispatcher (if with the Object Request
needed) with the Object Broker.
Request Broker.
Parent for Proxy and RemoteObject
ITimeService();
virtual ~ITimeService();
std::string uri(“http://server:8080/soap/TimeService/TheTimeService");
//@ remote
class Sensor: public Device
{
public:
Poco::BasicEvent<const double> valueChanged;
tempSensor.on(‘valueChanged', function(ev) {
var temp = ev.data;
// ...
});
if (tempSensor.ready())
{
var temp = tempSensor.value();
// ...
}
Web Event Service
WebEvent at a Glance
org.poco.events.someEvent
org.poco.events.anotherEvent
WebEvent Code - Server Side
ServiceRef::Ptr pWebEventSvcRef =
_pContext>registry().findByName(WebEventService::SERVICE_NAME);
WebEventService::Ptr pWebEventService =
pWebEventServiceRef->castedInstance<WebEventService>();
Poco::BasicEvent<WebNotificationEvent>& notification =
pWebEventService->subjectNotified(“org.poco.demo");
//…
ServiceRef::Ptr pWebEventSvcRef =
_pContext>registry().findByName(WebEventService::SERVICE_NAME);
WebEventService::Ptr pWebEventService =
pWebEventSvcRef->castedInstance<WebEventService>();
function onOpen(response) {
ws.subscribe("org.poco.demo");
}
function onMessage(response) {
var subject = response.data.split(" ")[1];
if (subject.lastIndexOf(uid) != -1) return;// ignore echo
var data = response.data.split("\r\n")[1];
// …
}
Events
WebSockets
Network
Demo
macchina .io
A modular open source toolkit for building
embedded IoT applications that connect sensors,
devices and cloud services.
devices.net
Cloud Services
Devices/Sensor Networks
CoAP, IEEE 802.15.4,
AirVantage, Bluemix, Modbus, USB,
Tinamous, Xively, etc. Bluetooth,
RS-232
HTTP(S)
MQTT
device apps
Mobile/Web local “business logic”
Clients IoT Gateway web services
web visualization
database
Remote Access
discoverability
my-devices.net
> open source (Apache 2.0 License)
> built in C++ for best performance and efficiency
(JavaScript for parts of web interface)
> modular and extensible
> mature, proven codebase:
POCO C++ Libraries, Google V8, Eclipse Paho, SQLite
AngularJS, jQuery, OpenLayers, Ace (text editor),
+ Applied Informatics OSP and Remoting frameworks
> C++ - to - JavaScript bridge
> Raspberry Pi, Beaglebone, Edison, RED, MangOH, etc.
> prototype on Linux or OS X host, easily deploy to device
> web interface with JavaScript editor
Pro Users and Device Manufacturers
> add device specific APIs
> make devices programmable in JavaScript for partners or end
users
> device specific app store (sell additional software features)
> additional frameworks (UPnP, Remoting SOAP and JSON-RPC)
> customizable web user interface
> improved user authentication and authorization
> signed bundles
> pro support, more info http://www.appinf.com
Conclusion
Q&A