Mutant World

Tuesday, January 13, 2009

Creative Webcam Live! on Ubuntu Intrepid Ibex

I received the Creative Webcam Live! as a gift quite some time ago, but never had the time to use it or install it properly.
My good ol' Thinkpad T43p was maybe bleeding edge in 2005, but not as far as having already an incorporated webcam :)

Under Ubuntu Intrepid, this webcam does not work out of the box, at least for me, and I have a fresh (re)install of Intrepid.
This camera appears using lsusb as:

Bus 003 Device 008: ID 041e:4036 Creative Technology, Ltd Webcam Live!/Live! Pro


The solution is quite easy though: you need to set this variable before launching the program that uses the webcam:

export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so


I tested this with camorama and skype, and in both cases it works just fine.
For skype, I created a wrapper script that first set the variable as above and then invokes /usr/bin/skype and modified the menu entry to point to my wrapper script.

Labels:

Monday, December 29, 2008

Visibility in JavaScript

The topic of visibility of members and functions in JavaScript has been figured out already, and there are good resources, among which one of the most important is Douglas Crockford's Private Members in JavaScript.

Here I just recap my experience for my own (and for anyone interested) future reference.

I am pretty paranoid in making all members and methods with the least possible visibility in my Java code. Before opening up a method as protected or public, one should always remind that most of the times, once it's opened up, you cannot close it because other code uses it.

JavaScript can have private members and functions, and I mostly use two idioms for this: the constructor function and the the object initializer.

The constructor function


I use the constructor function technique when I want to create few objects that behave the same (in Java you would say "few objects that belong to the same class", but JavaScript does not have classes).

var MyNS = {}; // An optional namespace
MyNS.Service = function(offset)
{
var _privateMember = offset;

function _privateFunction(value)
{
return _privateMember + value;
}

this.myFunction = function(value)
{
_privateMember = _privateFunction(value);
return _privateMember;
}
};

The common convention is that function names beginning with an upper case letter are constructor functions, in this case Service.

From the example above, private members are defined within the constructor function using the keyword var, private functions are just nested functions within the constructor function, and publicly accessible functions are defined with the this.<functionName> idiom.
The function myFunction in the example above is a privileged function, but for the sake of visibility it can be classified as public because anyone can invoke it.
From the example above, you see that myFunction can refer to private members and functions.

However, referring to publicly accessible functions from private functions is not possible:

// A constructor function without namespace; does not work
function Service2()
{
var _open;

function _clean()
{
if (_open) close(); // Does not work: ReferenceError
}

this.close = function()
{
...
}
}

It is not possible to call privileged functions from private functions. In my experience this is not a big problem, as it is possible to refactor function close() into a private function _close() and call the private version from both _clean() and close().

With a constructor function it is possible to create several objects that behave the same. In the example above you can create several service objects using the following syntax:

var s1 = new MyNS.Service(1);
var result1 = s1.myFunction(5); // returns 6

var s2 = new MyNS.Service(2);
var result2 = s2.myFunction(0); // returns 2

As you would expect, object s1 and s2 have different internal state and can be used independently.

The object initializer


I use the object initializer technique when I want to create a singleton object.
My preferred way of using the object initializer technique is via a function with immediate invocation, which allows to have private members and functions:

MyNS.EventHandler = function()
{
var _queue = [];

function _privateFunction(event)
{
...
}

return {
handle: function(event)
{
_privateFunction(event);
},
get size()
{
return _queue.length;
}
};
}();

Note the parenthesis at the end of the function definition, which perform the immediate invocation of the function.

Similarly to the constructor function technique, it is possible to define private members and functions, but the object initializer technique also allows (for non-crappy JavaScript interpreters) to use the getter and setter notation.
In the example above I used the getter notation to define a read-only property called size, which can be accessed like this:

var s = MyNS.EventHandler.size;

Using the getter notation is much nicer than having a getter function, and much better than having a public member (which would be not only readable but also writable).

Caveats


