32

Universal Declarative Services

Embed Size (px)

DESCRIPTION

Slides for my talk at the OSGi Community Event / Eclipse Con Europe 2012. See http://www.eclipsecon.org/europe2012/sessions/universal-declarative-services for abstract.

Citation preview

Page 1: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Universal Declarative ServicesA component framework for C++ and a model-driven solution to

polyglot components

Simon Chemouil1

1Global Vision Systems

OSGi Community Event, 2012

Simon Chemouil Universal Declarative Services

Page 2: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Who am I?

Free Software enthusiast, functional programmer, interested inmodel-driven development, modularity, re-usability,maintainability. . .

�How to never have to write that piece of software again?�

Technical Architect at Global Vision Systems, Toulouse

We create data visualization software for large industries (2D,3D, tables, etc).We do both Java and C++

Java using OSGi (Felix, iPojo) for server and client side(Eclipse RCP and Android)C++ to keep a fast 3D engine-agnostic manipulation layer

Simon Chemouil Universal Declarative Services

Page 3: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Outline

1 MotivationThe case for modularityService Components and C++

2 Implementing Declarative Services for C++Challenges in C++De�ning components

3 Going UniversalSimpler Component De�nitionsWhat's next?

4 SummaryConclusion

Simon Chemouil Universal Declarative Services

Page 4: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Why modularity?Service Components

Native OSGi

Did you miss �Native-OSGi, Modular Software Development ina Native world� by Alexander Broekhuis and Sascha Zelzer?

�Universal OSGi�

an RFP and a blog post by Peter Kriens in 2007.

Apply OSGi techniques to native (C/C++) development.

Simon Chemouil Universal Declarative Services

Page 5: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Why modularity?Service Components

An OSGi Architect Walks in a C++ Bar...

Our C++ project going through re-architecture / refactoring

Custom-made plugin system, limited.

Plugins can't use each other

C/C++ OSGi ports:

Celix (Alexander), CppMicroServices (Sascha), SOF, CTK,nOSGi, . . .

Yet no service component framework such as DeclarativeServices

Simon Chemouil Universal Declarative Services

Page 6: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Why modularity?Service Components

Why a Service Component Framework?

Services: powerful primitives, unpractical programming:

Lack of structureDynamic

Service Components:

Structure the servicesA nice semantic model to reason about dynamism

Why is there no such framework for an OSGi C++ port:

1 Technical challenges2 It was just not done yet!

Simon Chemouil Universal Declarative Services

Page 7: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Why modularity?Service Components

Why Declarative Services?

Many existing component frameworks for C++

Just as there were some for Java before DSNo modularity

Simple and matured component model

A few years of personal experience

Works with POJOs (or POCOs!)

Components do not depend on the frameworkMakes them testable and easily reusable!

Simon Chemouil Universal Declarative Services

Page 8: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Why modularity?Service Components

Does it make sense for C++?

We use C++ for performance:

Performance hit with component frameworks?

We need extensibility and customization...

Do we need modularity?

Why use di�erent architecture between Java and C++?

What if we could talk about components, whatever theimplementation language?

Simon Chemouil Universal Declarative Services

Page 9: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Getting started

Native OSGi implementation:

CppMicroServices is light, works with any shared libraryInspired by PojoSRFor C++!

Bundles are called Modules:

Native shared libraries: not archives.

Moving to NativeOSGi later?

Easy! Components are framework-agnostic!

Simon Chemouil Universal Declarative Services

Page 10: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Ingredients in Declarative Services

Components are described in XML �les in the bundle

Service callbacks

De�ned in descriptionService arrival/departurePolicy, Cardinality, Target

Component callbacks

Activation, modi�cation, deactivation

Simon Chemouil Universal Declarative Services

Page 11: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Component Descriptions in C++

XML descriptions. . .

We have native shared libraries, not JARs!

Instead, we register a ComponentDescriptor instance to theComponentManager service.

The ComponentManager service is provided by the coreDs4Cpp framework.

Simon Chemouil Universal Declarative Services

Page 12: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Component Descriptions in C++

De�nition

