wow!

wow! I cannot believe there are actually comments from readers, and one of my blogs is even referenced by a stackoverflow reply post.  I am glad that some of my posts helped !

I feel it has been so long after my last post here. I hope to resume writings here !

Posted in Uncategorized | Leave a comment

Pub/Sub in Redis using PHP

I would like to put an example together about the pub/sub using php in Redis; there is only API documentation available in phpredis, the PHP client I am using (http://redis.io/clients).

0. Setup
First setup a Redis Server. I set up a Redis server in my local box using port 6378 (myredisserver.test.com:6378).

1. Publish: push a message to a channel.
This part is relatively easy. The following is a php script “publish.php“.

<?php   

//publish.php    
$redis = new Redis();    
$redis->pconnect('myredisserver.test.com',6378);
  $redis->publish('chan-1', 'hello, world!'); // send message to channel 1.
  $redis->publish('chan-2', 'hello, world2!'); // send message to channel 2.

  print "\n";
  $redis->close();

?>

1.5 Checkpoint: Monitor in Redis server.
Let’s take a break now and see what will happen if we run the script “publish.php” from a client side.
Because I use a non default port, “-p” option is used with “redis-cli” command.
Open a new terminal at Redis Server, and issue “MONITOR” command in redis “console”:

%redis-cli -p 6378
redis 127.0.0.1:6378> MONITOR
OK
1321312790.866271 "MONITOR"
1321312792.221599 "PING"
1321312796.330376 "PUBLISH" "chan-1" "hello, world!"      # after run "publish.php"
1321312796.330482 "PUBLISH" "chan-2" "hello, world2!"     # after run "publish.php"

2. Subscribe: Listen to a channel (or some channels).
Here is the complete php script “subscribe.php” after I did some debugging. Thanks to the info here: https://github.com/nicolasff/phpredis/issues/36.

<?php

//subscribe.php

function f($redis, $chan, $msg) {
    switch($chan) {
        case 'chan-1':
            print "get $msg from $chan\n";
            break;
        case 'chan-2':
            print "get $msg FROM $chan\n";
            break;
        case 'chan-3':
            break;
    }
}

ini_set('default_socket_timeout', -1);

$redis = new Redis();
$redis->pconnect('myredisserver.test.com',6378);

$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f');
print "\n";

?>

2.5
1) run script “subscribe.php“:

%php subscribe.php

of course, nothing happens.

2) In another terminal window, run script “publish.php” twice, and come back to have a look!

%php subscribe.php
get hello, world! from chan-1         #newly displayed
get hello, world2! FROM chan-2        #newly displayed
get hello, world! from chan-1         #newly displayed
get hello, world2! FROM chan-2        #newly displayed

3. Breakpoint: Revisit the MINTOR window in Redis Server:

%redis-cli -p 6378
redis 127.0.0.1:6378> MONITOR
OK
1321313546.882528 "MONITOR"
1321313552.848569 "PING"
1321313553.458541 "SUBSCRIBE" "chan-1" "chan-2" "chan-3"
1321313556.223800 "PUBLISH" "chan-1" "hello, world!"
1321313556.223862 "PUBLISH" "chan-2" "hello, world2!"
1321313557.597914 "PUBLISH" "chan-1" "hello, world!"
1321313557.598061 "PUBLISH" "chan-2" "hello, world2!"
1321313562.851878 "PING"

4. Test with multiple publishers and multiple subscribers!

Reference: http://robots.thoughtbot.com/post/6325247416/redis-pub-sub-how-does-it-work

Posted in Uncategorized | Tagged | 4 Comments

one cannot be a slave for multi-masters

one can only be one master’s slave. 😀

Today I set up three redis server in this way: Redis-Server-A:6379, Redis-Server-B:6379, Redis-Server-C:6378.

Each is up and running fine by my testing of set and get.

Then I set up a Master-Slave using: Redis-Server-A:6379 (Master), Redis-Server-B:6379 (Slave).

It is working like a magic :). I mean instantly data is replicated from Master to Slave.

Then I configured the redis.conf file in Reids-Server-B to be slave of Redis-Server-C:6378 like this:
slaveof Redis-Server-A 6379
slaveof Redis-Server-C 6378

Restart Redis-Server-B, and all the content in Redis-Server-B are the same as Redis-Server-C, all the previous records replicated form Redis-Server-A have been wiped out.

So, one cannot be a slave for multi masters!

PART two: One Master can have multiple slaves!
I configured Redis-Server-A as Master, Redis-Server-B slaveof Redis-Server-A, Redis-Server-C slaveof Redis-Server-A. A’s records are replicated to B and C both.

Posted in Uncategorized | Tagged | Leave a comment

memcache

1. how to do replication
http://repcached.lab.klab.org/

1.5 how to do multiple to multiple replication?
repcache is only one-to-one?

2. how to do dump and reload
http://shairosenfeld.com/blog/index.php/2010/10/memcached-dump-and-reload/

Posted in Uncategorized | Leave a comment

AsnycTask in Android

data flow :

Posted in Uncategorized | Leave a comment

Android books

Since I started learning Android programming, I have read several books and learned everything from zero background.
Here is the list:
1) Programming Android

2) Beginning Android™ Application Development

By: Wei-Meng Lee
Publisher: Wrox
Pub. Date: April 19, 2011
Print ISBN: 978-1-118-01711-1
Web ISBN: 1-118017-11-0

Posted in Android Beginner | Tagged | Leave a comment

The mysteries about “upload photo” using facebook-android-sdk

After my first try, I now want to expand the functionality of my “MyCreateApp” to upload photo. It took me quite some time to figure it out. The downloaded example code package provided by facebook is not working without some tweaking. I found it is quite interesting to fix it here and there and make it run eventually.

