From iPhone to Android

Wednesday, 28th September, 2011

No doubt I’ll get used to it but, after a week of living with a HTC Sensation running Google’s Android operating system, I’m starting to think the unthinkable: surely even Windows Phone can’t be as bad as this!

There’s been various gossip around the web this past week about Baidu’s forthcoming mobile operating system, (yì, easy).

  • Reuters wrote that Baidu Yi is “modelled” on Android.
  • 山寨机 (Shānzhài Jī, cottage machine or kind of home made) wrote about its compatibility with Android.
  • The English-language rumour mill says Yi is built on / based on / a fork of Android.

Without a decent grasp of Chinese, it’s quite difficult to get beyond the chatter. 搜狐IT (Sōuhú IT) has a couple of articles from the 6th September:

  • 百度易手机11月上市 戴尔负责硬件制造 (Baidu Yi mobile phone, devices from Dell, coming in November): quotes Baidu CEO on the importance of compatibility with Android (百度CEO李彦宏在接受搜狐IT采访时 … 百度•易“刚开始做,需要兼容现有流行的操作系统——Android系统”。).
  • 外媒解读百度易:剥离安卓应用 封装自己的服务 (Foreign media interpretation of Baidu Yi: Android with Baidu applications): has a passage mentioning Yi as a fork of Android, but the paragraph does start with “allegedly” (据称,实际上在中国销售的部分安卓手机上,百度的网络服务开始取代谷歌的服务,不过百度此次将更加深入,将在安卓操作系统基础之上推出一个独立的分支。).

Baby steps with a nif

Sunday, 14th August, 2011

simple_nif is an erlang NIF which takes a list of integers and returns a record, called params, containing the sum, the mean, and the quartiles of the input list:

1> simple:get_params([1,2,3,4,5,6,7]).
{params,28,4.0,{2.0,4.0,6.0}}

The main point of the exercise was the C interface between erlang and the C functions. The file simple_nif.c shows how to parse the input list from erlang into a C array, and how to assemble the results into an erlang tuple to return.

More details in the README.

I am releasing the code under the ISC license.

References

Open an Eclipse project in Eclipse

Wednesday, 6th July, 2011

What could be simpler? Not.

I am developing an Android project using Eclipse. I’m working on several machines, with the project source kept in version control. Eclipse seems to generate a ton of metadata files. So far I haven’t been keeping these under version control, but it looks like I might have to.

Often when I launch Eclipse it doesn’t find the project. To gently remind Eclipse where your project is:

  1. Choose File | Import
  2. Select General | Existing Projects into Workspace
  3. Click next and then browse to the directory contain the project directory.

(source)

Eclipse mitigates the pain of having to write Java, but Eclipse brings pains of its own. Today I’m looking into developing for Android using my usual IDE, emacs.

Tsung is an excellent tool for stress-testing websites. With tsung-recorder you can record different visits (called sessions) to the target website, and later run many randomised versions of the visits.

One complication with testing a Django website, is that forms are generally protected against cross-site request forgery attacks by a hidden field in the form (see Cross Site Request Forgery protection).

Thanks to help from a respondent on django-users, we can overcome this complication: using dyn_variable, tsung can find values in a requested webpage and store them for use in later requests. The simplified session config below shows this in action.

<session name='login_with_csrf' probability='100'  type='ts_http'> 

<request> 
  <dyn_variable name="csrfmiddlewaretoken" ></dyn_variable> 
  <http url='http://mysite.com/' method='GET'></http> 
</request> 

<request subst="true"> 
  <http url='/home/' contents='csrfmiddlewaretoken=%%_csrfmiddlewaretoken%%&amp;csrfmiddlewaretoken=%%_csrfmiddlewaretoken%%&amp;username=xxxxxx&amp;password=xxxxxx&amp;next=%2F' content_type='application/x-www-form-urlencoded' method='POST'></http> 
</request> 

</session>

[updated 061111: added another award]
[updated 290911: added another award]

Congratulations to BeatBullying! Six awards — so far.

MAAW Globe Awards

MAAW = Marketing Agencies Association Worldwide

Third Sector Excellence Awards

Institute of Promotional Marketing

  • Digital Promotions (Gold)
  • Not for Profit (Silver)

List of 2011 Winners

UTalk Marketing

For more on the Big March:

I was the main server-side developer for the campaign, weaving together technologies including Google’s App Engine, Django (non-rel), gaem, nginx, and Crisp‘s community management platform.

I bought iScheme a while ago, purely for the novelty value, and thought little more of it. The other day I was browsing through The Little Schemer with my son, and we came across an exercise to write a function that adds two numbers, using only operations for add 1, subtract 1, and a test for zero (p. 60).

