talkingCode

Facebook Syndication Error – Getting your RSS back

posted by codders in code, ruby

If you’re anything like me, you grudgingly use Facebook while all the time warning everybody of the dangers of supporting a company who builds the tools that would all too easily facilitate the installation of a totalitarian regime. Because of your ambivalence to the whole thing, you try not to log in, and you read Facebook status updates through your RSS reader.

That was going pretty well for you until the 15th, at which point Facebook finally switched the RSS status and wall feeds off.

So you build a server to give you your RSS feeds back, using Sinatra, OAuth2 and Haml. It looks like this:

Full source is here: https://github.com/codders/facebook-rss-gen. You need to create your own Facebook app using the Facebook developer tools. You could use the app running on my server, but then I’d be able to log in as you :)

Using Arel without Rails

posted by codders in code, mysql, rails, ruby

Arel is a Relational Algebra for Ruby – a handy way to create SQL queries programmatically without requiring a full ORM. I’ve been doing some work (generating reports) that requires complex queries but between which there’s enough shared functionality to warrant some modularity in the query generation.

Cutting to the chase… Arel requires a database connection. It prefers ActiveRecord (which is probably overkill), but you do at least not have to create all the Model objects normally associated with Rails:

#!/usr/bin/ruby
require 'thread'  # RubyGems/ActiveRecord compatibility fix
require 'rubygems'
require 'active_record'
require 'arel'

@config = # Load some DB config

# Connect to the database
ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :host => @config[:host],
  :username => @config[:username],
  :password => @config[:password],
  :database => @config[:database]
)

# Setup the engine for Arel
Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base)

# Create an Arel table - should match a table in your database
@users = Arel::Table.new(:users)

all of which is easy enough, completely undocumented, and only requires reading the code of most of the library.

Now, though, you can go ahead and write some fun queries:

@gadgets = Arel::Table.new(:gadgets)
query = @users.project(@users[:id].count).
        join(@gadgets).on(@gadgets[:creator_id].eq(@users[:id])).
        project(@gadgets[:name]).
        group(@gadgets[:name]).
        order(@gadgets[:name].asc)

puts query.to_sql

which outputs the very reassuring “SELECT COUNT(`users`.`id`), `gadgets`.`name` FROM `users` INNER JOIN `gadgets` ON `gadgets`.`creator_id` = `users`.`id` GROUP BY `gadgets`.`name` ORDER BY `gadgets`.`name` ASC”. You can even execute it:

puts "Gadget\tUsers"
ActiveRecord::Base.connection.execute(query.to_sql).each_hash do |row|
  puts [ row["name"], row["count"] ].join("\t")
end

Simple as that. The library has some issues, and no documentation, but I’ve been playing with it on my fork over at Github and it seems pretty okay.

Enjoy :)

Javascript Snow performance

posted by codders in code, javascript

So I wrote Canvas snow. Canvas is effectively a pixel buffer, and I decided that the best strategy to render the snow would be to blank the canvas and redraw all the snow (and the ground) every time. Which prompted the question – is there a better way?

(as a recap, the canvas version:
http://prospero.talkingcode.co.uk/snow/snow.html?renderer=canvas)

Two approaches spring to mind that provide ‘sprite‘-like functionality – SVG and CSS.

The SVG implementation (courtesy my brother) creates a separately addressable circle object for each snowflake, and an updatable polygon for the ground:

http://prospero.talkingcode.co.uk/snow/snow.html?renderer=svg

The CSS implementation uses a <div> with rounded corners for each of the flakes, and a number of straight-line single-pixel <div>s to draw the ground.

http://prospero.talkingcode.co.uk/snow/snow.html?renderer=css

… and it turns out (in my unscientific testing) that Canvas wins out. You can test for yourself with the lovely JSPerf:

http://jsperf.com/js-snow/2

Any enhancement suggestions welcome :)

Javascript Snow with Canvas

posted by codders in code, javascript

Seasons greetings!

I was trying to project some snow onto my wall the other day, and realised that it was actually surprisingly difficult to get decent javascript snow that settles at the bottom of the screen – none of the examples I found did the business.

So there you have it. It’s a little bit CPU intensive and a little bit simple in the head, but it basically works. Code is on github (http://github.com/codders/snow) and there’s a bigger demo on prospero.

Any tips on how to make it perform better or look more like snow would be more than welcome.

Merry Christmas :)

Error #2046: The loaded file did not have a valid signature

posted by codders in flash

Ahh, Flash. Flash Flash Flash. You’re at the supermarket, you’re buying chicken breasts for statutory Saturday night curry, and the boss calls to say the website is down. You check the services monitor – no errors. You’ve deployed no changes – it’s a weekend. But sure enough, the flash player on the site doesn’t load.

