Translate

Archive for the 'Code' Category

eclipse.ini gone after Helios update? No worries

Saturday, March 5th, 2011

So you had a pimped out eclipse.ini file and for some odd reason in the world, eclipse.ini is missing in action after one of the numerous updates, you try to put it where it was, but none of the options you pass it seem to have an effect.

Here’s what I did

After I gave up on reading the documentation on what the hell happened to eclipse.ini and why it’s not having any effect on my eclipse, I just created my own eclipse launcher.

Here’s what it has inside:

#!/bin/bash
./eclipse -vmargs -XX:MaxPermSize=512M-Xms512m -Xmx1024m -XX:+UseParallelGC -XX:PermSize=256M
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

New Location of src.jar for JDK 1.6.0_24 on Mac OS X

Saturday, March 5th, 2011

These Apple devs… why do you keep moving src.jar?

Here’s the new location:

/Library/Java/JavaVirtualMachines/1.6.0_24-b07-330.jdk/Contents/Home/src.jar

Not there!
If you don’t see src.jar there, and you’re running Mac OSX 10.6, and you did download the Java system update, you will have to go to developer.apple.com and download the “Java for Mac OS X 10.6 (10M3321) (Disk Image)” (or whatever is current and makes sense for your Mac OSX version)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

How to convert Android GPS coordinates into X,Y coordinates.

Thursday, December 30th, 2010

Without further math bullshit about all the conversion systems, when you have a bunch of Android GPS coordinates (which are compatible with Google Earth and Google Maps), and you want to draw them on a finite 2D plane, here’s what worked for me.

int x =  (int) ((PLANE_WIDTH/360.0) * (180 + lon));
int y =  (int) ((PLANE_HEIGHT/180.0) * (90 - lat));
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Animating a game-like sky with HTML5 Canvas

Tuesday, December 28th, 2010


Try it | View Source

Again playing a little more with HTML5 and Canvas animation.

This time around the result is a little more pleasing to the eye, based on some ideas I have for a little arcade game I want to make I’ve created a gradient blue sky and clouds that move either to the left or right at different speeds.

If I’m correct, I don’t think there is a way to move something that’s already been painted in the canvas, therefore I made the Clouds a “Cloud” object (I used to think I could paint a vector based graphic and then manipulate it in the canvas, but I think now the HTML Canvas is more of a place where you paint and you can’t really manipulate what’s already been painted). Each cloud is nothing but an array of white circles. The program initializes several clouds randomly and then with an interval call it repaints the gradient sky, and each one of the clouds, then moves them, to repeat the cycle over and over.

I’m not sure if game devs out there are doing this or if there is a way to have the canvas move vector objects in a more efficient way than repainting the whole thing from scratch on every frame. At this point the animation looks very smooth but nothing except clouds moving is happening.

I hope in your comments I’ll find some answers as if this is the way to animate your game, meaning, keeping object representations of everything that’s been painted in memory, and repainting the canvas on every frame of the game cycle.

My goal (which I don’t know if is correct) is to do everything within a single CANVAS object. I’ve seen though how some people still like to play around with the DOM Tree, I’m not sure if adding and manipulating the DOM Tree is the correct way to procede here, I think it’d also be a nightmare to handle things when it comes to focus, or converting coordinates from one canvas to another and have everything make sense.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

jconsole: “Connection Failed: Retry?” #SOLVED #java #jmx

Sunday, November 21st, 2010

So you wrote a piece of server software and you found out about JMX and the jconsole tool to remotely monitor all sorts of interesting metrics remotely, all without adding a single line of code to your project.

Say you want to run it the simplest way possible with no authentication, the tutorial says that these are the options you need to pass to the remote virtual machine to enable JMX remote monitoring on some port (let’s put 9595 for illustrative purposes).

-Dcom.sun.management.jmxremote.port=9595',
-Dcom.sun.management.jmxremote.ssl=false',
-Dcom.sun.management.jmxremote.authenticate=false

right?

But when you open your jconsole on your local computer to connect to the remote server…

jconsole myserver.com:9696

You get this fucking error no matter what you do.

You’re just missing one more option they must have forgotten to mention in the retard tutorial at oracle.com

(let’s use IP address 72.14.204.147 as the remote server IP)

-Dcom.sun.management.jmxremote.port=9595',
-Dcom.sun.management.jmxremote.ssl=false',
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=72.14.204.147 #ip of the remote machine, yes the ip, not the name

Voilà

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Mercurial for Subversion Expats: Merging remote changes. “abort: push creates new remote heads!”

Sunday, November 14th, 2010


Commit anywhere/anytime with Mercurial

