Outputting MySQL results to a CSV file without OUTFILE access

The MySQL client’s -e (or –execute=) flag allows you to specify a query to be run at the command line, which can then be output back to a local file:

$ mysql -h server.com -u username -p -D database -e "SELECT user_id FROM users ORDER BY user_id" > output.tsv

Note that by default this will output a tab-separated value file (TSV), not a comma-separated value (CSV) file.

Points (pt) to pixels (px) conversion chart

Chart courtesy of Suresh Kumar. I don’t believe the relationship to em is correct in all cases, but the rest is quite useful.

Deploying a BlackBerry WebWorks app to the PlayBook from Windows

Back in 2007 I wrote a BlackBerry OS application to perform a simple e-mail routing health check procedure. It turned out to be a hellish process, and not just for the fact that I’m not much of a Java developer (I don’t include Java in my list of skills on my resume). That was a minor problem compared to the overly involved process that had to be undertaken in order to simply compile the damn app and deploy it to a handheld for testing. On top of all that, Research In Motion at that time required that any developer who wished to use certain parts of their API, even just to test on their own device, to register at the cost of $200 USD. My employer paid this fee on my behalf, but it capped off an unpleasant process.

Times have changed and RIM has learned a few lessons. Developers no longer have to pay RIM merely for the pleasure of developing an app, but they do still face an unclear process for deploying to a test device. For both my own sake and for other developers, I’ve attempted to do a comprehensive write up of the process of deploying a BlackBerry WebWorks app to a PlayBook tablet. It consists of ten rather involved steps, but after the first time you do it once the process shrinks down to just the two last steps.

All right – let’s both take a deep breath and get started.

Read More

Creating a patch file for a single commit using Git

One important workflow to attempt to standardize is the method in which an organization rolls out updates to its production webservers. If you’re making heavy use of Git (and GitHub) in your development environment, it only makes sense to bundle your updates into patch files that can safely be applied and rolled back by someone with no working knowledge of the source code.

To create a patch file out of a Git commit, you’ll first want to retrieve the commit ID that your patch will retrieve data from. The git log command below retrieves information regarding the last 3 commits made:

$ git log -3
commit b29d8f2a0e6ad5595330f62f60b978fbc5696bcb
Author: Sully Syed <[email protected]>
Date:   Fri May 13 15:41:05 2011 -0400

    The Feed Importer module will hook into Drupal's cron scheduler script and retrieve new 
    featured articles automatically.

commit 65abfeeea27311effb21a547af99b3427126601f
Author: Sully Syed <[email protected]>
Date:   Fri May 13 15:36:01 2011 -0400

    Add the Google logo image to the footer directory.

commit 33764e5d9ccd0222ad3d38cc1e77eb8242fb120d
Author: Sully Syed <[email protected]>
Date:   Wed May 11 15:45:41 2011 -0400

    Fixed the typo that would have made Skynet sentient.
$ git format-patch --help

The git format-patch command allows us to select a range of commit IDs to bundle into a patch. For our example, the range will consist of begin and end with the same commit ID to limit the patch to the one commit:

$ git format-patch -M -C b29d8f2a0e6ad5595330f62f60b978fbc5696bcb~1..b29d8f2a0e6ad5595330f62f60b978fbc5696bcb
0001-The-Feed-Importer-module-will-hook-into-Drupal-s-cron-.patch

The patch file created takes the name 0001-The-Feed-Importer-module-will-hook-into-the-Drupal-s-cron-.patch, which can then be e-mailed or sent by other means to the production staff who will apply it.

Reference: Blogging the Monkey: git format-patch for specific commit-ids

Fix: My DD-WRT router drops all connections when it renews a DHCP lease

I’m one of a surprisingly large amount of people that have loaded third-party firmware onto their wireless network router at home. One long standing flaw of the DD-WRT flavour of firmware has been a pair of issues that lead to a momentary loss of connectivity when one’s DHCP server provided IP lease requires renewal.

There are three causes of this issue, and most people will need to address both to solve their connectivity problems:

  1. A popular and recommended version of the DD-WRT firmware – v24-sp2 (10/10/09) micro – build 13064, to be specific – has a flaw in it that leads to a dump of the UPnP mapping list when a DHCP lease renewal ACK is received.
  2. The SPI Firewall, if enabled, blocks DHCP renewal responses by default.
  3. If the DMZ is enabled, DHCP renewals are mistakenly sent there instead of to the router.

