Monday, November 28, 2011

Check it out, my family is on TV! But I'm not

http://abclocal.go.com/wls/story?section=news/local/mathie&id=8443324

Saturday, November 26, 2011

Dawn of Black Friday 2011

I don't have desire to join Black Friday shopping in early morning anymore, not in last 7 years, not a little bit. Things I bought from 2004 Black Friday are still in sealed boxes. This Thanksgiving, my biggest hope is to stay at home, completely forget about work, and take a long 4 days break with satisfying sleeps. Really want to do it this time.

Reality emerges in unfortunate way. At 3AM, my IPhone jumped up indicating a text message was received. There can be so many reasons for a text message on 3AM, sales promotion,wrong number, payment due, or I'm paged. I kept fingers crossed in dream and hoped it was not the last one. A few seconds later pager beeped. Instead of any of the other reasons, it really was because someone paged me. Finger crossing continue to prove in vain.

Walking to computer in pajama,  I opened two red eyes and read, "blah blah blah... to draw someone's attention, I escalated this ticket". No English words I learned could describe my mood at this point. The author of ticket decided to wake me up on 3AM Black Friday morning. not because system is in trouble, not because release is blocked, but to "draw attention". In a different fine day I would reply with a bunch of links to condition of ticket escalation and cc every manager. However now everything was pointless, I was already awake.

Ticket was nothing but a question of a well known issue, which should never, never be escalated, except that my foreign colleagues believe paging people is an effective way of communication as they live in a different timezone.

Done with work, I ran into sleep trouble again. The moment eyes are closed, all worries come to my mind. What if people recognize technical debt more seriously and decided to deal with problem earlier, what if I work for a different project, or a different company without on call, what if I'm a movie director instead of programmer, what if, what if... Brain spins so fast like Turbo key is pressed.

4:30 AM, just about the time I went over most of "what if" questions, wife called. "Honey I got this sweet deal we always wanted and you should go to Sam's Club in Seattle and get one as well". Indeed it is the deal I always want. My wife spent Thank Giving with her family this year and I'm left alone in Seattle. She brought the entire platoon to shop, which is total reasonable. With family, Black Friday shopping can be a lot of fun. The only gotcha is, she is in Indiana under Eastern timezone. "That's sweet." I murmured, "But it's really early and I prefer to sleep, so I'll skip".

5:00 AM, wife called again. "Great news, I can get one for you here in Indiana. Just tell me the SimCard number. You can take out SimCard with a needle...". So a project started. A project involving sweep of entire 3 bedroom condo to look for a needle, some handcraft, picture taking and emailing started at 5AM. Once I'm done with that, sky started showing light.

It's about 5:20 AM. I've never seen Seattle at 5AM, I don't even know the existence of 5AM in Seattle. It is stunning. After a series of crazy events which completely destroyed my hope for sleep, why not another crazy event, like going out and take photos.



Yet again, after 7 years I got up early in Black Friday morning. Plan is kaput, eyes are sore. Family is thousands mines away, and refrigerator is empty. However bad things pass and good things stay. Looking at these pictures, I am happy in the morning of Black Friday.

Saturday, November 19, 2011

Cyclic numbers - Project Euler problem 358



Cyclic number, 1/7 specifically, is something amazed me while I was a child. However I never really thought about cyclic numbers other than 1/7 until this problem. I decided to spend an evening researching and resolving the latest Project Euler problem, #358 Cyclic Number.

To solve this problem, we need an integer number X satisfying following criteria based on my understanding of cyclic number:
1. 1/X starts with 0.00000000137
2. X is dividable by 10^X-1
3. The result of (10^X-1/X) ends with 56789
4. X is a prime number
5. ?

I put a question mark for the fifth criterion because I haven't figured out what it is, while only with four others, the problem can almost be solved.

The first hint limits candidates between 724637681 and 729927007. Given the second and third hint, the last 5 digits of 56789 * X should be 99999, which means 56789 * (X-1) ends with 43210. X must ends with 09891 to satisfy such requirement. There are 53 numbers within range and ends with 09891, in which only 3 are prime numbers, 725509891, 726509891 and 729809891. The final answer hides in these three numbers.