So you have been using subversion for the past few years and now your team has decided to move on to Mercurial for all the benefits. Two or more people are working on the same branch and they’re pushing code to the main copy of the repository before you’re done with your changes.

In the Subversion world, if you tried to commit in this same situation you’d get an error saying that your repository checkout is not up to date, so you’d fix this by doing:

$ svn up #gets the latest changes from the repo and it tries to merge all remote changes
$ # ... solve any conflicts if they arise.
$ svn ci -m "my commit message" #push your latest changes to the main repo.

In Mercurial it’s a little bit longer

$ hg ci -m "my local changes"
$ hg pull #this brings the latest version of the repo, but doesn't change the state of your files.
$ hg merge #when you're ready, you merge the changes

If at this point you try to push, you will get the following error

$ hg push
pushing to https://user@server.com/company/project
searching for changes
abort: push creates new remote heads!
(did you forget to merge? use push -f to force)

first, ignore the “push -f to force”, they should remove that message and put something like

(did you forget to merge and commit?)

After a lot of thinking I was doing something wrong with the merge, I realized that before pushing you have to commit your merge locally as well, makes sense, so the whole sequence should be like this instead:

$ hg ci -m "my local changes" #same as 'hg commit'
$ hg pull #this brings the latest version of the repo, but doesn't change the state of your files.
$ hg merge #when you're ready, you merge the changes
$ hg ci -m "merging latest changes from repository"
$ hg push
pushing to https://user@server.com/company/project
searching for changes
http authorization required
realm: Bitbucket.org HTTP
user: gubatron
password:
adding changesets
adding manifests
adding file changes
added 2 changesets with 1 changes to 1 files
bb/acl: gubatron is allowed. accepted payload.

So, yes, its a little more work to merge and commit, but remember that you’re working now on a distributed fashion and you have to think a little bit differently, you gotta merge locally, fix any conflicts if they arise, commit the changes and push them. In exchange you don’t get to deal with lots of .svn folders, no single point of failure (one remote repo that could go down and leave all developers without version control until it’s back up), and super easy branching on a single folder, no need to checkout branches and be writing down revision numbers.

Just remember that pulls don’t change your local changes unless you explicitly ask to do so by invoking hg merge; hg commit, which would be the equivalent (at least I see it like this) to svn up

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Android: How to obtain the WiFi’s corresponding NetworkInterface

Sunday, September 19th, 2010

Let’s say for some odd reason in the world you do need to get the corresponding NetworkInterface object of the Wifi on your android, in my case I needed to have my WiFi device send multicast packets, and I wanted my MulticastSocket to only send packets through the WiFi device (not 3g, or maybe even ethernet). The android API does not provide functionality to know what “NetworkInterface” your WiFi is using.

Here’s a solution proven in tens of different android phones, seems to work 100%.

public static NetworkInterface getWifiNetworkInterface(WifiManager manager) {

    Enumeration<NetworkInterface> interfaces = null;
    try {
        //the WiFi network interface will be one of these.
        interfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        return null;
    }

    //We'll use the WiFiManager's ConnectionInfo IP address and compare it with
    //the ips of the enumerated NetworkInterfaces to find the WiFi NetworkInterface.

    //Wifi manager gets a ConnectionInfo object that has the ipAdress as an int
    //It's endianness could be different as the one on java.net.InetAddress
    //maybe this varies from device to device, the android API has no documentation on this method.
    int wifiIP = manager.getConnectionInfo().getIpAddress();

    //so I keep the same IP number with the reverse endianness
    int reverseWifiIP = Integer.reverseBytes(wifiIP); 		

    while (interfaces.hasMoreElements()) {

        NetworkInterface iface = interfaces.nextElement();

        //since each interface could have many InetAddresses...
        Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            InetAddress nextElement = inetAddresses.nextElement();
            int byteArrayToInt = byteArrayToInt(nextElement.getAddress(),0);

            //grab that IP in byte[] form and convert it to int, then compare it
            //to the IP given by the WifiManager's ConnectionInfo. We compare
            //in both endianness to make sure we get it.
            if (byteArrayToInt == wifiIP || byteArrayToInt == reverseWifiIP) {
                return iface;
            }
        }
    }

    return null;
}

public static final int byteArrayToInt(byte[] arr, int offset) {
    if (arr == null || arr.length - offset < 4)
        return -1;

    int r0 = (arr[offset] & 0xFF) << 24;
    int r1 = (arr[offset + 1] & 0xFF) << 16;
    int r2 = (arr[offset + 2] & 0xFF) << 8;
    int r3 = arr[offset + 3] & 0xFF;
    return r0 + r1 + r2 + r3;
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Map function in Java

Tuesday, August 31st, 2010

I read on some email signature something along the lines of:
“If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich"

After coding on Android and learning about some of the tips for performance, like
"With an ArrayList, a hand-written counted loop is about 3x faster"

If you do use ArrayLists a lot you then have that tip in the back of your head and you end up with a lot of code like the following:

List<T> myList = ... ;
int size = myList.size();
for (int i=0; i < size; i++) {
  T elem = myList.get(i);
  //do something with elem
}

Eventually there was a moment when I said, I need a freaking map function, I’m sick of this little pattern, surprisingly I couldn’t find a map() function anywhere on the java collection framework. So I went and did this.

Warning: These mapping utilities are meant only for Lists with RandomAccess capabilities (.get(i) happens in constant time). Do not use with LinkedList or other lists that don’t implement RandomAccess, otherwise you’ll end up with up to O(n^2) times. Thanks to Roger Kapsi for noticing this issue. [you can now tell how much we like to use ArrayList]

//what I think would be the equivalent of a lambda or closure in Python...
public interface MapFunction<T> {
   public void map(T t);
}

And then on one of my utils classes I added a static method map that looks like this:

public static <T> void map(List<T> list, MapFunction<T> mapFunction) {
int size=list.size();
   for (int i=0; i < size; i++) {
      mapFunction.map(list.get(i));
   }
}

So now, everytime I need to iterate over a whole list and do something to each element it’s a lot cleaner

//let's serialize all the messages...
List<Message> messages = ...;
Utils.map(messages, new MapFunction<Message> {
   public void map(Message message) {
      Engine.INSTANCE.SERIALIZER.save(message);
   }
});

done deal.

Update

Here’s another Map utility that let’s your map() function know about the index of your current element. You may have to treat certain elements differently based on their position in the list.
This time your function takes a “IndexMapFunction” implementation.

public static <T> void map(List<T> list, IndexedMapFunction<T> mapFunction) {
		int size = list.size();
		for (int i = 0; i < size; i++) {
			mapFunction.map(i, list.get(i));
		}
}

The interface for IndexedMapFunction looks like this

public interface IndexedMapFunction<T> {
	public void map(int i, T obj);
}

In your implementation of “map(int i, T obj)”, “i” represents the position of the element being treated. You could do different things depending on what position of the List you are. For example, you could know if you’re at the beggining or end of the list, or maybe you could other data structure telling you things about some positions in the list in question.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Reference: Passing functions as parameters in C

Sunday, July 18th, 2010
#include <stdio.h>
//Example to show how to pass
//functions as parameters in C.                                               

//simple function that returns the sum of two ints
int sum(int i, int j) {
  return i+j;
}

//simple function that prints a char*
void printSomething(char* something) {
  printf("%s\n",something);
}

//function that takes
// 2 ints
// 1 char*
// one function that returns an int and takes two ints
// one function that takes a char* returns nothing
void functionThatTakesOtherFunctions(int a,
                int b,
                char* name,
                int (functionA) (int,int),
                void (functionB) (char*)) {
  printf("Function A: %d\n",functionA(a,b));
  functionB(name);
}

int main(void) {
  //we pass the first two functions as parameters
  functionThatTakesOtherFunctions(3,4,
                                  "John Doe",
                                  sum,
                                  printSomething);
  return 0;
}

This worked fine on my gcc compiler (MacOSX 10.5), no need to even use * or & operators when defining the parameters or when passing the functions as parameters.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

startKeychain – bash utility to start ssh-agent

Thursday, June 3rd, 2010

For my (and your) future reference, here’s a function to put on your .bashrc or .bash_profile, you can invoke it later at any time to start/re-start your ssh-agent.

function startKeychain {
    killall ssh-agent
    rm ~/.keychain/*
    keychain id_rsa
    HOSTNAME=`hostname`
    source ~/.keychain/${HOSTNAME}-sh
}

Then at any time, the “command” startKeychain will be available on your command line.

Output should look something like this:


gubatron@gubatron-desktop:~$ startKeychain 

KeyChain 2.6.8; http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

 * Initializing /home/gubatron/.keychain/gubatron-desktop-sh file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-csh file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-fish file...
 * Starting ssh-agent
 * Initializing /home/gubatron/.keychain/gubatron-desktop-sh-gpg file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-csh-gpg file...
 * Initializing /home/gubatron/.keychain/gubatron-desktop-fish-gpg file...
 * Starting gpg-agent
 * Adding 1 ssh key(s)...
Identity added: /home/gubatron/.ssh/id_rsa (/home/gubatron/.ssh/id_rsa)

Comments are welcome to improve it, I’m not an ssh-agent expert, but this seems to do the work.

[SOLVED] Issue with KDE 4.4.2 and Dolphin always asking my ssh passwords whenever I browsed folder I checked out from a remote subversion repository.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004