Let’s address these issues in order. First up: The firmware.

While the official DD-WRT website lists the 2009-10-10 firmware as its recommendation for my Linksys WRT54G v5 router, the forum dedicated to Linksys (Broadcom) routers surprisingly lists this as a build to explicitly avoid. Their alternative solution: Build 14929. (Make sure to take a quick glance at the upgrade procedure before attempting the update.)

Once you’ve logged back into the interface of your freshly flashed router (you should now be running v24-sp2 (08/12/10) micro – build 14929), we can tackle the issue number two. To allow the DHCP renewal messages to be received by your router, you have one of two options: You can disable the SPI Firewall feature completely (Security > Firewall > SPI Firewall), or you can add a rule to specifically allow those messages. Do this by navigating within your router’s interface to Administration > Commands, and entering the following into the Commands fields:

iptables -I INPUT -p udp --sport 67 --dport 68 -j ACCEPT

Press the Save Firewall button to save the rule to be executed whenever the router is restarted.

Finally, you’ll need to disable the DMZ option in DD-WRT by going to NAT / QoS > Demilitarized Zone (DMZ) > Use DMZ and setting it to Disable.

For me, the combination of these three items led to my first uneventful DHCP lease renewal in months. Some of the members of the DD-WRT forums have reported that the second issue was only solved by completely disabling their SPI firewall, so give that a try if the preferred option of adding a rule doesn’t work out.

References:

How do video games solve the “two people shoot at the same time” dilemma?

I’ve always wondered how multiplayer video games – that is, those involving players distributed across a network – decide upon a winner in this typical situation: Player A and Player B fire one-shot-kills bullets at each other at nearly the same time (Player A’s fires 1/1000th of a second first). Being that each player is on their own machine across a network and that the time on their machines is not necessarily correct (a bit fast or a bit slow), the game server cannot simply trust the received data. How are these life or death decisions made?

I posed the question to Quora and only received unhelpful answers. Elsewhere, however, I was provided two good sources that each answered the question. First, from The Valve Developer Community – Source Multiplayer Networking page:

Multiplayer games based on the Source Engine use a Client-Server networking architecture. Usually a server is a dedicated host that runs the game and is authoritative about world simulation, game rules, and player input processing. A client is a player’s computer connected to a game server. The client and server communicate with each other by sending small data packets at a high frequency (usually 20 to 30 packets per second).

A client receives the current world state from the server and generates video and audio output based on these updates. The client also samples data from input devices (keyboard, mouse, microphone, etc.) and sends these input samples back to the server for further processing. Clients only communicate with the game server and not between each other (like in a peer-to-peer application). In contrast with a single player game, a multiplayer game has to deal with a variety of new problems caused by packet-based communication.

Basic Networking

The server simulates the game in discrete time steps called ticks. By default, the timestep is 15ms, so 66.666… ticks per second are simulated, but mods can specify their own tickrate. During each tick, the server processes incoming user commands, runs a physical simulation step, checks the game rules, and updates all object states. If the game is large or the servers busy dealing with a roblox hack, this may interfere with gameplay.

After simulating a tick, the server decides if any client needs a world update and takes a snapshot of the current world state if necessary. A higher tickrate increases the simulation precision, but also requires more CPU power and available bandwidth on both server and client.

Lag Compensation

Let’s say a player shoots at a target at client time 10.5. The firing information is packed into a user command and sent to the server. While the packet is on its way through the network, the server continues to simulate the world, and the target might have moved to a different position. The user command arrives at server time 10.6 and the server wouldn’t detect the hit, even though the player has aimed exactly at the target. This error is corrected by the server-side lag compensation)

The lag compensation system keeps a history of all recent player positions for one second. If a user command is executed, the server estimates at what time the command was created as follows:

Command Execution Time = Current Server Time – Packet Round-Trip-Time – Client View Interpolation

Then the server moves all other players – only players – back to where they were at the command execution time. The user command is executed and the hit is detected correctly. After the user command has been processed, the players revert to their original positions.