After taking some time to express our mind-bogglement, we decided that the problem would become more accessible if we thought of the two numbers to be added as two piles of coins, and the operations as “take a coin”, “put a coin”, and “is this pile empty?”

Later that night I couldn’t resist trying it out on my phone:

iScheme iPhone screenshot

iScheme: this little schemer is a perfect companion to The Little Schemer!

I scheme, you scheme, we all scheme on iScheme!

etc.

Immodest as it sounds, I have to say I prefer my implementation to the one in the book (reformatted for exposition):

; my version
; (define pls (lambda (x y)
; (cond ((z0 x) y)
; (else (pls (s1 x)(a1 y))
; ))))

; book version
(define pls (lamdba (x y)
(cond ((z0 y) x)
(else (a1 (pls x (s1 y)))
))))

Why is the book’s version better?

Browsing error logs on a remote node

Wednesday, 18th May, 2011

First, set up your sasl

This config sets up a directory for rolling logs (max ten files, each max 10 megabytes). More configs on the man page.

% erl_config.config
[
 {sasl, [
         {sasl_error_logger, false},
         {error_logger_mf_dir, "/path/to/a/dir/for/erl/logs/"},
         {error_logger_mf_maxbytes, 10485760},  % 10 MB
         {error_logger_mf_maxfiles, 10}
         ]
  }
].

And when you run erlang, tell it where your config file is:

erl -config /path/to/erl_config -boot start_sasl  ... more args ...

n.b.:

  • erlang expects the config file to have the extension .config, which you don’t give in the command line.
  • the above config is cribbed from Programming Erlang. I was surprised to find that Erlang and OTP in Action does not describe how to configure the sasl error logger.

Connect and browse

Once you’re connected to the above node (see Connecting erlang nodes), you can run rb, the report browser. A little bit of set up is required. See this discussion on stackoverflow, which encapsulates the setup in a function:

%% @doc Start the report browser and reset its group-leader. 
%% For use in a remote shell
start_remote_rb() ->
    {ok, Pid} = rb:start(),
    true = erlang:group_leader(erlang:group_leader(), Pid),
    ok.

Connecting erlang nodes

Tuesday, 17th May, 2011

In order to connect to each other, erlang nodes each need a name, they need to share a secret cookie, and if they’re to communicate over the internet, they need access to ports.

The secret cookie can be either set at runtime (as in the examples below), or in each user’s .erlang.cookie file.

For local connection

On the same machine or subnet, each node just needs a short name:

$ erl -sname chico -setcookie marx
...
(chico@localhost)1>

For internet connection

Each node must have the following ports available:

  • port 4369, used by epmd (the Erlang Port Mapper Daemon, not Erick and Parrish Making Dollars), must be open for both TCP and UDP (n.b.: this is a default).
  • another port or range of ports for the erlang nodes themselves. These nodes can be set at run time using the -kernel, inet_dist_listen_min and inet_dist_listen_max flags.

Each node must also use a full name, with either a domain or an IP address:

$ erl  -name chico@brothers.org  -setcookie longrandomstring  -kernel inet_dist_listen_min 9000  inet_dist_listen_max 9005
(chico@brothers.org)1>

Connecting

Erlang nodes are gregarious: as soon as nodes find out about each other, they connect. An easy way to say hello is “ping”:

(chico@localhost)1> nodes().
[]
(chico@localhost)2> net_adm:ping(groucho@localhost).
pong
(chico@localhost)3> nodes().                        
[groucho@localhost]
(chico@localhost)4> ^g
User switch command
 --> r groucho@localhost
 --> c
Eshell V5.7.2  (abort with ^G)
(groucho@localhost)1> 

You can skip the connection palaver by using the -remsh flag at startup:

$ erl -sname chico -setcookie marx -remsh groucho@localhost
...
(groucho@localhost)1>

etc

Introspection GUIs like AppMon and Pman can access any connected node (see the Nodes menu in the toolbar).

On Unix-like systems low-level byte-to-byte copying is done with dd:

dd if=[input file] of=[output location]

n.b.: of is a location, not a filename, so you can’t use the file path to your USB stick, as in:

dd if=dfly-i386-gui-2.10.1_REL.img of=/Volumes/MyUSBStick

The path to the device location is required, as in:

dd if=dfly-i386-gui-2.10.1_REL.img of=/dev/disk1

On MacOSX, how to find out the dev name of your USB port is described in this Ubuntu help page. The following will list the currently mounted devices, with their dev names:

$ diskutil list

This will unmount the device, ready for dd:

$ diskutil unmountDisk /dev/[name]

And this will do the deed:

$ dd if=/path/to/input/image of=/dev/[name] bs=2048

Finally, this will make your device safe for removal.

$ diskutil eject /dev/

Follow

Get every new post delivered to your Inbox.