Escalier v0.1 Released - Angelo Corsaro

Posted on 11th June 2011

The other nice bonus that comes with Escalier is that it allows you to write DDS applications as Scala script. Likewise, you can start a Scala interpreter and start to create DDS entities that publish or subscribe to data -- pretty cool!

Escalier promotes a reactive programming style for DDS applications and in upcoming releases will provide a complete Functional Reactive Programming framework for writing DDS applications (probably based on scala.react).

As I'll show you in the next code snipped, with Escalier you can write DDS applications in a few lines of code. In addition, when you want to write a simple application that connects to the default domain and the default partition, Escalier actually automatically creates DomainParticipants, Publisher/Subscriber under the hood leaving you with the only effort of having to define your topics, DataReaders and DataWriters.

To keep things simple let's assume we want to write a kind of Twitter using DDS. The first things to do is to define the type for the twits, which can be quickly done by defining the following IDL structure.

1.struct Twit {
2. string user;
3. string message;
4.}
5.#pragma keylist Twit user

At this point we can write our DDS application that allows us to write Twits: 

01.package dds.demo.twitter
02.import java.util.Scanner
03.import dds._
04.import dds.pub._
05.import dds.demo.twitter.gen.Twit
06.object TwitsWriter {
07. def main(args: Array[String]) {
08.  if (args.length < 2) {
09.   println("TwitsWriter  ")
10.   sys.exit(1)
11.  }
12.  val console = new Scanner(System.in)
13.  // Create a Tweet Topic in the default domain with default QoS
14.  val twits: Topic[Twit] = Topic[Twit]("Twits")
15.  // Create a DataWriter for writing Tweet
16.  val writer = DataWriter[Twit](twits)
17.  val tweet = new Twit
18.  tweet.name = args(0)
19.  val count = args(1).toInt
20.  for (i <- 1 to count) {
21.   print(tweet.name + ":> ")
22.   tweet.msg = console.nextLine()
23.   writer ! tweet
24.  }
25. }
26.}

Now, let's write the DDS applications to display all the twits from the people we are interested in: 

01.package dds.demo.twitter
02.import dds.{DomainParticipant, Topic}
03.import dds.demo.twitter.gen.{Twit,TwitSeqHolder}
04.import dds.sub.Subscriber
05.import dds.sub.DataReader
06.import dds.qos.{KeepLastHistory, DataReaderQos}
07.import dds.event.DataAvailable
08.   
09.object TwitsReader {
10. def main(args : Array[String]) : Unit = {
11.  // Create a Tweet Topic in the default Domain with default Qos
12.  val t: Topic[Twit] = Topic[Twit]("Twits")
13.  // Create a DataReader for reading Tweets
14.  val qos = DataReaderQos() <= KeepLastHistory(10)
15.  val reader = DataReader[Twit](t, qos)
16.  // Add a reaction to the Twits reader
17.  reader.reactions += {
18.   case d: DataAvailable[Twit] => {
19.     (d.reader read)foreach(t => println(t.name +" :> "+ t.msg))
20.   }
21.  }
22.  Thread.currentThread.join()
23. }
24.}
25.  

That's it, now you can  try it yourself by downloading the latest version of Escalier. Have Fun!

A+

Tagged with: