Tuesday, April 14, 2015

British Summer Time and System.currentTimeMillis()

It's that time of the year when I remember that you can't directly use System.currentTimeMillis() to calculate the current time, since it is now an hour out.  D'oh!

Thursday, February 19, 2015

Deity - "Multiplayer Populous"

My latest Android project is ready to announce: Deity.  If you remember Populous, then you'll be right at home here.  This is a multi-player turn-based strategy game.

You play a God who begins the game with a small group of followers and not much power (or "Mana" as professional Gods call it). 


As a God you can move your followers about, and ask them to either pray (giving you more Mana), grow crops (which are needed for your followers to survive) or build a house (which will generate more followers).

Once you have some Mana, the real fun begins.  You can create god-like effects to try and destroy your enemy God's followers, like floods, earthquakes and volcanos.  See the lava in the above screenshot!

This game is still in alpha, and I'll be the first to say that the graphics are a bit ropey, but I wanted to get it out there as quickly as possible as it's ready to play now, completely free, and has no adverts or IAPs.  What have you got to lose.

Monday, February 09, 2015

Seeing into the Future

The Linux "locate" command has a "-e" option which "Print only entries that refer to files existing at the time locate is run.". 

Does this mean that if you leave this out, it will find files that don't exist yet?  I'm running "$> locate half-life3" now...

Thursday, July 03, 2014

Hey!

Hey!  Do you breath air?  And do you like messaging?  If the answer to either of those questions is true, then you need Hey!, the new way to stay in contact with friends for free.

Hey! is a free Android app for sending and receiving message from other Hey! users quickly and easily.  It costs nothing, and there's no adverts!

So join Hey! today!

Monday, June 23, 2014

Think Anagrams Are Easy?

So did I until I wrote my new education app 'Alien Anagrams'.  It's a sort-of shoot'em-up, except you shoot bullets by correctly guessing anagrams.


Take the anagram in the screenshot above: "lseo".  it took me a good few minutes to guess it, when I would have assumed a 4-letter anagram would take about 4 seconds.  How many combinations can there be?  But for some reason my brain keeps thinking of words like "leo's" or "selo"[tape].  Still, that's why I like playing this game; if you lose it's your own fault, and when the computer shows you the word, it suddenly becomes obvious what the answer was.

Give it a go (the first few levels are free, and they are challenging enough) and see how well you do.

Friday, June 13, 2014

System-Wide Mutex

It's been a long long time since I actually posted anything directly to do with Java programming, so here I am to make up for that.

In one of my projects I needed to create a system-wide mutex (a "mutex" being an object or "flag" to say that something is locked, e.g. a method or bit of code, so no other process can call it).  I needed it to be system-wide since I had multiple programs running on the same computer that needed to be in sync, so I couldn't use the synchronized keyword.

So I created a new class which I've called  FileSystemMutex.  It's very simple, and works by creating a temporary file to indicate that the lock is enabled and the file is deleted when the lock is released.  Because it uses the filesystem, it is "global" across everything that uses that filesystem.

Here is the code:-


import java.io.File;
import java.io.IOException;

public class FileSystemMutex {
   
    private File file;
    private long max_duration;
   
    public FileSystemMutex(String _filename, long _max_duration) {
        super();
       
        file = new File(_filename);
        max_duration = _max_duration;
    }
   
   
    public void waitForLock() throws IOException {
        while (isLocked()) {

            // This is my own function.
            // Surely everyone's written their own "Thread.sleep()" 
            // wrapper by now?
            Functions.delay(100); // Wait for a sec 
        }
        lock();   
    }
   
   
   
private boolean isLocked() {
        boolean exists = file.exists();
        if (exists) {

            // It might exist but is it too old?
            long age = file.lastModified();
            if (age + max_duration < System.currentTimeMillis()) {
                // Too old!
                release(); // Delete the file
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
   
   
    private void lock() throws IOException {
        file.createNewFile();
    }
   
   
    public void release() {
        file.delete();
    }

}



One thing to always remember with locks it to ensure that under no circumstances should the lock get left on, usually by a program ending prematurely.  This is why I've included the failsafe "maximum duration" parameter, so if the process that created the lock "crashes", the lock will still be released, eventually.

You would use it like this:-

    private static FileSystemMutex fsmutex = 
        new FileSystemMutex("my_lock", 1000*60); // Might as well make it static

 
    public void MyFunction() {
        fsmutex.waitForLock();
        try {
            [ do something that requires locking this bit of the code]

        } finally {
            fsmutex.release();

        }
    }


'Tis all.