Tuesday, April 29, 2014

Reite = R + Delite

Delite - http://stanford-ppl.github.io/Delite/
Relite - https://github.com/TiarkRompf/Relite (based on the FastR)
FastR - https://github.com/allr/fastr

Delite is a compiler framework and runtime for parallel embedded DSLs.
Delite provides:
- Built-in parallel execution patterns
- Optimizers for parallel code
- Code generators for Scala, C++ and CUDA
- A heterogeneous runtime for executing DSLs

With Delite, parallelized using 8 threads, we get about a 10x speedup over GNU R (and about 4x over FastR).

 
http://sandeeptata.blogspot.com/2012/08/scala-dsls-and-big-data.html

Monday, April 28, 2014

How to pass actor messages through websocket ?

http://stackoverflow.com/questions/21851444/scala-play-akka-websocket-how-to-pass-actor-messages-through-websocket

I have an actor which is launched with application, running in the background watching for certain changes and if there are any reporting them. At the moment it just a println to the console. What I need to do is whenever there is a new message - send it to the front end using Websocket.
This is my Play Global object where the monitoring/listening actor is launched:
object Global extends GlobalSettings {

    override def onStart(app: Application) {

        class Listener extends Actor {
            //This needs to be changed to pass messages to Websocket, how?
            def receive = {
                case Create(path) => println("CREATE " + path)
                case Delete(path) => println("DELETE " + path)
                case Modify(path) => println("MODIFY " + path)
            }
        }

        val listener = Akka.system.actorOf(Props[Listener], "listener")
        val swatch = Akka.system.actorOf(Props[SwatchActor], "swatch")
        swatch ! Watch("/folder/path", Seq(Create, Modify, Delete), true, Some(listener))

    }

}
This is my Play controller:
object Application extends Controller {

    def test = WebSocket.using[String] { request =>

        //This hopefully gets the listener actor reference?
        val listener = Akka.system.actorSelection("/user/listener")

        val (out, channel) = Concurrent.broadcast[String]
        val in = Iteratee.foreach[String] { msg =>
            //Actor messages must be pushed here, how?
            channel push("RESPONSE: " + msg)
        }

        (in, out)

    }   

}
I understand that in order for websocket connection to be established there has to be an initial "in".
So my problems are:
  1. How do I modify the Listener actor to push messages to Websocket?
  2. What do I need to do to prepare the actor to push messages once the websocket connection is established?
  3. How do I push messages from the listener actor to the websocket?

Saturday, April 26, 2014

Skinny Framework

http://skinny-framework.org/
https://github.com/skinny-framework

Skinny Framework


Skinny is a full-stack web app framework built on Scalatra.
To put it simply, Skinny framework’s concept is Scala on Rails. Skinny is highly inspired by Ruby on Rails and it is optimized for sustainable productivity for Servlet-based web app development.
What’s more, Skinny’s components are basically independent from Skinny app environment. If you prefer using only Skinny ORM, Validator module or else, it’s also possible. We hope Skinny components help developers that use other frameworks too.

Why Skinny?


What does the name Skinny actually mean? We have three reasons as follows.

Application should be skinny

All the parts of a web application - controllers, models, views, routings and other settings - should be skinny. If you use Skinny framework, you can do without a lot of non-essential boilerplate code. For instance, when you create a simple registration form, all you need to do is define the parameters and validation rules and create view templates in an efficient way (ssp, scaml, jade, FreeMarker or something else) in most cases.

Framework should be skinny

Even if you need to investigate Skinny’s internals, don’t worry. Skinny keeps itself skinny, too. We believe that if the framework is well-designed, the resulting implementation will be skinny.

“su-ki-ni” in Japanese means “as you like it”


A sound-alike word “好きに (su-ki-ni)” in Japanese means “as you like it”. This is only half kidding but it also represents Skinny’s concept. Skinny framework should provide flexible APIs to empower developers as much as possible and shouldn’t get in the way.

Tuesday, April 22, 2014

OpenLayers vs OpenStreetMap

http://terokarvinen.com/2012/getting-started-with-openlayers-and-openstreetmap

Getting Started with OpenLayers and OpenStreetmap

Add a free, interactive map on your homepage. Just plain HTML5 and JavaScript, no special server needed. Live demo with source.
No API keys needed. And if you really like it, you can later download planet vector data for free!
Prequisites: HTML5, JavaScript

Source Code Explained

< !doctype html >
The new HTML5 Doctype. Some older examples won’t work with HTML5. Especially, you must make sure that #map div is larger than 0×0.
< meta charset="utf-8" / >
Always define the charset in document. UTF-8 is the new black.
< div id="map" style="top: 0; left: 0; bottom: 0; right: 0; position: fixed;" >
Fill parent with just CSS. This is the preferred method.
< script src="http://openlayers.org/api/OpenLayers.js" >< /script >
For good performance, JavaScript should be last, just before /body. OpenLayers.js should be called before your own code using it. To speed things up, you could also copy OpenLayers.js to your server.
map = new OpenLayers.Map("map");
OpenLayers.Map(divName) is the main container for everything. It includes layers, all the vectors and point of interest (POI) markers.
map.addLayer(new OpenLayers.Layer.OSM());
OpenLayers.Layer.OSM() is the standard OpenStreetMap Mapnik layer. This is what you normally want.
var lonLat = new OpenLayers.LonLat(24.9342, 60.2017)
 .transform(
    new OpenLayers.Projection("EPSG:4326"), // from WGS 1984
    map.getProjectionObject() // to Spherical Mercator
 );
You can get coordinates from OpenStreetMap.org regular web interface. Just navigate to your point of interest, using search if needed. Then click “Permalink” on bottom right. The URL shows latitude and longitude. Be careful not to mix lat and lon: they have different order in the URL and in OpenLayers.LonLat() constructor.
map.setCenter (lonLat, 14);

Getting started with the Play framewotk

http://brikis98.blogspot.com/2014/03/the-ultimate-guide-to-getting-started.html

Are you trying to get started with the Play Framework? Struggling to wrap your head around Futures, SBT, Scala, Functional Programming, or Iteratees? Then you've come to the right place.

This post is a collection of the best resources I've found for getting started with Play. I've broken it down by category to make it easier to browse and jump to the topic you're most interested in.

The list below is not meant to be comprehensive documentation, but rather, a collection of resources that cover the main areas where new Play users tend to get stuck. If you've got any great resources that are missing from the list below, leave a comment!

Introduction to Play

  1. Play Framework Documentation: the official docs are a must read to get a solid starting point.
  2. The Play Framework at LinkedIn: Performance and Productivity at Scale: video intro to Play and why LinkedIn uses it. See the accompanying slides and blog post.
  3. Typesafe Activator: very easy way to get started with Play. A simple script you run to generate Play app skeletons from the many available templates - including Hello Play (Scala)Hello Play (Java)Realtime and Reactive Play appsPlay with SlickPlay with AngularJS - and an in-browser UI that interactively walks you through changing, running, and testing those apps

Sunday, April 20, 2014

How do I change the default port (9000) that Play uses ?

http://stackoverflow.com/questions/8205067/how-do-i-change-the-default-port-9000-that-play-uses-when-i-execute-the-run


Play 1.x

Change the http.port value in the conf/application.conf file or pass it command line:
play run --http.port=8080

Play 2.x - Dev Mode

For browser-reload mode:
play "run 8080"
For continuous-reload mode:
play "~run 8080"

Play 2.x - Debug Mode

To run in debug mode with the http listener on port 8080, run:
play debug "run 8080"

Play 2.x - Prod Mode

Start in Prod mode:
play "start -Dhttp.port=8080"

Play 2.x - Staged Distribution

Create a staged distribution:
play stage
For Play 2.0.x and 2.1.x use the target/start script (Unix Only):
target/start -Dhttp.port=8080
For Play 2.2.x use the appropriate start script in the target/universal/stage/bin directory:
target/universal/stage/bin/[appname] -Dhttp.port=8080
With Play 2.2.x on Windows:
target\universal\stage\bin\[appname].bat -Dhttp.port=8080

Play 2.x - Zip Distribution

To create a zip distribution:
play dist
For Play 2.0.x and 2.1.x use the start script (Unix Only) in the extracted zip:
start -Dhttp.port=8080
For Play 2.2.x use the appropriate script in the [appname]-[version]/bin directory:
[appname]-[version]/bin/[appname] -Dhttp.port=8080
With Play 2.2.x on Windows:
[appname]-[version]\bin\[appname].bat -Dhttp.port=8080

Thursday, April 17, 2014

How to run an Actor ?

http://doc.akka.io/docs/akka/2.2.0/scala/hello-world.html

As a Scala developer you will probably want to tell us that there is no main(Array[String]) method anywhere in these classes, so how do we run this program? The answer is that the appropriate main method is implemented in the generic launcher class akka.Main which expects only one command line argument: the class name of the application’s main actor. This main method will then create the infrastructure needed for running the actors, start the given main actor and arrange for the whole application to shut down once the main actor terminates. Thus you will be able to run the above code with a command similar to the following:
> java -classpath <all those JARs> akka.Main com.example.HelloWorl

import akka.actor.Actor
import akka.actor.Props
class HelloWorld extends Actor {
  override def preStart(): Unit = {
  // create the greeter actor
  val greeter = context.actorOf(Props[Greeter], "greeter")
  // tell it to perform the greeting
  greeter ! Greeter.Greet
  }

def receive = {
  // when the greeter is done, stop this actor and with it the application
  case Greeter.Done ⇒ context.stop(self)
  }
}


object Greeter { 
  case object Greet
  case object Done
}
class Greeter extends Actor {

  def receive = {
   case Greeter.Greet ⇒
   println("Hello World!")
   sender ! Greeter.Done
  }
}

 

Tuesday, April 15, 2014

Simple Scala REST example using Spray

http://sysgears.com/articles/building-rest-service-with-scala/
https://github.com/oermolaev/simple-scala-rest-example

