Category: Programming

Michael
01/11/10

Flicka.mobi Goes Live

Flicka - Flickr + Android

We've (Koa + me = MokaSocial, LLC) finally pushed out Flicka.mobi the official portal to Flicka, our Flickr application for the Android platform! It will bring all the free functionality of the Flickr website and add tons of integration into the mobile platform. We're hoping to be able to launch the actual application by the beginning of Q2 this year. We'll see.

The page kind of tells you about the release and stuff but here are all the links:

It's been one frantic push to get the code base stabilized and things to a place where we can finally start talking about it. There isn't much about the Google Android SDK on the web so that's probably the biggest challenge. Also, the Java library we're using for our interactions doesn't seem to have everything.

Anyway, it's been a wonderful dive into Java and I've been thoroughly enjoying it. We're hoping it's a successful mobile platform application; one in a series of many increasingly complex and sexy!

Special thanks to those that have been helping. The organization, insight, and comradeship has been awesome!

Michael
12/08/09

Android: SDK & IDE Fun

Recently I tried to get my Android Virtual Device (AVD) to run on my recently updated Ubuntu install and ran into the usual disheartening array of messages, errors, and other strange cruft that comes with that. Start and finish buttons in the Eclipse IDE weren't responding. I tried running an AVD and got messages like "could not find virtual device".

There is a decent page on fixing the AVD problem in Windows and although anyone with half a wit can extrapolate and figure out that the similar can be done to fix the issue in Linux, I've done that and share:

ln -s /root/.android/avd ~/.android/avd

To summarize (for both cases), we're creating symbolic links from where the Android SDK thinks the AVDs should be to where they actually are. You will probably need to delete the directory 'avd' within your ~/.android directory before running that command.

While I'm at it, there is some general wonkiness getting Eclipse 3.5 to run properly on Ubuntu 9.10. I was having trouble with groups and owners (Android SDK installation has them set to root when you install so you'll have to chown and chgrp recursively through the tree) and sometimes certain features simply don't respond. The following code seems to alleviate the IDE issues.

Code:

#/usr/bin/bash
export GDK_NATIVE_WINDOWS=1
`~/bin/eclipsePackage/eclipse3.5/eclipse -vmargs -Xms128M -Xmx512M -XX:PermSize=128M -XX:MaxPermSize=512M &> /dev/null` &

This assumes that you are running Eclipse on Linux via a Bash script. Most of the command is from the usual Ubuntu/Eclipse forums on how to start eclipse. The important line is the one about the GDK stuff.

I hope that helps. We've been running Android 1.5, 1.6, and 2.0. Each time a new version gets released we all need to tweak things here and there. With how young the SDK is and how little there is about it out there the more stuff we can all share the better.

Michael
11/19/09

Emmm, Easter Egg Function

While carousing the code base I've been alerted of the existence of an interesting function:

PHP:

public static function the_answer_to_life_the_universe_and_everything() {
    
    $calculation sqrt(9) * 12;
    $calculation pow($calculation2);
    $calculation = ($calculation 4);
    $calculation = ($calculation 4);
    $calculation = ($calculation 10);
    $calculation = ($calculation 10);
    
    return $calculation;
}


Brilliant. +2 Intelligence, +4 Charisma if you get the reference. Yes, this made my day and yes, it was worthy of a blog post.

Michael
11/03/09

Rate Limiting in PHP

Recently I was having a discussion about putting together an API and some ideas about caching, rate limiting or flood protection, design patterns for rendering the output, scalability, and security came up.

A while ago I came up with a rate limiting class. It wasn't as thoroughly tested as I would hope as I left a little over a week after putting this together there, but basically we use time as a cross thread limiter for all connections. If we wanted to limit a particular user, then we'd simply have to create a unique key for the memcache entry for each user.

This logic uses time (as Unix timestamps with microseconds) as a method to "charge" each request against the server a "fee" which accrues against the max or limit. As time moves forward, it removes accrued cost from the memcache saved value.

The beauty of doing rate limiting in code is it allows the developer to manage flow and how limits are handled to protect our databases and CPU load; dishing out less expensive and properly (and expected) formed API responses.

Here's the code:

PHP:

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//     RATE LIMITER CLASS - rateLimiter.class.php
//
//     @date:    2009.05.21
//     @author     Michael Hradek <mhradek@gmail.com>
//     @version     1.0.0
//     @license     Apache License, Version 2.0
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////
// INCLUDES
////////////////////////////////////////////////////////////////////////////
 
require_once("initializeMemcache.class.php");
 
////////////////////////////////////////////////////////////////////////////
// PRIMARY CLASS
////////////////////////////////////////////////////////////////////////////
 
class rateLimiter extends initializeMemcache
{
    private $rateLimitKey;
    private $rateLimitMax;
    private $rateLimitCost;
    
    private $memcache;
    
    function __construct($rateLimitMax$rateLimitCost,
                         $rateLimitKey "rate_limiter_key")
    {
        $this->rateLimitMax        = (float)$rateLimitMax;
        $this->rateLimitCost       = (float)$rateLimitCost;
        $this->rateLimitKey        $rateLimitKey;
        
        $this->memcache         $this->initMemcache();
    }
        
