Wednesday, September 4, 2013

Mixing WebSocket and HttpRequest

http://stackoverflow.com/questions/15101858/how-do-i-register-multiple-handlers-for-a-httpserver-in-dart

// pseudo-code

var server = HttpServer;
server.register('/foo', someHandlerFunction);        // 1
server.register('/bar', someOtherHandlerFunction);   // 2
server.register('/ws', webSocketHandler);            // 3

===>
Recommended Approach

----------------
 
import 'dart:io';
import 'dart:async';

handleWebSocket(WebSocket webSocket) {
  webSocket.listen((event) {
    if (event is MessageEvent) {
      /* Handle message. */
    } else if (event is CloseEvent) {
      /* Handle closed. */
    }
  });
}

typedef bool ShouldTake(e);
typedef void RouteTo(Stream stream);
typedef void HandleEvent(e);

class TakeAndRoute<S, T> extends StreamEventTransformer<S, T> {
  ShouldTake shouldTake;
  RouteTo routeTo;
  StreamController controller = new StreamController();
  HandleEvent handler;

  TakeAndRoute(this.shouldTake, {this.routeTo, this.handler}) {
    if (routeTo != null) routeTo(controller.stream);
  }

  handleData(event, StreamSink sink) {
    print("handling");
    if (shouldTake(event)) {
      if (routeTo != null) {
        controller.add(event);
      }
      if (handler != null) {
        handler(event);
      }
    } else {
      sink.add(event);
    }
  }
}

main() {
  HttpServer.bind('127.0.0.1', 8888)
    .then((HttpServer server) {
      server
        .transform(new TakeAndRoute<HttpRequest, HttpRequest>(
          (req) => req.uri.path == '/ws',
          routeTo: (stream) => stream.transform(new WebSocketTransformer()).listen(handleWebSocket)))
        .transform(new TakeAndRoute<HttpRequest, HttpRequest>(
          (req) => req.uri.path == '/foo',
          handler: (req) {
            print('got foo');
            req.response.addString("foo");
            req.response.close();
          }))
        .listen((req) {
          print("got 404 for ${req.uri}");
          req.response.statusCode = 404;
          req.response.close();
        });
    });
}

Tuesday, September 3, 2013

ECMA workshop on Dart

There seems an activity to standardize Dart.
It is a good way to promote Dart VM on other browsers.

http://news.dartlang.org/2013/08/ecma-to-hold-workshop-on-dart.html

Considering the run-time performance of  Dart VM is now comparable to JVM, Dart may replace both language on client side JavaScript and server side Java.
That would be definitely possible future scenario, in particular Java has been used for such web related applications, and Dart is better positioned for such applications.

Sunday, September 1, 2013

Dart vs Java runtime performance benchmark

 Looks like Dart VM became faster than JVM.
This means we may consider Dart for server side language.


http://www.infoq.com/news/2013/05/Dart-Java-DeltaBlue

 http://bonovox.be/blog/dart-vs-java-the-deltablue-benchmark/

http://www.reddit.com/r/programming/comments/1e2jhr/dart_vs_java_the_deltablue_benchmark/c9wloq3


https://github.com/xxgreg/deltablue

Results, allowing enough iterations for the VM's to warm up.
gcc -O3   1x
JVM       1.2x
DartVM    1.3x
V8        1.6x
gcc       2.5x
 
http://code.google.com/p/v8/ 

How to create Dart language spec pdf/epub

Dart site does not provide pdf document of Dart language specification.

 https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html

I don't understand the reason, but it is definitely inconvenient.

So I created pdf/epub on linux(Ubuntu)

[pdf creation]

http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/

$ sudo apt-get install wkhtmltopdf
$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf

html2pdf https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.5c9w1alpe7y datlang-spec-0.51.pdf


[epub]

$ sudo add-apt-repository ppa:n-muench/calibre
$ sudo apt-get update
$ sudo apt-get install calibre


save above url to local file with browser and name it as dartlang-spec-0.51.html.

$ ebook-convert dartlang-spec-0.51.html  dartlang-spec-0-51.epub 
 

Message queue system for Dart

Dart  has rereating idiam of message massing in various places.
This may be simplified with a Message queue system like Qpid.

[abstraction of event driven message sending/receiving pattern]
Future is probably implemented as a generic class in Dart class.   so no language extension would have been required to support Future.

Other event driven communication also requires couple of functions to be associated with the communication.
In this book, these are written in codes which is scattered in the source code.
Also it requires similar code need to be repeated again and again.
It might be possible to provide generic class which may be similar to Future class for these setup of communication.

[replacing message passing by message queue system]
These message passing is quite similar to message queue system like Qpid(Apache). If we use such system as communication mechanism, there will  be more uniformity and  flexibility.
For instance, some of the queue can be processed in C/C++ applications.
If we need have high performance server side engine, some of the job can be delegated to such C/C++ codes interacting with Message queue system(e.g,  Qpid) without changing code.
Also it might be possible to make local/remote process transparent for the client.
for instance if some job is simple, it will do locally, but if it require more resources, it may distributes the jobs in distributed servers, and yet client may not notice these things are happening. 


[writing sender and listener on the same side ]

It might be possible to define both logic on teh same side(client or server).
The current approach need to write corresponding codes on both of client and server. these are going to be confusing if they maintained separately.
For instance, listener code may be shipped to client using dart or javascript, and that code may be loaded dynamically in the client, and these VMs are communicated with message passing approach.


connectors/drivers for Dart

There are a few thirdparty library of connectors/drivers for SQL/NoSQL.

sqljocky for MySQL
https://github.com/jamesots/sqljocky

Postgres
http://pub.dartlang.org/packages/postgresql
 
MongoDB
https://github.com/vadimtsushko/mongo_dart/

CouchDB
using httpclient

Riak database
https://code.google.com/p/riak-dart/