Testing if hostname is numeric IPv4

I had to resort this hack when testing a hybrid web/mobile site which uses site hostname based device discrimination. In production mode we can have m.yoursite.com and www.yoursite.com hostnames. However, when running the site locally, on your development computer and in LAN this does not work very well: one cannot spoof hostnames for web browsers in devices like iPhone/iPod/other mobile phone unless you install a DNS server. And installing a DNS server for LAN is something you don’t want to do…

So, I figured out that I can use  hostname spoofing on desktop computers (/etc/hosts file) and I always access the site via numeric IP (IPv4 over ethernet) when testing over WLAN on mobile devices.

  • The site is rendered in web mode when it is being accessed via textual hostname (localhost, yourpcname)
  • The site is rendered in mobile mode when it is being accessed via IPv4 numeric hostname (127.0.0.1, 196.168.200.1)

And,… dadaa,… here is my magical code to test whether hostname is numeric IPv4. I couldn’t find a ready function from Python standard library

import re

ipv4_regex_source = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
ipv4_regex = re.compile(ipv4_regex_source)

def is_numeric_ipv4(str):
    """

    http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/

    @param str: Hostname as a string.

    @return: True if the given string is numeric IPv4 address
    """
    # ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
    return ipv4_regex.match(str)

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

UIScrollView & setContentOffset random scrolling on iOS 4

We encountered a nasty bug with UIScrollView while porting app to iOS 4. The following code worked just fine on old iPhone versions:

 
// Update view with new content
[self updateContent:...]

// We want the view to be on top every time the it is shown
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];

// And set the correct size to enable scrolling.
// contentHeight is updated when setting the content of subviews.
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, contentheight)];

No problems. But on iOS 4 the UIScrollView decided to do something fancy and scroll to random position depending on where it was left when the view was hidden the last time. After spending expensive manpower to solve the issue by trying numerous ways to tell the scrollView to go to top using setContentOffset, the final solution was simple yet very different what one might think: to fix this we had to set the contentSize to zero before updating any of the content.

// Need to do this on iOS 4 or the view scrolls to random position
[scrollView setContentSize:CGSizeMake(0,0)];

// Update view with new content
[self updateContent:...]

// We want the view to be on top every time it is shown
[scrollView setContentOffset:CGPointMake(0,0) animated:NO];

// And set the correct size to enable scrolling.
// contentHeight is updated when setting the content of subviews.
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, contentheight)];

Hopefully there is more logical way to update scrollview’s content without being scrolled around.

Xcode and iPhone simulator: The application won’t launch

This is another migration issue from iOS 3.x to iOS 4.x projects.

The application does not launch in XCode when running Simulator – Debug. Instead it dies (SIGABRT) before entering main().

#0     0x91a02ef6 in __kill
#1     0x91a02ee8 in kill$UNIX2003
#2     0x91a9562d in raise
#3     0x91aab6e4 in abort
#4     0x0245e8c9 in __springboard_unimplemented
#5     0x0246d6b2 in mcount
#6     0x00002298 in main at main.m:11

The answer lies here. mcount() function is profiling function. Profiling is available only on iPhone device build. The setting “Generate Profile Code” somehow gets into foobar state when you migrate the project from older XCode. Note the setting is present under both Project and Target settings so you might need to fix it under the both sections.

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

Open source contribution agreement template

We are looking for creating contribution agreements for few new open source projects. IANAL, but hiring a real lawyer is freaking expensive.

The thing is that we, us a company, want to guarantee that all code coming into the project is “clean”. We also want to guarantee our right to change the license in the future (GPL -> BSD, GPL -> Apache, etc.)

Thus far, the best free, as in freedom and in beer, contribution agreement template we have found is Sun Contribution Agreement 1.5 which is available under  Creative Commons Attribution-Share Alike 3.0 license. It is at least used by high profile Phonegap project (Nitobi as the company) if you don’t count OpenSolaris anymore as open source project.

IANAL, but if I understood correctly, the agreement basically says

  • the company can do whatever it wish with your contributions (joint ownership)
  • the company is entitled to release your contributions under open source license – perfect for GPL’ed projects. The exact wording is terms. Any contribution we make available under any license will also be made available under a suitable FSF (Free Software Foundation) or OSI (Open Source Initiative) approved license.

