Unlocking Windows Phone 7 – ChevronWP7

December 31st, 2010 by Crazy No comments »

If you're a developer like me writing his own apps for his phone, then you're out of luck with the default windows 7 phone.

To 'unlock' it you have to sign up for a developer account on APP HUB, meaning you have to pay $99.

Lucky for us, a group of people quickly found a way to unlock the phone, so saving us the $99.

The tool I'm referring to is ChevronWP7. Although it wasn't on-line for that long, it does a great job.

The creators were contacted by Microsoft for talks about the tool and will sit together in mid January.

It appears MS has plans to open up the platform to us home developers as well. But that will happen maybe on the big January release and that sounds fast, so most likely it'll take several more months(if not longer). To speed up the negotiations the tool was taken off line by the ChevronWP7 team.

Now that the history lesson is over, let's get down to unlocking our phone.

Download this zip file, it contains everything you'll need:
Download ChevronWP7

The tool looks like this:

Before running ChevronWP7 do the following:

  • Download and install Zune desktop software, you can get it here.
  • Download and install Windows Phone SDK or run the registry key in the zip(You need administrator permissions for this).
  • Connect your phone and let is sync.
  • If required change the relationship in the Zune software to "Full Sync" and not "Guest Access"
  • Install the certificate in the zip file on your phone. This can be done by emailing it to yourself and opening the .cer file on the phone.
  • Be sure your phone isn't locked. Just leave it on the start screen
  • Open ChevronWP7.exe
  • Check the checkboxes and click 'Unlock'
     

Troubleshooting:

  • Make sure you have the certificate installed and your device is completely connected to your PC.
  • Make sure your device's screen is on and sitting on the Start screen.
  • Make sure the following registry key exists:[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\WindowsPhone\\ProxyPorts]“DeviceReg”=dword:000069C5
  • Programs running on port 443(example Skype and ssh) block the unlocking and deployment close all applications you think can cause this

 

The next step is how to deploy an app on your phone.

In the zip I also included a PictureViewer.xap file. This is the application for on the phone. It's like an exe or cab file.

When you installed the Windows Phone SDK you can deploy applications.

Open 'Application Deployment', should be in your start menu under 'Windows Phone Developer Tools'.

Once open, browse to the xap file and make sure your phone is connected, it's not locked and Zune is open and in sync.

Press 'Deploy' and the application will appear between your other apps.

That's it!

Using APC to store your data

August 31st, 2010 by Crazy No comments »

Sometimes you have large amount of generated data that you don’t want to create on every web request.


An examples would be a home page with several news posts on it.
The text would be in a database, but all the ‘extra’ things you did like bbtags present in the text, information about the user that posted it, etc etc requires extra processing.


If these queries and calculation only have to be done once then it would mean a big performance gain. Especially if it’s on pages that get requested allot.


The solution for this is shared memory. APC has this, if you do not know what APC is, please read this blog post about opcode cachers.

So, we have data and we want to store it in memory, what we need is a small wrapper class that does all the work for us, explanation is in the code:

<?php
/**
 * Contains the APCCache class to store, retrieve and delete data from the APC shared memory
 * @package    Tools
 * @author     Crazy <crazytje@gmail.com>
 * @copyright  2000-2010 Crazy
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @filesource
 * @link       http://www.php.net/manual/en/book.apc.php
 */
/**
 * used to store, retrieve and delete data from the APC shared memory
 *
 * @package    Tools
 * @author     Crazy <crazytje@gmail.com>
 * @copyright  2000-2010 Crazy
 * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link       http://www.php.net/manual/en/book.apc.php
 */
class APCCache {
    /**
     * Contains the prefix that will be prepended to the identifiers
     * @var string
     */
    private static $prefix = 'MyPrefix';
 