All these three numbers meets requirement 2. I tried answering them to projecteuler and found the last one is correct answer. However I haven't figure out how to filter out two others programmatically. This still bothers me.

The Java code is in GitHub. It's quite efficient as far as I can tell. Most of the time is spent on prime number checking.

>>>>>> Runnining solution of problem 358
Last five digits result is 9891
Searching numbers between 724637681 and 729927007
Checking candidate: 725509891 with sum of digits 3264794505
Checking candidate: 726509891 with sum of digits 3269294505
Checking candidate: 729809891 with sum of digits 3284144505
53 numbers are verified
<<<<<< Solution 358 took 878.149399 ms

Sunday, November 06, 2011

Night of the living programmer - Facebook login in Java web application

It is a sunny Saturday, not often in autumn of Seattle. For quite a while, I've been waited for such a chance to walk peacefully in Olympic Sculpture Park. On the other side, I also always wanted to add a "Login with Facebook" button to my new website to attract users as the popularity of the website just about to reach all time low. So the plan is, finishing the login button before afternoon, have peaceful walk during sunset, which would be nice, and watch a relaxing DVD before sleep.

With this guide, adding a button with popup login window wasn't difficult. Out of box, div element with class fb-login-button is decorated as long as Facebook Javascript SDK is included.  The script snippet in tutorial is mostly to enable Javascript SDK in an asynchronous fashion for performance gain. The only gotcha is that the URL of page with button must be under a registered domain of application. I did local testing by modifying my /etc/hosts and mapping local.mydomain.com to 127.0.0.1.

However from this tutorial it isn't clear how the data that application asks user the access for, is accessed or passed over to application? In my first attempt, the pop up window asked for login information, permission to access a few fields, then closed as if it has nothing to do with the Java code running on the server side of application. My guess is that the access token used to make "/me" call should be passed as a parameter to a given URL under mydomain.com, but the tutorial doesn't say anything about it, it doesn't even say how the redirect URL is configured.

Reading further in authentication concept, I realized I could do a three-step authentication all by myself without Javascript SDK or fb-login-button class. It'd give me decent chance to smoothly embed my Java page into the process and get access token, which of course, requires a little bit of work. But then I wonder what's the purpose of the nice fb-login-button class button.

After playing for a while, it turns out in the simple fb-login-button approach, after authentication is done and window is closed, the useful access token that I expected is left in a cookie named fbs_<app-id>. I can totally redirect to an empty page that picks up this cookie after login window is closed. Follow this direction, my application ends up with:

<div  class="fb-login-button" data-perms="email"
        onlogin="location.href='/do_land_facebook.html"
        title="You may sign in right away if you have a facebook account">
    Login with Facebook</div> 

The do_land_facebook.html is back'ed with following Java code that picks up cookie and call "/me" api.

String cookieName = "fbs_" + config.getFacebookAppId();
Cookie fbCookie = null;
for ( Cookie cookie : request.getCookies() )
{
    if ( cookie.getName().equals( cookieName ) )
    {
        fbCookie = cookie;
        break;
    }
}
if ( fbCookie == null )
{    // This could happen when user clicks cancel button
    // The onlogin event is triggered no matter user clicks Login or Cancel
    return "redirect:/login_failed.html"; // It happens to be a SpringMVC based application
}
    // The cookie value is the exact query string required to call facebook API
InputStream in = new URL( "https://graph.facebook.com/me?" + fbCookie.getValue() ).openStream();
try
{
    String content = IOUtils.toString( in );
    LOG.info( "Received facebook response: " + content );
    JSONObject me = new JSONObject( content );
    loginWithFacebook(request, me); // Do my own login code logic with given JSON object graph
    return "redirect:/index.html";
}
finally 
{
    IOUtils.closeQuitely( in );
}

Finally everything starts working. I'm still debating with myself whether to do the three step authentication without Javascript SDK, or leave it as is. Maybe I will make this decision tomorrow with a dice.

Another thing I just realized, sunset is gone now and it's pitch black outside. Time went so fast and it's already the time to watch relaxing horror movie. What a Saturday.