    public function enforceRateLimit() 
    {
        $rateLimitDataMs $this->memcache->get($this->rateLimitKey);
        $currentTimeMs microtime(TRUE);
        if($rateLimitDataMs === FALSE
        {
            $this->memcache->set($this->rateLimitKey$currentTimeMs,
                                 MEMCACHE_COMPRESSED);
            return TRUE;
        }
        
        $timeDiffMs $rateLimitDataMs $currentTimeMs;
        if($timeDiffMs 0) {
            $rateLimitDataMs $currentTimeMs;
        }
        
        $rateLimitDataMs += $this->rateLimitCost;
        if($timeDiffMs $this->rateLimitMax)
        {
            if(!$this->memcache->replace($this->rateLimitKey,
                                $rateLimitDataMsMEMCACHE_COMPRESSED)) {
                $this->memcache->set($this->rateLimitKey$rateLimitDataMs,
                                     MEMCACHE_COMPRESSED);
            }
            
            return TRUE;
        }
        
        $rateLimitDataMs += ($this->rateLimitCost*2);
        if(!$this->memcache->replace($this->rateLimitKey$rateLimitDataMs,
                                     MEMCACHE_COMPRESSED)) {
            $this->memcache->set($this->rateLimitKey$rateLimitDataMs,
                                 MEMCACHE_COMPRESSED);
        }
 
        error_log(get_class().": Rate limit enforced.");
        return FALSE;
    }
}

The memcache class is simply something that reads memcache server configuration arrays and loads them into a memcache handle and returns it. I think with some larger volumes we can excuse things like misses in key lookups. We're already using a hashed configuration. Why add the trouble of retrying?

Michael
10/02/09

Touch MyTouch

With my recent phone number change I've changed phones from my old Blackberry Flip 8820 to the MyTouch 3G. It's a fantastic device as far as mobile phones go. I am excited to start developing apps for it. The Android platform is available for a lot of different mobile devices and will surely be a wonderful learning experience into the world of Java programming.

I've gotten Eclipse and the Android SDK loaded. Now it's time get something done. I'll share what we've got going on soon. I'm just glad I didn't join the droves of Apple fanboys. Supporting Google, however, doesn't make me feel all that much better.

Michael
09/30/09

PHP Unit & Exceptions

Recently I've been working on testing some code that throws exceptions. What a pain to test! I thought initially that I had just designed myself into disaster with my new objects. Functions should return results or boolean, right?

Read more »

Michael
07/27/09

Effort Nullification

Someone went through the effort of writing an entire database abstraction layer. Kudos? Nope: eventually I came across regular expressions and string replaces instead of binding parameters in their "parameterized query" scheme.

So disappointing. Honestly, I remember a mistake I made 5+ years ago of which Dennis still reminds me. I didn't quite understand associative arrays just yet and used all integers for keys. Now that is fun to understand!

Michael
07/21/09

PHP Static Properties

A quick note that if you instantiate a static class and call a member static method which writes to a member property, PHP will fail without telling you about it. It makes sense why that isn't legal, but it sucks that it happens without any clue... so it's a head banger of a problem to find.

PHP:

class testClass
{
    private $memberProperty;
    
    public static function doSchtuff($stuff)
    {
        $this->memberProperty $stuff;
        return true;
    }
    
    public function doSomthing($youzaCrazy) {
        return sha1($this->memberProperty.$youzaCrazy);
    }
}

In this particular problem the solution is to try and keep static and non-static functionality from inter mingling inside the same class. I know that isn't always a possibility, but I would point to interfaces or inheritance to keep things pleasantly organized.

unfortunately, keeping these sorts of things aren't always possible... especially when working with massive legacy piles.

Now that my head doesn't hurt anymore...

Michael
07/09/09

Hello World!

In my work I sometimes chat with colleagues and we laugh about how sometimes we come across code bases that are so bad they resemble lint balls in some of the projects we've been working on. Then someone, usually me, says something like "it wouldn't surprise me if we ran across a few 'Hello World!' lines in that mess."

It may not a surprise in some cases but it's always funny. And yes, it's happened.

Michael
05/19/09

W32 versus Unix Time

From here:

Windows NT time is specified as the number of 100 nanosecond intervals since January 1, 1601. UNIX time is specified as the number of seconds since January 1, 1970. There are 134,774 days (or 11,644,473,600 seconds) between these dates.

How to convert Windows NT Time to UNIX Time:

Divide by 10,000,000 and subtract 11,644,473,600.

How to convert UNIX Time to Windows NT Time:

Add 11,644,473,600 and multiply by 10,000,000.

Yeah, that wasn't funny at all. Yay for lack of documentation!

1 2 3 4 5 >>

This is an online journal for me. I might rant, ramble or review random things. Its my intention to post at least once a week. Thank you for checking this out. Your feedback is appreciated.

Search

free blog software