Both solutions outlined above have the problem that for each object created in those ways, the function definitions are also duplicated: each object will have its own copy of the functions, and this will result in more memory occupation. That's why I suggest the constructor function technique when you create few objects and the object initializer technique for singletons.

At first, I thought that there must have been a way to have private visibility for member and functions, but only one copy of the function definitions, very much like Java does: each Java object has its own copy of the members, but they all share the methods definitions.

Unfortunately I could not figure out how to do it, not even reading JavaScript library code such as Dojo or jQuery.

However, there is a different technique that it is possible to use to share the functions definitions, which I will call here "public members and prototype".

Public members and prototype


When you have to create lots of objects with members and functions, the best technique is to define the functions in the prototype, so that they will be shared by all objects, and have public members holding the state.
You have to give up on restricted visibility (members must be public) to save memory (only one copy of the functions is shared by all objects), because functions defined in the prototype can only (to my knowledge) refer to public members.

MyNS.Event = function(source)
{
this._source = source;
};
MyNS.Event.prototype = function()
{
var _privateStatic;

function _privateFunction()
{
}

return {
consume: function()
{
this._consumed = true;
},
get source()
{
return this._source;
}
};
}();

In this example, MyNS.Event is a constructor function that allows to create events via:

var source = window;
var event1 = new MyNS.Event(source);

The constructor function defines a public member called _source, which can be later referred from functions.
Also functions in the prototype can define public members (like _consumed in function consume()).

All function definitions are defined in the prototype. Note how the prototype object is returned via immediate function invocation, which again allows to have private functions and members as in previous techniques.

The interesting part comes when you define private members in the prototype, like _privateStatic.
These members can only be modified by functions defined in the prototype, which are shared by all objects. The result is that modifications made by one object to _privateStatic (via shared prototype functions) are reflected by all other objects created with the same constructor function. This behavior is the same behavior of static members in Java.

Instead, public members modified by functions defined in the prototype are attached to the object (not to the prototype), so that each different object has its own copy of the members, and therefore they can be modified independently:
 
function A(value)
{
this._field = value;
}
A.prototype = function()
{
var _staticField;
return {
staticGetter: function()
{
return _staticField;
},
staticSetter: function(value)
{
_staticField = value;
},
instanceGetter: function()
{
return this._field;
},
};
}();

var a1 = new A(1);
var a2 = new A(2);
a1.instanceGetter(); // returns 1
a1._field; // public field, returns 1
a1._field = 3;
a1.instanceGetter(); // returns 3
a2.instanceGetter(); // returns 2
a2.staticSetter('foo');
a1.staticGetter(); // returns 'foo'

I'd be interested in any solution that will allow private members with prototype-shared functions, if this is possible in JavaScript.

Labels:

Thursday, August 14, 2008

Java's uncertain future

Java's future looks to me very uncertain in this period.

I started studying Java during 1999 summer, and working with it since then.

Many things happened since the release of JDK 5.

Many key people left Sun for other companies (and wow, the list of those names is truly impressive; among them Josh Bloch, Neal Gafter, Gilad Bracha, most of the Swing core team - Hans Muller, Scott Violet, Chet Haase, etc.)

Sun itself is not performing very well these days (profit plunged 73% in the last quarter).

Sun has open sourced Java.

Yet, there is no JDK 7 plan.
You heard that right, no official JSR has been opened yet, when we were used to have release of version N of the JDK and official JSR for version N+1 almost immediately opened.
But not this time, and noone seems able to predict neither when JDK 7 will be released, nor what it will contain.

There is OpenJDK.
Can anyone tell me the difference between JDK 7 and OpenJDK ?
And I am not meaning the obvious ones, but why two efforts, and how are they synchronizing ?
I never thought that open sourcing the JDK was such a great idea, from the point of view of developers, though it was probably such a good move for other fields that developers could be forgotten.

There is JDK 6 and JDK 6 update 10. Mmm. More confusion.

The closures will (most likely) not be part of JDK 7, but everyone talks about the "upcoming language changes in JDK 7" yet there is no official JSR, nor for JDK 7 nor for closures. Oops.

Java blogs are beginning to label Java as the next legacy language, but it surely will still live for many many years. I hear the same of COBOL.