http://forum.developers.facebook.net/viewtopic.php?id=104678

and good tip: https://www.facebook.com/topic.php?uid=74769995908&topic=16667

Posted in Android Beginner | Tagged , | Leave a comment

build my first Facebook android app using facebook-android-sdk! – III

So long ! well, don’t give up. It is only halfway.

Let’s check where we are now from the official facebook blog: https://developers.facebook.com/docs/guides/mobile/#android

Register your app in Facebook
Visit this URL to register your app: https://developers.facebook.com/apps

If you have not verified your account, you need to verify your account either by SMS or credit card number first.

I named my first registered app as “MyCreateApp”, and enter the signature generated in last step. Up to this point, all the non-coding preparation is done.

According to the facebook blog, next step is “Installing the Facebook Android App” in emulator, but this is optional. I skipped this one for now.

SSO (Single-Sign-On)

Let’s modify the MyCreateAppActivity.java file in our Android project “MyCreateApp“. As I did not install the Facebook.apk in my emulator, it is slightly different from the sample code in tutorial:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;

public class MyCreateAppActivity extends Activity {
	 Facebook facebook = new Facebook("YOUR OWN APP ID");

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);

	        facebook.dialog(this,"feed",
	        	      new DialogListener() {
	        	           public void onComplete(Bundle values) {}

	        	           public void onFacebookError(FacebookError error) {}

	        	           public void onError(DialogError e) {}

	        	           public void onCancel() {}
	        	      }
	        	);
	    }

	    @Override
	    public void onActivityResult(int requestCode, int resultCode, Intent data) {
	        super.onActivityResult(requestCode, resultCode, data);

	        facebook.authorizeCallback(requestCode, resultCode, data);
	    }
}

The following is the screenshot when running the project in an Android2.2 Emulator. It will ask for user name and password first:

After input user name and password, a new window will pop up and if you enter some status and click “Share” button, the newest update will be posted to the user’s wall.

Verify it in facebook to see whether the post is on the wall ! — END

Posted in Android Beginner | Tagged , | Leave a comment

build my first Facebook android app using facebook-android-sdk! – II

In my last blog, I finished installation of all the prerequisite tools and source code. Now move on to next step by following instructions on https://developers.facebook.com/docs/guides/mobile/#android

According to the tutorial, I need to first create a “com_facebook_android” project from the code I just “git cloned”, and then reference it as a library in your real app later. Hope Facebook can improve this to release a real “.jar” style library, rather than using this confusing step to first create a almost-do-nothing Android project in Eclipse. Or maybe there are advantages this way, but I was confused at first.

So…….after the lengthy creation of two projects, I had an Android project “MyCreateApp” in my Eclipse and also added the permission to use internet in its “AndroidManifest.xml” file :

Keytool, openssl and app signature
Now comes to a headache part. It took me quite a few hours to figure out how to make it work, sort of…This is definitely another improvement to make in Facebook documentation.

The following is a screenshot of tutorial:

1) Keytool
I found some good discussion in stackoverflow forum about keytool: http://stackoverflow.com/questions/2997348/cant-find-the-android-keytool It is worth a reading if you have time. I first tried to list the keystore using keytool :

  1. find where is “keytool”. It is actually part of java jdk:
  2. list the keystore: keytool -list -keystore "C:\Documents and Settings\your user name here\.android\debug.keystore"
  3. Enter keystore password: by default, it is set as empty. So simply press “Enter” in your keyboard.

2) Openssl on windows

openssl is not provided by windows, and there are several ways to build and install openssl on windows. After a bit of research, I found that the simplest way is to install Cygwin :).

This is a good tutorial : http://robotification.com/2007/08/31/installing-openssl-on-windows/

After downloading the setup package from cygwin website, click the icon to install cygwin. The following are the screenshots I took:






remember to install the two packages: openssl and openssl-devel:

3) App signature
Finally! Let’s generate the facebook app signature !
See the following screenshot:

Posted in Android Beginner | Tagged , | 1 Comment

build my first Facebook android app using facebook-android-sdk!

Hooooray! I finally got my first facebook android app working !

Here is a step-by-step guide about how-to. I started it from zero, and ended up in posting an update status to my wall successfully! so you can do it too!

My programming environment: hmmmm, although I am a vi+linux person, I did all my android programming in Windows machine using Eclipse.

This is the official blog about Facebook SDK blog: Facebook SDK blog; it serves as a good starting point, but it is not sufficient to complete the first “Hello World” app up and running. I really hope facebook can improve and update their documentation.

Anyways, let’s get started from the real starting point: https://developers.facebook.com/docs/guides/mobile/#android

From facebook, prerequisite is to install the following:

  • Install Android SDK & the Eclipse Plugin
  • Install Git
  • Clone the GitHub repository: git clone git://github.com/facebook/facebook-android-sdk.git

The first item can be easily checked!

Install Git:
You don’t have to be expert on Git here. All you need is 1) install Git plugin to Eclipse 2) can clone the Github repository. 🙂 Here is an excellent tutorial for beginner: http://www.vogella.de/articles/EGit/article.html
1) install Git plugin (EGit) to Eclipse:

http://www.vogella.de/articles/EGit/article.html#eclipseinstallation

The article was written in 2009, so you might see some differences (the following was what I had):

2) clone the project:

tutorial: http://www.vogella.de/articles/EGit/article.html#respository_checkoutproject

use the URI provided in facebook blog when you follow the above tutorial to clone the project: git://github.com/facebook/facebook-android-sdk.git

I used default settings all the time, so the final checked-out code are in the folder: “C:\Documents and Settings\xmeng\git\facebook-android-sdk”. You can double check when you have cloned successfully.

Posted in Android Beginner | Tagged , | Leave a comment