Since I couldn’t find the orignal document in editable form (PDF was the best I could get) I made OpenOffice.org ODS document out of it with easily replaceable identification information.

Comments welcome.

The agreement text pasted below.

YOURPROJECT Contributor Agreement

These terms apply to your contribution of materials to the YOURCOMPANY ("us"/"our"), and set out the intellectual property rights you grant to us in the contributed materials.  If this contribution is on behalf of a company, the term "you" will also mean the company you identify below. If you agree to be bound by these terms, fill in the information requested below and provide your signature. 

Read this agreement carefully before signing. 

1.  The term "contribution" means any source code, object code, patch, tool, sample, graphic, specification, manual, documentation, or any other material posted or submitted by you to the project. 

2.  With respect to any worldwide copyrights, or copyright applications and registrations, in your contribution: 

you assign to us joint ownership through this document, and to the extent that such assignment is or becomes invalid, ineffective or unenforceable, through this document you grant to us a perpetual, irrevocable, non-exclusive, worldwide, no-charge, royalty-free, unrestricted license to exercise all rights under those copyrights. This includes, at our option, the right to sublicense these same rights to third parties through multiple levels of sublicensees or other licensing arrangements;
you agree that each of us can do all things in relation to your contribution as if each of us were the sole owners, and if one of us makes a derivative work of your contribution, the one who makes the derivative work (or has it made) will be the sole owner of that derivative work;
you agree that you will not assert any moral rights in your contribution against us, our licensees or transferees;
you agree that we may register a copyright in your contribution and exercise all ownership rights associated with it; and
you agree that neither of us has any duty to consult with, obtain the consent of, pay, or give an accounting to the other for any use or distribution of your contribution. 

3.  With respect to any patents you own, or that you can license without payment to any third party, through this document you grant to us a perpetual, irrevocable, non-exclusive, worldwide, no-charge, royalty-free license to: 

make, have made, use, sell, offer to sell, import, and otherwise transfer your contribution in whole or in part, alone or in combination with or included in any product, work or materials arising out of the project to which your contribution was submitted, and
at our option, to sublicense these same rights to third parties through multiple levels of sublicensees or other licensing arrangements. 

4.  Except as set out above, you keep all right, title, and interest in your contribution.  The rights that you grant to us under these terms are effective on the date you first submitted a contribution to us, even if your submission took place before the date you sign these terms. Any contribution we make available under any license will also be made available under a Free Culture (as defined by http://freedomdefined.org)  or Free Software/Open Source licence (as defined and approved by the Free Software Foundation or the Open Source Initiative).

5.  With respect to your contribution, you represent that it is an original work and that you can legally grant the rights set out in these terms; 

it does not to the best of your knowledge violate any third party's copyrights, trademarks, patents, or other intellectual property rights; and
you are authorized to sign this contract on behalf of your company (if identified below). 

6.  The place of performance is the registered seat of

	YOURCOMPANYNAME
	YOURCOMPANYADDRESS1
	YOURCOMPANYADDRESS2
	YOURCOUNTRY
	YOURCOMPANYBUSINESSID	

Any disputes concerning this agreement including the issue of its valid conclusion and its pre and past contractual effects are exclusively decided by the competent court in YOURHOMECITY, YOURCOUNTRY or, at our discretion, also by the competent court is whose district you may have your residence, your registered seat, an establishment or assets.

If available, please list your YOURPROJECT username(s) for the YOURPROJECT systems.

Username(s): __________________________________________________________________

_______________________________________________________________________________

Your contact information (Please print clearly) 

Your name: ____________________________________________________________________

Your company's name (if applicable): __________________________________________

Mailing address: ______________________________________________________________

Telephone, Fax and Email: _____________________________________________________

Your signature: _______________________________________________________________

Date: _________________________________________________________________________

To complete this agreement:
email a scanned copy of a signed agreement to
fax a signed copy to + .....; or
post a signed copy to:

	YOURCOPMANYNAME
	YOURCOMPANYADDRESS1
	YOURCOMPANYADDRESS2
	YOURCOUNTRY

This agreement is based on version 1.5 of the Sun Contributor Agreement, which
can be found at:

    http://www.sun.com/software/opensource/contributor_agreement.jsp

This document is licensed under a Creative Commons Attribution-Share Alike 3.0
Unported License http://creativecommons.org/licenses/by-sa/3.0

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+