Enterprise application might be developed using a variety of architectural styles. And REST is one of the most powerful of them. It allows us to build simple, scalable, highly productive APIs with independent components on the basis of widespread standards like HTTP, MIME etc, engaging their true potential.
Let's discuss how to create a lightweight, but full-featured RESTful service from scratch.
Consider building REST [1] service that doesn't contain any complicated functionality, but provide basic CRUD operations and have following HTTP endpoints as an API:
  • POST /customer/ to create Customer.
  • GET /customer// to retrieve specific Customer.
  • PUT /customer// to update specific Customer.
  • DELETE /customer// to delete specific Customer.
  • GET /customer/ to search for Customers with specific parameters.

Technology stack

Scala was chosen as the foundation for the REST service we are going to implement. On the Scala website [2] it is described as a "general purpose programming language designed to express common programming patterns in a concise, elegant and type-safe way. Also, it smoothly integrates features of object-oriented and functional languages."
Despite the fact that Scala is a relatively new language and may have some drawbacks, there are several attractive elements, that made it not just an "another new programming language":
  • Operating on the JVM. It is known fact that Java is de facto the most popular programming language for enterprise. Many libraries are written in Java. A variety of tools are designed for JVM. Environments have been stable for years. It can be rather risky to change the entire programming stack even if this step may provide apparent benefits. But Scala has a great interoperability with existing Java code because it still operates on the JVM, has compatible byte code and allows Scala applications to use most of the JVM libraries. Furthermore, developers are able to apply their existing Java skills after migration. Therefore, Scala could be integrated into enterprise infrastructure quite smoothly.
  • Functional programming. Except that Scala is a pure object-oriented language it provides syntactic sugar for functional programming. Like a pattern matching, anonymous and higher-order functions, carrying, immutable collections, etc.
  • Concise and powerful syntax. Code size is reduced significantly compared to an equivalent application written in Java. This may improve development performance: less key-strokes to make, easier code review and testing. Moreover, many features like function passing and type inference can reduce syntactic overhead.
  • Static typing. Scala is equipped with a rich and balanced type system. It provides compile time constraints that could help to avoid certain erroneous scenarios. On the other hand, a local type inference mechanism allows developers not to annotate the program with redundant type information.
The following software was also used:
  •  SBT (Simple Build Tool) - Build tool for Scala and Java projects. Maven or Gradle with appropriate Scala plug-ins can be used as well, however, SBT became de facto the number one build tool for Scala. It is easy-to-use but quite powerful utility. [3]
  •  Akka - Asynchronous event-driven middleware framework implemented in Scala, for building high performance and reliable distributed applications. Akka decouples business logic from low-level mechanisms such as threads, locks and non-blocking IO. [4]
  •  Spray - Scala framework for building RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, modular, testable. [5]
  •  Slick - Database query and access library for Scala. It provides a toolkit to work with stored data almost as using Scala collections. Features an extensible query compiler which can generate code for different backends. [6]
  •  MySQL - Well-known open-source RDBMS. [7]
  •  Lift-json - Parsing and formatting utilities library for JSON. [8]
  •  Logback - Fast and stable logging utility. Considered as a successor to the log4j project. Natively implements the SLF4J API. [9]

Spray + Bootstrap

https://bitbucket.org/diversit/spray-twirl-bootstrap
https://github.com/spray/spray-template

"Spray - Twirl - Twitter Bootstrap" Template Project

This project is based on the Spray Template project. I added support for serving static files, added Twitter Bootstrap files and added Twirl for templating. You can use this project as a template for a REST API or even serve a website.
Spray routing is split up in serveral traits for static routing, twirl pages and inline html.
This project uses Git Flow.
Follow these steps to get started:
  1. Git-clone this repository.
    $ git clone git@bitbucket.org:diversit/spray-twirl-bootstrap.git my-project
    
  2. Change directory into your clone:
    $ cd my-project
    
  3. Launch SBT:
    $ sbt
    
  4. Compile everything and run all tests:
    > test
    
    Note: For the Selenium Chome test to be able to run, you need to install thechromedriver locally. Read my blogpost about how Selenium is setup for ScalaTest. And read the ScalaTest Selenium documentation for using Selenium in ScalaTests.
  5. Start the application:
    > re-start
    
  6. Browse to http://localhost:8080/
  7. Start the application:
    > re-stop
    
  8. Start hacking on src/main/scala/com/example/MyService.scala



Spray Project
This projects provides a starting point for your own spray-routing endeavors. There are 8 branches, providing templates for spray-routing on
  • spray-can, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_spray-can_1.0 branch)
  • spray-can, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_spray-can_1.1 branch)
  • spray-can, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_spray-can_1.2 branch)
  • spray-can, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_spray-can_1.3 branch)
  • Jetty, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_jetty_1.0 branch)
  • Jetty, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_jetty_1.1 branch)
  • Jetty, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_jetty_1.2 branch)
  • Jetty, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_jetty_1.3 branch)
You are currently on the on_spray-can_1.1 branch.
> git clone git://github.com/spray/spray-template.git my-project -b on_spray-can_1.3