Client and server hitboxes don’t exactly match because of small precision errors in time measurement. Even a small difference of a few milliseconds can cause an error of several inches for fast-moving objects. Multiplayer hit detection is not pixel perfect and has known precision limitations based on the tickrate and the speed of moving objects. Increasing the tickrate does improve the precision of hit detection, but also requires more CPU, memory, and bandwidth capacity for server and clients.

The question arises, why is hit detection so complicated on the server? Doing the back tracking of player positions and dealing with precision errors while hit detection could be done client-side way easier and with pixel precision. The client would just tell the server with a “hit” message what player has been hit and where.

We can’t allow that simply because a game server can’t trust the clients on such important decisions. Even if the client is “clean” and protected by Valve Anti-Cheat, the packets could be still modified on a 3rd machine while routed to the game server. These “cheat proxies” could inject “hit” messages into the network packet without being detected by VAC (a “man-in-the-middle” attack).

Another great article on the subject is Gamasutra – 1500 Archers on a 28.8: Network Programming in Age of Empires and Beyond, which talks about the same subject from the perspective of a RTS game and in the age of dial up.

The best fixed-width programming font for Windows OSes: Inconsolata

As I round into the last year of a solid decade of being a professional software developer, I’m becoming more and more aware of how important it is to make your working environment as comfortable as possible. This extends beyond your keyboard, mouse and chair into the realm of software: Is your development environment making you log more keystrokes and mouse movements than needed? Are you taking it easy on your eyes by providing a high-contrast interface with a decent font size?

That last question, spurred mostly by my transition to a MacBook Pro as my development machine, had me settle on Menlo. Back on my Windows machines at home, after sifting through a number of options mentioned here by Jeff Atwood I decided on Inconsolata. Tracking down a TrueType (TTF) version took a few more minutes but eventually led to success – I’ve mirrored the file I found locally here for others to download – enjoy.

Inconsolata TrueType (TTF) Font

Study: All net job growth in the U.S. since 1977 has come from startups

There was an interesting piece in the HBR blogs a few days ago that was written in response to President Obama’s State of the Union speech – specifically, the part about job creation. The author takes note of how job creation in since 1977 has been entirely due to startups, but that the President’s go-to crew for ideas on how to foster growth consist of all the wrong people for the job.

Harvard Business Review: Looking for Jobs in All the Wrong Places: Memo to the President

According to a recent study by the Kauffman Foundation, for example, all net job growth in the U.S. since 1977 has been due to start-ups. The data show that if you took start-ups out of the picture and looked only at large established firms, job growth in the U.S. over the last 34 years would actually be negative.

“When it comes to U.S. job growth,” said Kauffman Foundation economist Tim Kane in his report, “start-up companies aren’t everything. They’re the only thing.”

In your address last night, Mr. President, you correctly noted that, “The first step in winning the future is encouraging American innovation.” Here, too, start-ups are the driving engine of our nation’s global innovation leadership.

It is startups who have generated virtually all of our nation’s major technological breakthroughs in the last hundred years — from cars and planes to semiconductors, PCs, software, and the Internet — and in the process sparked the creation of whole new industries and millions of new jobs. And as economists have demonstrated, this kind of start-up-led innovation is the source of virtually all economic growth and increases in living standards in the U.S.

In other words, Mr. President, everything depends upon start-ups: Job creation. Our standard of living. Our prosperity as a nation. The American Dream itself.

So if the target of national policy is job creation, then the bullseye of that policy must be centered on startups. Yet policy makers in both parties continue to aim at the wrong target.

Last month, Mr. President, you held a summit meeting with 20 of the nation’s top CEOs to look for ways to spur job creation. But Fortune 100 CEOs are exactly the wrong people to talk to about jobs. Big Business is not a major job creator. Indeed, as one commentator put it, the guest list at this summit meeting represented “a who’s who of outsourcing American jobs.”

The best programmers

The best programmers are not marginally better than merely good ones. They are an order-of-magnitude better, measured by whatever standard: conceptual creativity, speed, ingenuity of design, or problem-solving ability.

— Randall E. Stross

Validating credit card numbers in code using the Lunh algorithm

I’ve been writing billing system code for years, but this is something I’ve not known until today: VISA, MasterCard and American Express credit card numbers can be checked for validity (meaning you gain one extra check against someone typing in random numbers) using what’s known as the Luhn algorithm.

Read More