c l a s s ComponentDescr ip to r {pub l i c :

const s t r i n g componentId ;const s t r i n g imp lSha r edOb j ec t ;const vec to r<s t r i n g >∗ p r o v i d e d S e r v i c e s ;v e c to r<ComponentReference>∗ r e f e r e n c e s ;const bool immediate ;const bool autoEnab l e ;

} ;

Simon Chemouil Universal Declarative Services

Page 13: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Component Descriptions in C++

Let's see how it looks like in practice. . .

Simon Chemouil Universal Declarative Services

Page 14: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Extensible Hello World!

Simon Chemouil Universal Declarative Services

Page 15: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Component Descriptions in C++

De�nition

s t d : : v e c to r<ComponentReference>∗ r e f e r e n c e s = . . . ;r e f e r e n c e s −>push_back (

ComponentReference ( " greetdemo : : G r e e tP r o v i d e r " , "greetdemo : : G r e e tP r o v i d e r " ,

s t d : : s t r i n g ( ) , ComponentReference : : DYNAMIC,ComponentReference : : MULTIPLE ,ComponentReference : : OPTIONAL_REF) ) ;

s t d : : v e c to r<s td : : s t r i n g > s e r v i c e s ;s e r v i c e s . push_back ( " greetdemo : : GreetManager " ) ;

ComponentDescr iptor ∗ componentDesc =new ds4cpp : : ComponentDescr iptor (" greetdemo : : GreetManager Impl " , "" ,s e r v i c e s , ∗ r e f e r e n c e s , t rue , t r ue ) ;

Simon Chemouil Universal Declarative Services

Page 16: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

A Component in C++

De�nition

c l a s s GreetManager Impl : p ub l i c GreetManager {p ub l i c :

GreetManager Impl ( ) ; v i r t u a l ~GreetManager Impl ( ) ;vo id a c t i v a t e ( ) ;vo id addGr e e tP rov i d e r ( G r e e tP r o v i d e r ∗) ;vo id r emoveGree tP rov i d e r ( G r e e tP r o v i d e r ∗) ;

// S e r v i c e methods .s t r i n g g e tDe f au l tTa r g e t ( const s t r i n g &) const ;s t r i n g g e tG r e e t i n g ( const s t r i n g &, const s t r i n g &)

const ;l i s t <s t r i n g > g e t A l l G r e e t i n g s ( const s t r i n g &) const ;l i s t <s t r i n g > ge tAva i l a b l e L anguag e s ( ) const ;

} ;

Simon Chemouil Universal Declarative Services

Page 17: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Service Callbacks in C++ (1/2)

No re�ection/introspection!

There are some limited libraries (e.g Re�ex).

C++ name mangling

We can't guess the binary name

Instead, we force a convention in the C++ component:

set<RefName>, unset<RefName> for single dependenciesadd<RefName>, remove<RefName> for multipledependencies

Simon Chemouil Universal Declarative Services

Page 18: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

Service Callbacks in C++ (2/2)

Our tools: dlopen/dlsym/dlclose (and Windows equivalents)

Against us: C++ name mangling

Solutions?

Re�ection libraries: too limited, not dynamic.C++11 attributes (≈ Java annotations): still largelyunsupportedQt meta-objects? Not bad!

Yet need a custom preprocessorStill not modular (work in progress)

How about C wrappers?

Di�cult to write manually, but provide universal ABI!

Simon Chemouil Universal Declarative Services

Page 19: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

C Wrapper

De�nition

e x t e r n "C" {DS_ABI_EXPORT GreetManager ImplWrapper∗

__greetdemo__GreetManagerImpl__create ( ) {r e t u r n new GreetManager ImplWrapper ;

}DS_ABI_EXPORT v o i d __greetdemo__GreetManagerImpl__activate (

GreetManager ImplWrapper∗ o b j e c t ) {ob j e c t −>a c t i v a t e ( ) ;

}DS_ABI_EXPORT v o i d

__greetdemo__GreetManagerImpl__add_greetdemo__GreetProvider (GreetManager ImplWrapper∗ ob j e c t , : : us : : Base ∗ s e r v i c e ) {

greetdemo : : G r e e tP r o v i d e r ∗ l s e r v i c e = dynamic_cast<greetdemo : :G r e e tP r o v i d e r ∗>( s e r v i c e ) ;

ob j e c t −>addGre e tP rov i d e r ( l s e r v i c e ) ;}DS_ABI_EXPORT v o i d

__greetdemo__GreetManagerImpl__remove_greetdemo__GreetProvider (GreetManager ImplWrapper∗ ob j e c t , : : us : : Base ∗ s e r v i c e ) {

greetdemo : : G r e e tP r o v i d e r ∗ l s e r v i c e = dynamic_cast<greetdemo : :G r e e tP r o v i d e r ∗>( s e r v i c e ) ;

ob j e c t −>removeGree tP rov i d e r ( l s e r v i c e ) ;} } // e x t e r n "C"