It appears as if Sun released the command of the Java ship and let its best commanders flew away.
It would not be bad to see a real move from Sun about Java (because strong statements are just that, words), because hey, the ship seems DIW, dead in water.

It's 2008 summer, and I am studying Ruby.

But the worst indicator is that Hani and its Bileblog are quiet: he cannot desecrate the last moments of a dying era, can he ?

Labels:

Sunday, July 27, 2008

Static Fields in Enums

Enums in Java have a numeric, read-only, property called ordinal that is a strictly consecutive integer (starting from 0).

Sometimes, however, it is necessary to associate to an Enum another numeric property, let's call it code, that it may not be strictly consecutive and normally specifies something different than a sequence number. Furthermore, it may be necessary to be able to lookup the correct Enum from its code.

It seems totally trivial to write an Enum that has a static map from codes to Enum instances:

public enum Port
{
SSH(22), TELNET(23), HTTP(80), POP3(110), HTTPS(443);

// The static map from codes to Enum instances
private static final
Map<Integer, Port> ports = new HashMap<Integer, Port>();
private final int code;

private Port(int code)
{
this.code = code;
ports.put(code, this);
}

public int getCode()
{
return this.code;
}

// Lookup method that returns the Enum instance from the given code
public static Port from(int code)
{
return ports.get(code);
}
}

Note the method static from(int code) that returns an Enum instance from the given code.

Unfortunately, this will not compile. The compiler reports that it is illegal to access the static member ports from the constructor.

This makes sense when one imagines how an Enum is first translated into a class by the compiler. Roughly, there is a static initializer that creates the Ports instances, in this case SSH, TELNET, etc.; this static initializer is the first initializer run when the Ports class is referenced, and it is easy to see that when the static initializer runs, no other initializers have been run yet, and in particular the static Map ports has not been created yet.
See http://java.sun.com/docs/books/jls/third_edition/html/classes.html#301020 for further details.

Fortunately, there is an easy workaround: it's enough to use a different class to store the map from codes to Enum instances, and a private static inner class does the job:

public enum Port
{
SSH(22), TELNET(23), HTTP(80), POP3(110), HTTPS(443);

private final int code;

private Port(int code)
{
this.code = code;
Ports.ports.put(code, this);
}

public int getCode()
{
return this.code;
}

// Lookup method that returns the Enum instance from the given code
public static Port from(int code)
{
return Ports.ports.get(code);
}

private static class Ports
{
// The static map from codes to Enum instances
private static final
Map<Integer, Port> ports = new HashMap<Integer, Port>();
}
}

Labels: ,

Randy Pausch Last Lecture

I just watched Randy Pausch last lecture, and found it absolutely incredible, one of those gems that cannot be missed.

If you want to spend 1hr 16 mins in something really worth, then go watch it.

My favorite quote:
"The brickwalls are there for a reason: to give us a chance to show how badly we want something. Brickwalls are there to stop the people who don't want it badly enough."

Thanks Randy: for the head fake that I've got from your last lecture.

Thursday, September 20, 2007

JSP page encoding

I recently had a problem with a web application using JSP pages that was not handling non-ASCII characters correctly: submitted text containing characters such as ù or è resulted in garbage characters stored in the database and consequently garbage displayed by the browser.

The application was developed in Linux, using an IDE configured to save files using UTF-8 encoding, and deployed on a Linux box.
The database was configured to use UTF-8 encoding.
Every page contained the directive <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> in the <head> section.

I thought that given these premises, there was no chance of misbehaviors with respect to character encoding, but I was wrong.
The browser kept telling me that the page it was displaying had ISO-8859-1 encoding.

It turned out that the JSP specification says that if the page encoding of the JSP pages is not explicitely declared, then ISO-8859-1 should be used (!).
The Jetty servlet container was correctly setting the HTTP header as: Content-Type: text/html; charset=ISO-8859-1, following the specification.

The fix is simple, just add this to web.xml:

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>

It will be interesting to know why the JSP expert group did not pick up UTF-8 as the default character encoding.