Often, #2046 is caused by some kind of corruption. Sometimes, deleting your .macromedia folder fixes it (causes a redownload of any corrupt Flash objects). But when everybody reports the error across all platforms… it must be something else.

‘Did not have a valid signature’ isn’t terribly specific, but you might find that setting your system clock back by 24 hours fixes the issue. If this is the case for you, Adobe are bastards. The SDK you are using has expired. It will still generate Flash binaries without reporting an error, but the binaries it creates will mysteriously not work on any system with a correctly-set clock. Download the latest SDK, and it’s business as usual.

Oh. And one day that SDK will expire, of course. And the binaries you’ve already created will need to be rebuilt. You could buy an SDK license, but … well … not for Linux. So I guess you’re trapped.

Thanks, Adobe

Error #2032 IOError in URLLoader.load()

posted by codders in actionscript, flash

Long time no see.

This one cost me a certain amount of time and pain, so I thought I’d share the wealth. We’re in ActionScript3, and we’re looking at mysterious error messages. Every time my application loaded in 64-bit Flash on Linux, I was getting a #2032, but no bad HTTP responses in the Firebug Net Console. Because it was 64-bit, I didn’t have a debug version of the player available, and was unable to get any stack traces. Fortunately, the application uses the very fine SOS Logging Framework, so I was able to do some log-based debugging and track the issue down to a call to URLLoader.load(). Unfortunately, you can’t really get any deeper than that.

The player didn’t like the URL. It generated an IOErrorEvent with no additional explanation. It didn’t try sending it over the network. The URL was well formed and looked similar to all the other URLs I was using. We had had sporadic reports from users, but I was only able to reproduce it on my work desktop (and only the 64-bit Firefox, not the 32-bit chroot).

Long story short, if you’re using an ad blocker like AdBlockPlus and the blacklist matches the URL of your URLRequest, Flash will return you a mysterious Error #2032. In particular, this URL contained the word ‘adverts’ – silly me.

The more you know

iPojoRC – An iPOJO-based OSGi IRC Bot

posted by codders in code, java

I’ve been doing a fair bit of work recently in OSGi land. OSGi is a dynamic module system for Java designed to facilitate the creation, distribution and consumption of modular Java code. The idea is that anybody who has an application running on an OSGi platform implementation (Felix, Equinox) can retrieve your module (optionally stored in an OBR) and have it load in to their running system without upsetting anything. Exactly how well the hot-swapping of bundles works depends heavily on how well the application is written and how well the bundle being loaded / unloaded is written, but it can be made to work.

The Whiteboard Pattern

I’m not quite sure why it’s called a whiteboard pattern, but one of the neat things about the OSGi platform is the service registry. Service consumers and producers interact through the service registry that the platform provides, which takes care (in principle) of service lifecycle and dependency resolution. A correctly written service only becomes available to a correctly written consumer when the service’s dependencies have been met and its initialisation is complete. If the dependencies of any service are no longer satisfied (because of the disappearance of a required bundle), the service is removed from the registry and consumers are notified. Not rocket science, but saves developers a fair bit of effort and makes interoperable components easier to write.

The canonical example of the whiteboard pattern is an IRC bot whose commands are implemented as services. In this example, an IRC Bot is created that maintains its connection to the IRC server while commands are loaded and unloaded from the Bot on the fly. Here, the ServiceTracker is used to listen for the appearance and disappearance of bundles. Neat, but there’s still some associated boilerplate:

  private ServiceTracker serviceTracker;

  public void start(BundleContext context) throws Exception {
    serviceTracker = new ServiceTracker(context, 
                              MyTrackedService.class.getName(), null);
    serviceTracker.open();
    ... startup code...
  }

  public void doStuff() {
    Object[] implementors = serviceTracker.getServices();
    if (implementors != null && implementors.length > 0) {
      for (Object o : implementors) {
        MyService myService = (MyService) o;
        ... do stuff...
      }
    }
  }

  public void stop(BundleContext context) throws Exception {
    serviceTracker.close();
    serviceTracker = null;
    ... shutdown code ...
  }

iPOJO

iPOJO attempts to reduce this boilerplate and make service registration and listening even simpler. Under iPOJO, the boilerplate is reduced to:

  @Requires
  private MyService[] implementors;

plus some hand waving. iPOJO takes care of making sure that the implementors array contains registered implementations of MyService – all your application has to do is iterate over them.

iPojoRC

With a view to having a play around with this stuff, I thought I’d implement the IRC Bot using iPOJO. You can find the fruits of that labour on my github account. It seems to work pretty well. I’ve so far not had a problem adding and removing commands on the fly. It’s even possible (if you’re really careful with versions) to rev parts of the IRCCommand API and have the old and new versions running side by side, but in practice it’s much easier just to mvn clean install.

Why?

Summut to do, innit. More seriously, OSGi seems to be gaining ground as a way of building and distributing Java applications. It plays very nicely with Maven and there are some quite neat bundles available that you can just drop in to your application with very little wiring. That said, one of the challenges of OSGi is dealing with packages and dependencies when you create your own bundles. Most of the tutorials recommend starting with all your code in a single bundle, with good reason – I invariably spend more time sorting out dependency and classloader issues with my bundles than I do writing the code inside them. If you’re going to start a serious OSGi project, it’s good to have experience of bundling simple things and the IRC Bot is a great example of simple code in many bundles.

Mysterious ClassCastException from Scala

posted by codders in scala

So here’s one that’s got me thinking. I think this is a fairly straightforward use case, but maybe I’m doing something odd. I have an application where I’m putting objects in to a hash and I want to be able to pull them out with the “correct” types. Which is to say I want to be able not to have to cast the objects on their way out but rather have their types inferred by their use context.

The following code compiles. There’s a method where I’m putting the type hints in, a method where I’m not, and a method where I’m passing the retrieved hash value directly to an explicitly typed function. In all three cases, at least in principle, the runtime types should match – I’m putting in a String, I expect a String out the other side.

There is, so we’re clear, a runtime cast (call to asInstanceOf), but this is where I’m expecting the developer using my class to be right. And where they’re right, what I don’t expect is a ClassCastException.

import scala.collection.mutable.Map

object Main {
  def printIt(optString: Option[String])
  {
    optString match
    {
      case x: Some[_] => println("Got: " + x.get.substring(0,2))
      case None => println("Got nothing")
    }
  }

  def withInferredType(token: Token)
  {
    println("\n--- With Inferred Type ---")

    val fish = token.get("fish")
    printIt(fish)
  }

  def withoutExplicitType(token: Token)
  {
    println("\n--- Without Explicit Type ---")

    val fish = token.get("fish")
    fish match
    {
      case x: Some[_] => println("Got: " + x.get.substring(0,2))
      case None => println("Got nothing")
    }
  }

  def withExplicitType(token: Token)
  {
    println("\n--- With Explicit Type ---")

    val fish = token.get[String]("fish")
    fish match
    {
      case x: Some[_] => println("Got: " + x.get.substring(0,2))
      case None => println("Got nothing")
    }
  }

  def main(args: Array[String])
  {
    val token = new Token
    token.put("fish", "cat")
    withExplicitType(token)
    withInferredType(token)
    withoutExplicitType(token)
  }
}

class Token()
{
  private var elements = Map[String,Object]()

  def get[A <: Object](key: String): Option[A] =
  {
    for (val value <- elements.get(key))
      return Some(value.asInstanceOf[A])
    return None
  }

  def put[A <: Object](key: String, value: A)
  {
    elements.put(key, value)
  }
}

resulting in:

 scalac Token.scala && scala Main

--- With Explicit Type ---
Got: ca

--- With Inferred Type ---
Got: ca

--- Without Explicit Type ---
java.lang.ClassCastException: java.lang.String cannot be cast to scala.runtime.RichString
        at Main$.withoutExplicitType(Token.scala:29)
        at Main$.main(Token.scala:52)
        at Main.main(Token.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
        at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

In all three cases I’m doing the same thing with the same data. In the ‘Inferred Type’ case, it’s actually doing the thing I want in production. But all three cases compile and one throws a ClassCastException – I had very much hoped that the type system would pick me up on the broken case.

What am I doing wrong, lazyweb? Any comments greatly appreciated.

Considered Harmful

Morning. I’ve had a think about it and as usual with me writing about Scala, it’s my mistake rather than the language’s. What I’d hoped to achieve was something like duck-typing where I could pull something out of the hash and use it as whatever made sense in the code. Static type inference doesn’t work this way though.

In the above code, in the two cases where I was constraining the type, it works fine. In the case where I don’t constrain the type, Scala can legitimately choose any type that implements substring; in this case it chose RichString, which happens not to be the type I put in to the hash. In general, this is a pretty dangerous approach – what I want is for the type system to force me to nominate a type for the result of the get before I use it.

Turns out the way to do this is to have get return Object and use asInstanceOf at the call site. C’est la vie.

A modest copyright reform proposal

posted by codders in rant

Copyright law is a little bit broken. Lawrence thought (thinks, presumably) so, Rufus thinks so, I think so. What’s to be done though? Efforts at legislative reform have, to date, been pretty ineffective. The issues are complex and difficult for laypeople to understand / care about, the opposing lobbyists are a powerful bunch, and it’s hard even to convince artists in whose interest copyright should be reformed of the benefits of doing so. How might we possibly fix it?

I’m going to keep my proposal brief, partly to avoid TL;DR-ism, and partly because it’s probably garbage, but here goes.

Creative Commons over Time

Legislative reform is a tough battle. Let’s just side-step it. Take the existing CC licenses and extend them to allow CC/T; the creator retains all the rights to their work afforded under “default” copyright until a nominated date, after which the work is covered by the nominated CC license. Instead of saying “CC BY SA”, you’d say “CC BY SA / T2015″. Until the expiry date is reached, the creator can exploit the work in whatever way they see fit, just as they can today under the “default” copyright regime.

How does that help?

The idea here (and I confess I’ve not talked to any artists) is that going completely CC is scary for artists whose living is made from their creative output. As fervently as you might believe their work would be better served by a CC license, it’s really tough to persuade people to “give away” their livelihoods. Instead, the pitch goes like this… Their work is going to end up in the public domain; everything created today is implicitly “PD / T2150″ (let’s say). All I’m proposing is that instead of using the default date, they choose a date themselves and a license for use after the date has passed. They might well choose “CC / T2100″, or “CC / T2050″, but it’d be an improvement.

Raising awareness

In and of itself, that won’t achieve anything, but if you can convince artists to take control over the relationship between their work and society, that’s already a small win. The bigger win will require use of technology. Assuming some plucky evangelist can persuade artists to start using these licenses, the next step is to write a webservice to map the (for instance) ASIN for a CD / track / book to an expiry date. That done, you could a) set up you own shop highlighting the expiry date on the items or b) write a GreaseMonkey script to overlay the expiry date on Amazon’s existing shop. You could colour code works – Red for “PD / T2150″, Green if the creator has nominated an expiry date (we needn’t be any more divisive than that to begin with).

Working out the “correct” term

The plan, in case it’s not clear, is to let the market work out the “right” value for copyright term without global multilateral legislative reform. If you can get adoption and if you can raise awareness and if consumers actually care, what you might hope to see is a dutch auction in copyright term lengths. Artists could compete to best serve their fans by varying the amount of time they felt they needed to profit from a work. This won’t necessarily be the optimal value for society (consumers may underestimate the value to them of reduced term), but one would hope we’d end up with a smaller number for copyright term than that selected by legislation.

What about artists’ retirements?

It’s a commonly held misconception that part of copyright’s role is as a pension scheme for aging artists or a life insurance policy for their families. Society affords artists a temporary monopoly on their work so as to encourage them to create more work, on the understanding that the work is donated to the commons after that monopoly expires. It’s easy to find examples of outliers for whom extended term is a key concern, but the majority of artists won’t see continued revenue from their work that is anything like as substantial as the value to society of having all that work in the public domain. If they’re looking for a pension / insurance scheme one might politely suggest that they should invest in a pension / insurance scheme.

Won’t we just end up eating our CC babies?

There is obviously a risk in this that artists who might otherwise choose a pure CC license end up choosing to exploit their works under a CC/T license. Much like GPL vs. LGPL, we’d obviously prefer that CC/T licenses didn’t exist at all lest they tempt people away from the “right” answer. I think it’s a risk worth taking to try and accelerate the copyfight and get people thinking about the relationship between copyrighted work and society.

My new game – Civil Liberties Trolling

posted by codders in rant

I know, I know. You prefer the posts about code. Go read JSR #170, #277, #283, #311, revise the two kinds of JMS and have a butchers at JPA. That’ll keep you occupied while I rant.

So. Cory writes to tell us of an exciting new police initiative (hot-linked here – could change to goatse at any moment):

Police Initiative

Yay. But does taking photos of CCTV cameras make you a terrorist? I’ve decided to find out. To play my new game, you will need:

Now all you have to do is snap pictures of the cameras you find and upload them to Flickr before the police confiscate your camera. Of course, you can delete the photos from your phone the second they’re uploaded. No point leaving them hanging around – at best you’ll look like a weirdo in the unlikely event that the police go rifling through your personal property.

Here’s my photo stream – where’s yours? (Remember to tag your photos ‘cctvfun’).

Aren’t you helping the terrorists?

Let’s get some perspective. You’re still more likely to die in your bath than you are to die in an explosion. If people photographing CCTV cameras represents a significant threat to everyone’s safety, we should make some kind of law against it. At least that way there would be some kind of review of whether or not such a restriction on our freedoms makes sense and is proportionate to the threat. We shouldn’t allow the police to make up their own laws – that way madness lies.

As long as it’s legal to do so (and possibly even after it stops being legal to do so), I would encourage everyone to join in my new game and help redress the balance of surveillance.

Recent Posts
Recent Comments
About Us
jp: works like a charm! thanks!...
Blake: Check this out: http://bugs.adobe.com/jira/browse/SDK-28016...
Boydell: Wow. That was it. You are the only one that had it figured out, and I looked at many...
mark van schaik: thanks! was using a beta SDK version for a production app, which stopped working over...
Sebastian: Steve, I find most asynchronous programming to be incredibly painful. Haskell's appro...

This is the personal blog of a professional software engineer. This site and the views expressed on it are in no way endorsed by the RIAA.