Simon Chemouil Universal Declarative Services

Page 20: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Challenges in C++De�ning components

It works! But. . .

The proof-of-concept is successful:

We have components wired dynamically

OSGi's patterns work nicely (yet a bit more tricky)

We support the use cases we need

Dynamic arrival, thread-safety planned but un�nished, serviceremoval to do.

But no one would write that wrapper code!

Are there solutions to help us here?

Simon Chemouil Universal Declarative Services

Page 21: Universal Declarative Services

Demo Time!

Page 22: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

C++ is too complex!

Automatically generating the wrappers?

Where to get the description?

Macros?Parsing C++?!

Xtext

Java tooling to de�ne DSLs and compilersGenerates an Eclipse editor!Uses EMF as backend, so more can be built upon.

Simon Chemouil Universal Declarative Services

Page 23: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

Back to GreetManagerImpl!

De�nition

component greetdemo . GreetManager Impl {prov ides {

greetdemo . GreetManager}re fe rences {

dynamic se r v i c e greetdemo . G r e e tP r o v i d e r [ 0 . . n ]}

}

Simon Chemouil Universal Declarative Services

Page 24: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

De�ning multiple components

De�nition

module DSDemo {name = "DS Gree t Demo"vers ion = 1 . 0 . 0i nc ludes {

greetdemo . E n g l i s hG r e e tP r o v i d e rgreetdemo . F r en chG r e e tP r o v i d e rgreetdemo . GreetManager Implgreetdemo . Con so l eG r e e t e r

}}

Simon Chemouil Universal Declarative Services

Page 25: Universal Declarative Services

Generating C++ code

Page 26: Universal Declarative Services

Demo Time!

Page 27: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

Improving our ADL

The ADL we designed maps to Declarative Services'capabilities

What about richer component models?

Creating new target language back-ends

Java/DS, Java/iPojoMaking it extensible

Simon Chemouil Universal Declarative Services

Page 28: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

Bridging Java and C++

Using Remote OSGi Services:

An idea discussed in NativeOSGi.We expose C++ services where we can generate a Javainterface

Use JNI/JNA bindings or network.

Basically, we bridge the two service registries!

It does sound like CORBA

Simon Chemouil Universal Declarative Services

Page 29: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Simpler Component De�nitionsWhat's next?

Creating UI Tools

Textual DSLs are nice...

But component diagrams are nicer!

Up-to-date component metadata is extremely useful:

Forces high-level design (round-trips between architects anddevelopers)Allows code generation/rapid project bootstrappingStatic analysis (code conformance)Can be used for SDK documentation.

Long term project!

Simon Chemouil Universal Declarative Services

Page 30: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Conclusion

Declarative Services for C++

We have been using Ds4Cpp for a few months!

If you want to try it. . . you can! We just open sourced it(Apache Software License 2.0)

https://github.com/Global-Vision-Systems/Ds4Cpp

(the demo is packaged with it)!

We need testers and eyes to �x my bad C++ :-)

And remove the limitations (talk to me!)

Simon Chemouil Universal Declarative Services

Page 31: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Conclusion

Universal Components Tooling

The tooling has still some rough edges

Enough for our current needs. . .

Some clean-up required.

We have a roadmap for the improvements.

Simon Chemouil Universal Declarative Services

Page 32: Universal Declarative Services

MotivationImplementationGoing Universal

Summary

Conclusion

Universal Declarative Services

Questions?

Twitter:

@simach

[email protected]

Simon Chemouil Universal Declarative Services