Thursday, July 26, 2007

Upgrading to SSL an existing socket connection

SSL support is in the JDK since 1.4 (and even before as a separate jar), so it is fairly common knowledge how to create and use SSLSocket and SSLServerSocket.

What is less known, or it was to me at least, is that it is possible to create a plain socket, connect to a server, exchange some message, then upgrade the existing socket connection to SSL, exchange some private message with confidentiality (for example a password that would have traveled in clear text), then downgrade back to the plain the socket connection; all this without closing the original socket nor creating a new one on a different port.

Let's see how.

The server


The server class creates a plain ServerSocket, and calls accept() on it; when a client connects, accept() returns a socket that is normally handed to a different thread (details omitted):

Socket socket = serverSocket.accept();
threadPool.execute(new Handler(socket));

class Handler implements Runnable
{
private final Socket socket;
Handler(Socket socket) { this.socket = socket; }
public void run()
{
Socket currentSocket = this.socket;
while (isConnectionOpen(currentSocket))
{
String message = readMessage(currentSocket);
if (upgradeToSSL(message))
{
SSLSocketFactory sslSocketFactory =
(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslSocket =
(SSLSocket)sslSocketFactory.
createSocket(currentSocket,
currentSocket.getInetAddress().getHostAddress(),
currentSocket.getPort(),
false);
sslSocket.setUseClientMode(false);
sslSocket.startHandshake();
currentSocket = sslSocket;
}
else if (downgradeFromSSL(message))
{
currentSocket.close();
currentSocket = this.socket;
}
else
{
handleNormalMessage(currenSocket, message);
}
}
}
}

When upgrading, the server wraps the current (plain) socket using SSLSocketFactory.createSocket(socket, address, port, autoClose), then configures the SSLSocket to be a non-client one (for the purposes of the SSL handshake), then starts the SSL handshake to secure the connection.

When downgrading, the current (ssl) socket is just closed and, thanks to the autoClose argument set to false (which will not close the underlying plain socket), the original plain socket is still operative.

In order for this mechanism to work, you have to configure the SSL details as usual for a server. This means having created a private/public key pair in a keystore (and optionally having signed the public key with a CA root) and having set the relevant SSL system properties (or having done the equivalent using the SSL APIs).

The client


The client connects to the server using a plain Socket, then upgrades the socket connection (details omitted):

// Connects to server
this.socket = new Socket(serverAddress, serverPort);
...
Socket currentSocket = this.socket;
while (moreMessagesToSend)
{
if (upgradeToSSL)
{
SSLSocketFactory sslSocketFactory =
(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslSocket =
(SSLSocket)sslSocketFactory.
createSocket(currentSocket,
currentSocket.getInetAddress().getHostAddress(),
currentSocket.getPort(),
false);
sslSocket.setUseClientMode(true);
sslSocket.startHandshake();
currentSocket = sslSocket;
}
else if (downgradeFromSSL)
{
currentSocket.close();
currentSocket = this.socket;
}

sendMessage(currentSocket);
readReply(currentSocket);
}

Upgrading and downgrading the socket connection in the client works like in the server code, the only difference being that the SSLSocket is configured to be a client one (for the purposes of the SSL handshake). The autoClose argument prevents the original plain socket to be closed when the SSL socket is closed.

In order for this mechanism to work, you have once again to configure the SSL details as usual for a client. This means having setup properly a trust store (and optionally having imported into it the server certificate if it's not signed by a CA root) and having set the relevant SSL system properties (or having done the equivalent using the SSL APIs).

If you're writing custom network protocols that require confidentiality, the above may come handy. Enjoy !

Labels: , ,

Wednesday, January 17, 2007

Jonas Bonér @ JUG Torino - January 19th

Jonas Bonér of Terracotta Tech will be our guest speaker at the monthly meeting of the JUG Torino on January 19th, 18:30.

The title of his talk is: How to write scalable, highly available web applications.

If you are in the Torino area, this meeting is one which will be really worth attending, both for the speaker importance and for the arguments.

Here you can find more information on this meeting.

Labels: