Monday, May 28, 2012

Appcelerator - HTTP Post Image To Server

Appcelerator HTTPClient provide quite a easy way to perform HTTP POST to server.  The following code perform a simple POST to a remote server


var postUrl "http://host:port/posturl";var data = {
    name: "John",
    age: "11"
};
var httpClient = Titanium.Network.createHTTPClient({
    timeout : 60000});
httpClient.open('POST', postUrl);
httpClient.send(data);



The trick is create a data variable with {key:value} pair and open a POST http client. Appcelerator will understand the value pair and send http POST to your URL stated in postUrl.

The data arrive in the server will be in standard multipart POST message as follows

--0xKhTmLbOuNdArY-11C2E750-7BF3-444E-AC7F-AAEAB534613F
Content-Disposition: form-data; name="name"

John
--0xKhTmLbOuNdArY-11C2E750-7BF3-444E-AC7F-AAEAB534613F--





It can also perform HTTP POST raw image bytes to remote server. However, on my testing, there are something wrong with the raw bytes. The raw bytes size arrived at server doubled compares to the original bytes. I am not sure what is the reason, So, if you encounter this issue whereby your server does not understand the posted bytes, here is the alternative solution.


var postUrl =  "http://host:port/posturl";var data {
    name: "John",
    age: "11",
    picture: Ti.Utils.base64encode(image)
};
var httpClient Titanium.Network.createHTTPClient({
    timeout : 60000});
httpClient.open('POST', postUrl);
httpClient.send(data);


The basic idea is to base64 encode the raw bytes and post to the server


picture: Ti.Utils.base64encode(image)


In the above line, image is a TiBlob and its contents is encoded by base64.

An example of the image POST data is


--0xKhTmLbOuNdArY-11C2E750-7BF3-444E-AC7F-AAEAB534613F
Content-Disposition: form-data; name="picture"; filename="1h39ae13.bin"
Content-Type: application/octet-stream

 ... BASE64 Encoded Image Bytes
 --0xKhTmLbOuNdArY-11C2E750-7BF3-444E-AC7F-AAEAB534613F

On the server, you have to Base64 decode the image post string and convert it to byte[].

Thursday, May 24, 2012

Appcelerator - ScrollView setContentOffset Animated: false issue

ScrollView is a very nice UI component in Appcelerator that will handle pinch zoom and drag move their content view. By default the content view are at X = 0, Y = 0 location. What if I want to default the content views to center it?

You could use toScroll or setContentOffset to move the inner content view. Both of these method invoke an animation. How about moving the inner view without animation?

The only choice is setContentOffset with animated: false. For example


scrollview.setContentOffset({x: 200, y: 100}, { animated: false });

Now, if you tried this method and does not work for you. The following may help

1. Make sure you call this method after window.open().
2. Also, only call the method after you added ScrollView to the Window

var win = Titanium.UI.createWindow();
var scrollview = = Ti.UI.createScrollView();
win.add(scrollview)
win.open()
scrollview.setContentOffset({x: 200, y: 100}, { animated: false });


Monday, May 21, 2012

Appcelerator - Rotating iPhone Simulator Does Not Change App Orientation

iPhone simulator allow you to rotate the hardware by going to iPhone Simulator menu Hardware -> Rotate Left or Rotate Right

But, you may experience hardware device rotated but not the app when you are using appcelerator.

At first look, it seems like the simulator fault. However, please check your tiapp.xml and make sure the following are in.

<orientations device="iphone">            <orientation>Ti.UI.PORTRAITorientation>            <orientation>Ti.UI.UPSIDE_PORTRAITorientation>            <orientation>Ti.UI.LANDSCAPE_LEFTorientation>            <orientation>Ti.UI.LANDSCAPE_RIGHTorientation>orientations>



Tuesday, May 15, 2012

SSH - RSA Host Key Validation Error

Technically, this is not an error but a security feature for SSH.

Whenever SSH perform a connection to a remote host, it check ~/.ssh/known_host for IP to RSA key match to prevent malicious attack such as man-in-the-middle.

This warning will occurs when you had previously connected to the host, but however, for the current connection, the host RSA key has changed. In general, we do not expect RSA key of a server to be change frequently. Thus, SSH deems it as a security breach and terminate the connection.

Yes, you could goes to ~/.ssh/known_host to removed the entry for the given IP address. But, it is troublesome especially if your IP is used by multiple machines.

Same IP for multiple machine is possible if you are using Virtual Machine and each Virtual OS is configured with the same IP address.

To bypass this issue in command line, you can do the following

ssh username@host -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null -> This option make SSH to use the null device as known file. /dev/null will discard all data written to it.
StrictHostKeyChecking=no -> This option make SSH to add host to known host file automatically

Adding the 2 options together, it means SSH will automatically write host key to null device.

Warning: Use this only if you know what you are doing. Because, this command allow you to connect to any server without any security check. It could lead to real security breach.

SSH - Prevent SSH Session Timeout

If your SSH session is being timeout (it could be your ssh server or router timeout inactive connection), this could help

SSH obtains configuration data from the following order

1. Command line (eg, ssh host -o "options")
2. User configuration file (eg, ~/.ssh/config)
3. System wide configuration file (eg, /etc/ssh_config for Mac and /etc/ssh/ssh_config for Linux)

We will be focusing on command line

In general, a command to prevent server disconnecting inactive connection is as follow

ssh username@host -o ServerAliveInterval=X

ServerAliveInterval=X where X is in seconds. This option request the client to send a message to server in every X seconds and request a respond from the server.

Example of setting interval request for 60 second


ssh username@host -o ServerAliveInterval=60


References: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/ssh_config.5.html

Tuesday, May 8, 2012

iPod Shuffle - Cannot be detected by PC/Mac

If you iPod Shuffle suddenly not detected by your PC/Mac, you can do the following

Press and hold both Menu and the center circle button until a Apple icon appear.

That will force reset your iPod and should do the work.


RTP - RTP Timestamp


RTP timestamp is generally telling you the number of media samples in that packets per media unit (fps or ms)

For MPEG video is, it is generally calculated by

clock rate / fps 

That is if the clock rate for video is 90000 Hz for 30fps

90000 / 30 = 3000 sample per frame

In general, audio is

(clock rate / 1000) * duration of audio

for example a G.711 audio with 8000Hz clock rate and your packet has audio of 30ms duration

Then it will be (8000 / 1000) * 30 = 240 samples in 30 ms

This work because G.711 is a 8 bytes per sample codec with 8000 samples per seconds.

For AAC, it is a bit different. It will come in either 1024 or 960 samples per rtp packet.

Hadoop - How to setup a Hadoop Cluster

Below is a step-by-step guide which I had used to setup a Hadoop Cluster Scenario 3 VMs involved: 1) NameNode, ResourceManager - Host...