    /** 
     * Stores a variable in the shared memory
     * 
     * @param string $pKey Unique identifier for the data
     * @param mixed $pValue Data to store
     *
     */
    public static function SetVar($pKey, $pValue) {
        return apc_store(self::$prefix.'.'.$pKey, $pValue);
    }
 
    /**
     * Returns a stored variable from the shared memory
     *
     * @param string Data identifier
     * 
     * @return mixed stored data
     *
     */
    public static function GetVar($pKey) {
        return apc_fetch(self::$prefix.'.'.$pKey);
    }
 
 
    /**
     * Removed data from the shared memory
     *
     * @param string Data identifier
     *
     */
    public static function Destroy($pKey) {
        return apc_delete(self::$prefix.'.'.$pKey);
    }
 
    /**
     * Sets a prefix for your identifiers
     * This is to prevent conflicts with software also using apc
     *
     * @param string $pValue prefix 
     */
    public static SetPrefix($pValue) {
        self::$prefix = $pValue;
    }
}
?>

Example usage:

<?php
 
APCCache::SetVar('MyTest', 'wOOt');
$var = APCCache::GetVar('MyTest');
 
 
//outputs 'MyTest'
echo $var;
?>

Easy right?

Evolve: PhpDocumentor Template

August 29th, 2010 by Crazy No comments »

For those of you that do not know phpDocumentor, a small intro:

phpDocumentor, sometimes referred to as phpdoc or phpdocu, is the current standard auto-documentation tool for the php language. Similar to Javadoc, and written in php, phpDocumentor can be used from the command line or a web interface to create professional documentation from php source code. phpDocumentor has support for linking between documentation, incorporating user level documents like tutorials and creation of highlighted source code with cross referencing to php general documentation. A complete list of features is available. phpDocumentor uses an extensive templating system to change your source code comments into human readable, and hence useful, formats. This system allows the creation of easy to read documentation in 15 different pre-designed HTML versions, PDF format, Windows Helpfile CHM format, and in Docbook XML. You can also create your own templates to match the look and feel of your project.

The 'problem' with phpDocumentor is the templates. They're ugly and 'old style', completely out of date. The best template I have found is a template based on frames.

So I decided to make my own template.

You can find it here.

Optimizing your web server: Part 4b – XDebug Profiler

November 23rd, 2009 by Crazy No comments »
Here we are, part 4b. By using XDebug, a pecl extension, our php code can be analyzed pinpointing the slower functions. Installing XDebug is easy:
pecl install xdebug
Now, you'll have to enable it in the php.ini file:
extension=xdebug.so
xdebug.profiler_enable=1
xdebug.profiler_output_name=profiler-%s-%t.cache
xdebug.profiler_output_dir=/tmp/xdebug
Don't forget to create the directory and give apache enough permissions to write in it. So after restarting apache you'll see new files showing up under '/tmp/xdebug/'. These are, as the XDebug documentation says 'cachegrind compatible files'. As I'm using putty on windows to connect to my webserver and the webserver does not run a GUI, I'll be using wincachegrind to display the information. To make it easy for myself, I use winscp and make it so that the cachegrind files are opened with WinCacheGrind. Setting this up goes beyond the scope of this post and greatly depends on your setup/way of working. Anyway, up till now we managed to install XDebug, enabling the extension, generate the cachegrind files and now opening them with WinCacheGrind. Upon opening such a file in wingrind, you'll see something similar to this:

main(click to enlarge) As this blog post was made while I was profiling one of my own sites, I'll use a real world example. In the right hand side, the {main} takes 89ms to process. Let us expand this to see what function is taking the most time. functions When looking at the functons that take the most time. I noticed, as you can see in the screenshot above, the Component->startup and the beforeFilter both take 13ms. The component is the cakephp cookie class, it seems the startup of this class takes 12ms. To be exact, it's the decrypting of the cookie that takes 11ms. The same is true for the beforeFilter, in that method I read the cookie, taking 12ms, because it decrypts again. cookie After seeing this, I changed the code so that on the first hit on the site the information in the cookie is is copied into the session, as this was super fast(less then 1ms). Saving me 2 times 12ms. That comes down to 27% faster! To put things even more in context. This was a base class, all my pages inherited from that class, so it saved me those 24ms on EACH request. Imagine doing that to all pages, or one of those heavy visited pages on your own site. Worth the 10 minutes it takes to install XDebug with WinCacheGrind, no? Optimizing your web server: Part 1 – Gzip Optimizing your web server: Part 2 – Keep Alives Optimizing your web server: Part 3 – Opcode Caching Optimizing your web server: Part 4a – PHP Optimizing your web server: Part 4b – XDebug Profiler

Optimizing your web server: Part 4a – PHP

November 23rd, 2009 by Crazy No comments »
Let me start by saying, I won't bore you with the list of what I call stupid optimizations. That's why I call this 4a, I recommend skipping 4a and go directly to 4b :) When I first when on my quest to optimize my code, I was disappointed at what I found. Let me give you a short list of 'tips' you find when searching for 'php optimize code'. Some have a little value, others are crap. The results will state things like this:
  • echo vs print
  • pass objects by reference
  • remove comments from your code
  • use single quotes
  • error suppression with @
  • use assosiative arrays
  • use for loops with the count before the loop
  • unset your variables
  • ...


Now, seriously, the echo vs print again??, yes, totally worthless. If I want to make my site faster, there are better ways then to replace print with echo, for the amazing 0.001 second.

The pass objects by reference, this one is true, but only IF you know what you're doing. For base types even if you pass by reference it won't make a difference. Only use it for objects and even then, you have to watch out, in many cases you'll actually slow your script down by forcing a copy when it wouldn’t otherwise be made.

Removing comments from your code. Well, I had a good laugh with this one. If you're going to do that, then you might want to install an opcode cacher, see my other blog posts about them. They don't cache this, so instead, document your code!, as much as possible. More (good)comment makes it readable 5 to 10 years from now, not to mention causing less issues/bugs in the process.

Use single quotes. This is true, if you're interested in that soooo little difference. Personally I made a habit of using the single quotes, but that's just so that I don't have to press the shift key to type them(Yea, I'm lazy).

Error suppression with @. It is slow, yes, but I never use the @. When writing your code, error handling is part of it. You should not suppress them, you should fix them!. And if errors do happen, then use a try/catch and handle the error, do NOT ignore errors.

Use assosiative arrays, I hear that using [5] is 7 times slower then ['me'], haven't run any benchmarks, but might be a good idea to use this when having big arrays.

Use for loops with the count before the loop, when using for($i=0; $i< count(array); $i++) the count(array) is executed every time. It's useful to put this outside of the for loop. But then again, you could count backwards, $i=count(array);0<=$i; $i--, or something similar.

The last one on the list, unset your variables, is only if you use var's with allot of data in them, or when using big objects. You won't win anything if you unset ALL your variables, your script would be spending more time unsetting them then actually keeping them in memory. This is more the case when doing large queries on a database for example. Unsetting a couple of MB of data might be a good idea :) If my memory serves me right, php6 will implement garbage collection, making this unnecessary.

So, this post was about optimizing your php code right?, well, instead of doing all those ridicules things listed above, we'll do it the right way. A profiler!, see where your code is slow, and try and make it faster on those places!. Optimizing your code that way is WAY more efficient then doing all those small silly things.
Don't get me wrong, if you like coding in those small optimizations, go ahead. I just find it stupid to go and change those things afterwards.

So on to part 4b :) , where I'll be using XDebug to find the slow spots in the php code.

Optimizing your web server: Part 1 – Gzip Optimizing your web server: Part 2 – Keep Alives Optimizing your web server: Part 3 – Opcode Caching Optimizing your web server: Part 4a – PHP Optimizing your web server: Part 4b – XDebug Profiler