<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeUtopia - The blog of Jani Hartikainen</title>
	<atom:link href="http://codeutopia.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeutopia.net/blog</link>
	<description>Software development with a focus on web-related technologies</description>
	<lastBuildDate>Sat, 12 May 2012 12:49:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Is Haskell good for web development?</title>
		<link>http://codeutopia.net/blog/2012/05/12/is-haskell-good-for-web-development/</link>
		<comments>http://codeutopia.net/blog/2012/05/12/is-haskell-good-for-web-development/#comments</comments>
		<pubDate>Sat, 12 May 2012 12:49:21 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Yesod]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=698</guid>
		<description><![CDATA[I&#8217;ve been trying out Haskell for developing a web application, using the Yesod framework. I always found Haskell quite nice, but never tried developing web apps with it. After using Yesod, I&#8217;m slowly starting to think that Haskell might be much better for it than many of the commonly used languages like PHP, Python, Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been trying out Haskell for developing a web application, using the Yesod framework.</p>
<p>I always found Haskell quite nice, but never tried developing web apps with it. After using Yesod, I&#8217;m slowly starting to think that Haskell might be much better for it than many of the commonly used languages like PHP, Python, Ruby or JavaScript.</p>
<p>Here&#8217;s why:</p>
<p><span id="more-698"></span></p>
<h3>Type-safety</h3>
<p>As you may know, Haskell is statically typed. This may sound like an inconvenience, but Haskell&#8217;s type system is very flexible and it has very powerful type-inference &#8211; meaning you don&#8217;t need to specify types manually most of the time &#8211; so in actual usage, it&#8217;s not bad at all.</p>
<p>Yesod expands type-safety to various bits of web programming:</p>
<ul>
<li>Type-safe URLs</li>
<li>Type-safe DB queries</li>
</ul>
<p>In short, these prevent <em>a ton</em> of errors.</p>
<h3>Type-safe URLs</h3>
<p>Yesod has a routing mechanism similar to most web frameworks these days.</p>
<p>You define routes using a simple <a href="http://en.wikipedia.org/wiki/Domain_specific_language">DSL</a>, for example <code>/users/#UserId UserR GET POST</code>. This defines a route which works with GET and POST methods.</p>
<p>Notice the <code>#UserId</code> part. This is a parameter. However, it&#8217;s not just a parameter definition &#8211; <code>#UserId</code> is an actual <em>type</em>.</p>
<p>In your handler function, the parameter will <em>always</em> be a <code>UserId</code>. You could also use <code>#Text</code>, <code>#Int</code> or whatever. The value you get will be cast into the type you want, and if not possible, an error will be presented instead.</p>
<p>Type-safety in URLs also applies directly to generating URLs in code. If you are generating a URL for a page which requires a <code>UserId</code>, it&#8217;s again checked that you have the correct type of parameter for it. Same goes if you change your routes: a very typical scenario is adding parameters to some route, and then having to find all places where it&#8217;s used. Because URL generation uses types in Yesod, you will automatically get errors from the compiler from every place where your URL generation code is incorrect.</p>
<h3>Type-safe DB queries</h3>
<p>Wouldn&#8217;t it be nice if you would get an error if you mistyped something in an SQL query? Or a MongoDB query?</p>
<p>With Yesod, you do. Your application will not compile if your DB queries are incorrect.</p>
<p>In addition to this, Yesod has a persistence layer which is sort of like a light weight data mapper. </p>
<p>The mapper does what you&#8217;d expect: Performing queries returns you nice &#8220;objects&#8221; of data. (You actually define a data type for it, but it&#8217;s kind of like a simple object if you&#8217;re not familiar with Haskell)</p>
<p>Let&#8217;s say that you have a User model and need to query for all users created before a certain date. The type-safety extends to this as well: Assuming you&#8217;ve defined your user&#8217;s create time field as a <code>UTCTime</code>, you can only query this field with variables of the type <code>UTCTime</code>, again saving you from possible errors.</p>
<p>With model IDs it works even better: For example with the <code>UserId</code> mentioned earlier, you can simply pass it to the Yesod persistence layer, and it will always get you a <code>User</code>. You can&#8217;t query for something that isn&#8217;t a <code>User</code> with a <code>UserId</code>.</p>
<h3>Compile time errors</h3>
<p>As Haskell is not a dynamic language, you must compile the application in order to run it. Putting this together with Haskell&#8217;s type system again eliminates a lot of bugs, as many errors you could accidentally make will prevent the code from compiling at all.</p>
<p>This all may sound like a hassle when you can simply reload or restart with dynamic languages, but with Yesod it&#8217;s just a single command to compile, or if you&#8217;re using Yesod&#8217;s development server, it will compile it automatically whenever your files change.</p>
<h3>Compile-time checked configuration and templating</h3>
<p>Some common parts in Yesod use specialized DSLs. These are done using Template Haskell, Haskell&#8217;s metaprogramming facility. Essentially what this allows, is compile-time checking for correctness.</p>
<p>As mentioned, Yesod&#8217;s routing configuration uses a DSL. Yesod also has DSLs for models, HTML, JavaScript and CSS.</p>
<p>Yesod&#8217;s JavaScript and CSS parsers are pretty straightforward: You just get variable interpolation and some other such niceties. However, the HTML DSL, called Hamlet, is a full language of its own. It&#8217;s not wildly different from your standard HTML, so it&#8217;s easy to learn, but it&#8217;s also checked by the compiler for syntax errors and other mistakes &#8211; just as all other Haskell code &#8211; which is great for preventing errors again.</p>
<h3>It&#8217;s Haskell</h3>
<p>Have I mentioned that Haskell is great and fun? <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h3>Conclusion</h3>
<p>I think Yesod and Haskell brings many great ideas to the table for web development. These are just the few things I&#8217;ve found while working with it, and there&#8217;s still much more to it.</p>
<p>Just type-safety in URLs and compile-time checking for all this is a massive time saver when you have a mistake somewhere. Especially with URL generation, I have forgotten to change some code somewhere countless times and then only later stumbled upon it. With Yesod, I never would&#8217;ve had the problem in the first place.</p>
<p>Learning Haskell may be a large effort if you haven&#8217;t used functional languages, but this is just another example showing how much it&#8217;s worth.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/05/12/is-haskell-good-for-web-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>5 ways how PHP is better than Node.js</title>
		<link>http://codeutopia.net/blog/2012/04/24/5-ways-how-php-is-better-than-node-js/</link>
		<comments>http://codeutopia.net/blog/2012/04/24/5-ways-how-php-is-better-than-node-js/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 15:32:57 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=691</guid>
		<description><![CDATA[All hail Node.js! Boo PHP! Except there are various things where PHP is better than Node&#8230; Be sure to read the comments too for some discussion Easier to find hosting Majority of the web&#8217;s hosting providers are able to provide PHP hosting. For Node, you need a much more specialized hosting provider. Typically you need [...]]]></description>
			<content:encoded><![CDATA[<p>All hail Node.js! Boo PHP!</p>
<p>Except there are various things where PHP is better than Node&#8230;</p>
<p><span id="more-691"></span></p>
<p>Be sure to read the comments too for some discussion <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Easier to find hosting</h3>
<p>Majority of the web&#8217;s hosting providers are able to provide PHP hosting.</p>
<p>For Node, you need a much more specialized hosting provider. Typically you need shell access to set up your application, and most web hosting services do not include anything like this, or if they do, the other packages are much cheaper.</p>
<h3>It&#8217;s easier to get started with PHP</h3>
<p>With PHP, you can simply install WAMP, LAMP or MAMP and off you go. For deploying code into a web host, you just drop your files there and you&#8217;re done.</p>
<p>Although Node itself is not hard to install, it does require more specialized knowledge to set it up nicely. In order to install it on a server, you generally need some level of Linux sysadmin knowledge in order to set up the necessary packages, and tools such as process monitoring to keep your node instance running in case of a crash.</p>
<h3>If your PHP code breaks, it doesn&#8217;t bring your whole server down</h3>
<p>As PHP code runs each in its own process and the web server sits in front and directs this, if one of the requests causes an error it will only affect that specific request.</p>
<p>In a Node environment, you typically have a single process serve all the requests. If one request causes an uncaught error, the entire server comes down.</p>
<h3>PHP processes are short lived</h3>
<p>In PHP each process is only alive for the duration of the request. This means you don&#8217;t need to worry as much about resource allocation and memory.</p>
<p>In Node as the process is running for a long time, you need to be careful to manage memory properly. For example, it&#8217;s easy to accidentally take tons of memory if you forget to remove items from a global array. It&#8217;s also easy to accidentally core which leaks memory. (which in turn can bring the whole process down again)</p>
<h3>Bigger standard library</h3>
<p>PHP&#8217;s standard library is much bigger than Node&#8217;s.</p>
<h3>In closing</h3>
<p>This is not to say that PHP is better in every way than Node. It&#8217;s simply to point out that while Node is very good at certain things (real time processing of messages for example), it is not a silver bullet for all problems.</p>
<p>Feel free to leave your thoughts in the comments, or if you can think of more ways where PHP is better than node.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/04/24/5-ways-how-php-is-better-than-node-js/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>How to run Apache and NodeJS based sites on the same server with Varnish</title>
		<link>http://codeutopia.net/blog/2012/04/15/how-to-run-apache-and-nodejs-based-sites-on-the-same-server-with-varnish/</link>
		<comments>http://codeutopia.net/blog/2012/04/15/how-to-run-apache-and-nodejs-based-sites-on-the-same-server-with-varnish/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 17:11:09 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Varnish]]></category>
		<category><![CDATA[Web Applications]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=687</guid>
		<description><![CDATA[While developing Battlefield 3 Web Commander, I wanted to keep it on my VPS where I was already running Apache. Of course I had the option of putting it in a non-standard port like 8080, but it wouldn&#8217;t be nice if the URL would be somedomain.com:8080. The second option was ordering an extra IP address [...]]]></description>
			<content:encoded><![CDATA[<p>While developing <a href="http://www.bf3web.com/">Battlefield 3 Web Commander</a>, I wanted to keep it on my VPS where I was already running Apache.</p>
<p>Of course I had the option of putting it in a non-standard port like 8080, but it wouldn&#8217;t be nice if the URL would be somedomain.com:8080. The second option was ordering an extra IP address from Linode. That would&#8217;ve cost me a bit each month, so I didn&#8217;t really want to do that either.</p>
<p>The third option turned out to be pretty easy: Use <a href="https://www.varnish-cache.org/">Varnish</a>.</p>
<p>Here&#8217;s how I did it&#8230;</p>
<p><span id="more-687"></span></p>
<h3>Step 1: Put your servers on non-80 ports</h3>
<p>First, we need to put Apache on some other port than 80. Simply change the <code>Listen</code> option in Apache&#8217;s config.</p>
<p>Let&#8217;s say Apache goes to port 6001 for this example.</p>
<p>The node application will also need to run on  some other port than 80. Let&#8217;s put it to port 6002.</p>
<h3>Step 2: Configure Varnish on port 80</h3>
<p>First, find your Varnish config file. On Ubuntu, you can probably find it at <code>/etc/default/varnish</code></p>
<p>Find the <code>DAEMON_OPTS</code></p>
<p>Make sure the DAEMON_OPTS uses port 80 in the -a flag as follows:</p>
<pre>
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
</pre>
<p>Or, if you use the advanced configuration, set <code>VARNISH_LISTEN_PORT</code> to 80.</p>
<h3>Step 3: Set up Varnish proxying to Apache and Node</h3>
<p>Open your varnish VCL file, most likely it is <code>/etc/varnish/default.vcl</code></p>
<p>You will need to set up backends for all your servers here. In my case, I will set up backends &#8220;apache&#8221; and &#8220;node&#8221;, as follows:</p>
<pre>
backend apache {
    .host = "127.0.0.1";
    .port = "6001";
}

backend node {
    .host = "127.0.0.1";
    .port = "6002";
}
</pre>
<p>Next, we need to define the <code>vcl_recv</code> sub to route between these backends. This goes after the backend definitions in the VCL file:</p>
<pre>
sub vcl_recv {
    if(req.http.host == "apachesite.com") {
        set req.backend = apache;
    }

    if(req.http.host == "nodesite.com") {
        set req.backend = node;
    }
}
</pre>
<p>All we need to do is check what the host in the request is: If the domain used is apachesite.com, the request will be proxied to Apache, and for nodesite.com, to Node.</p>
<p>If you want to use a single domain, you could use subdomains to check which backend to use as well. In my case, I have an additional subdomain which I use to access my development node instance.</p>
<h3>Step 4</h3>
<p>There is no Step 4.</p>
<h3>Conclusion</h3>
<p>By using Varnish on port 80, we can pretty easily have any number of other servers serving content so that it seems that all of it is on port 80 when it really isn&#8217;t.</p>
<p>This of course is not the only thing Varnish can do. You can set up caching and all sorts of stuff to it, but for that you probably should look at the <a href="https://www.varnish-cache.org/docs/">Varnish manual</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/04/15/how-to-run-apache-and-nodejs-based-sites-on-the-same-server-with-varnish/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Nodejs application architecture: Battlefield 3 Web Commander</title>
		<link>http://codeutopia.net/blog/2012/03/10/nodejs-application-architecture-battlefield-3-web-commander/</link>
		<comments>http://codeutopia.net/blog/2012/03/10/nodejs-application-architecture-battlefield-3-web-commander/#comments</comments>
		<pubDate>Sat, 10 Mar 2012 18:11:38 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=673</guid>
		<description><![CDATA[In this post I&#8217;ll talk a bit about the architecture of my first nodejs application, Battlefield 3 Web Commander. In addition to your usual web app stuff, Web Commander talks to Battlefield 3 game servers in real time and has many features related to that. It also displays data from the servers in real time. [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll talk a bit about the architecture of my first nodejs application, <a href="http://bf3web.com/">Battlefield 3 Web Commander</a>.</p>
<p>In addition to your usual web app stuff, Web Commander talks to Battlefield 3 game servers in real time and has many features related to that. It also displays data from the servers in real time. Because of these, there is a fair bit of complexity to it.</p>
<p><span id="more-673"></span></p>
<h3>Main components used</h3>
<p>As said, the application itself runs on nodejs. The data is stored in MongoDB.</p>
<p>In case of the node process crashing, <a href="https://github.com/nodejitsu/forever">forever</a> is used to automatically restart it.</p>
<p>In front of the nodejs server is a <a href="https://www.varnish-cache.org/">Varnish HTTP accelerator</a>. This was mainly used due to the fact I wanted to run the node app on port 80, where I was also running Apache.</p>
<p>The setup with Varnish is pretty simple: Varnish runs on port 80, and looks at the Host header of each HTTP request. It then routes to Apache, the node production instance or the node testing instance depending on the domain name used. The actual servers sit on other ports, but the user can&#8217;t tell the difference.</p>
<h3>Overview of the application</h3>
<p><img src="http://jjh.fi/webcom_arch.png" alt="" width="600px"></p>
<p>The application is structured between the web interface and the part which talks to the Battlefield 3 servers. Database access is relatively limited: Only the web interface and the StatService (more on that soon) talk to it.</p>
<p>Let&#8217;s talk about the more interesting part first.</p>
<h3>Talking to the Battlefield 3 server</h3>
<p>The objects which handle actions with the gameserver are the Connection and Server objects. Connection implements the low-level protocol used to talk to the Battlefield 3 server. It&#8217;s a binary-protocol, so it&#8217;s implemented using the binary writing/reading methods in <a href="http://nodejs.org/docs/v0.6.9/api/buffers.html">Buffer</a>. It implements methods for sending commands to the server and listening to the low-level events from the server.</p>
<p>The Server object on the other hand implements a higher level interface on top of the Connection &#8211; methods such as kicking and banning players or editing the maplist are callable via a Server instance. In addition, it also handles all the other server-related stuff involved: It keeps track of the maplist, current map, the list of players on the server and other things &#8211; essentially it represents the state of the gameserver in the code. Whenever the state changes in a meaningful way (for example if a player leaves or such), it emits events about it.</p>
<p>Both the Connection and Server are primarily custom code written on top of node builtins, mainly utilizing <a href="http://nodejs.org/docs/v0.6.9/api/buffers.html">Buffer</a> and <a href="http://nodejs.org/docs/v0.6.9/api/events.html">EventEmitter</a>.</p>
<p>To keep track of the state, the Server keeps several lists it updates whenever things happen on the server. In some cases, it also has to query the gameserver from time to time (using setInterval) in order to check if the data has changed, as certain things will not emit any events (such as changing server settings like friendly fire). Other things, like the player list, use both a periodic check and events to stay synced: Whenever players join or leave, the list must be updated, but many player events don&#8217;t include data like the player&#8217;s total score in them, which in turn needs to be polled from the gameserver.</p>
<p>The job of the ServerManager is to keep track of the servers. It does tasks like connecting and disconnecting servers, testing connections to newly added servers, and it also tries to automatically reconnect any servers which get their connection to the gameserver dropped which happens pretty often and would otherwise require the user to manually reconnect.</p>
<p>For reconnecting, it uses a pretty simple approach of &#8220;Wait X seconds and try again.&#8221;. If it can&#8217;t get reconnected, it waits some more and tries again until it works (or reaches the current limit of 100 retries &#8211; The servers are sometimes pretty stubborn).</p>
<h3>Plugin architecture</h3>
<p>To make it easy to provide further features outside the basics of what is implemented by the game protocol itself, the Server object supports plugins. These are basically features the users can enable or disable on a per-server basis, such as automatic team balancing or map voting. Plugins themselves work simply by registering with the server, and listening events from them and sending commands back using the interface provided by the Server object.</p>
<p>Plugins are always server specific. A new instance of a plugin is always created for each server it&#8217;s running on. Sometimes plugins need shared functionality, so for that there are the two services shown in the diagram. These are exposed to the plugins via the constructor and the plugin can grab any service instances it needs from there.</p>
<p>In theory, 3rd parties could create new plugins which can then be used, but as the code is not open-source there is currently no way for anyone else besides myself to create plugins. </p>
<p>Plugins only utilize the interfaces provided by services and server.</p>
<h3>Services</h3>
<p>As one might guess, the purpose of the StatService is to persist server statistics to the database. There is a plugin which when installed on a server, calls methods on the StatService to record all kinds of statistics, such as kills, deaths, score, etc. for all players. The StatService is currently the only component in addition to the web interface which talks to MongoDB.</p>
<p>The IrcService is the more interesting of the current two services. There&#8217;s a plugin available which can send reports from your server to an IRC channel of your choice. Now, we could create a new IRC bot for each server, but IRC servers limit the amount of connections you can do from a single IP address. Thus, we need to share a single IRC bot for this.</p>
<p>The IRC service keeps track of the IRC bot. If a plugin requests a bot on a channel, the service will check if the bot is on the channel, and if necessary, it will connect to the IRC server and join the channel. It also keeps track of the bot so, that if multiple servers require the bot on the same channel, the bot will only leave the channel after it has been disabled for all the servers which requested it.</p>
<p>The IRC service uses <a href="https://github.com/martynsmith/node-irc">node-irc</a> to talk to the IRC server, but I will probably have to replace it with something else since it sometimes makes the entire application unstable if there&#8217;s a connection error. The StatService uses <a href="http://mongoosejs.com/">Mongoose</a> for the MongoDB parts.</p>
<h3>The web frontend</h3>
<p>The web parts of Battlefield 3 Web Commander are built on top of <a href="http://expressjs.com/">Express</a> and use <a href="https://github.com/visionmedia/ejs">ejs</a> for templating.</p>
<p>It&#8217;s split into three main modules: The server, admin and stats modules.</p>
<p>You can probably guess the primary purposes of each module based on their names.</p>
<h3>Server module</h3>
<p>The server module contains the logic involved in displaying the server list and all server-specific pages &#8211; overview, scoreboard, bans, maps and settings. </p>
<p>Many of the server pages update in real time. <a href="http://socket.io/">Socket.IO</a> made a perfect tool for this. Essentially, for each server, there&#8217;s a bunch of event listeners in the server module which listen to some events from the Server object, and then emit some more events using Socket.IO. Users who are connected will then receive those events via Socket.IO, which in turn updates the <a href="http://knockoutjs.com/">KnockoutJS</a> powered View Models on the client-side.</p>
<p>As mentioned, <a href="http://knockoutjs.com/">KnockoutJS</a> is also used on many of the server module&#8217;s pages. I found it perfect for easily creating templates on the client which could be rendered based on data, without having to mess around with adding or removing elements in the DOM. Simply update the Knockout view model and the library handles keeping the DOM in sync.</p>
<p>The server related pages mainly just grab data from the Server instance and displays it to the user. Hidden to most people visiting are the admin tools present on the pages: On all server pages, an admin can perform actions on the things displayed. Admins can kick, ban, kill and chat from the Overview, kick/ban/kill from Scoreboard, add/remove bans from Bans, edit the server list using a snappy drag/drop based interface from the Maps page and change server settings or use presets such as Hardcore or Infantry Only on their server from the Settings page.</p>
<p>Some of the admin functionality use normal forms &#8211; for example, adding a new map simply submits a form &#8211; but others use Socket.IO to send commands to make the interface nicer to use. Reordering maps or removing maps is a good example of this. Since you may often move maps around or delete multiple maps, the tools for that use Socket.IO to make it faster to work with.</p>
<p>The admin tools in the server module only perform actions which directly affect a specific server instance.</p>
<h3>Stats module</h3>
<p>The stats module is perhaps the simplest of all of the web modules: It simply queries the database and displays what was queried. There is no real-time updates on any of the pages (except if you hit reload), and there are no admin tools or such.</p>
<h3>Admin module</h3>
<p>The admin module contains the rest of the admin functionality available: It allows you to add and edit servers and users, and to view entries in the admin audit log and any reports sent by players.</p>
<p>The audit log is simply a list of actions taken by admins. Whenever an admin does something, their action is logged along with any relevant details. This makes it easy to see who did and what later if necessary.</p>
<p>The reporting functionality is provided as an option for each server. A player can send a report by using a command in chat, and Web Commander will save the current state of the server along with the most recent lines of chat into the DB so that admins can take a look at it later. This is so that if there&#8217;s a problem on the server and no admins, it can be later solved based on the report. The IRC bot plugin can also send messages to IRC whenever there&#8217;s a new report.</p>
<p>The server editing in the admin module is the only part of it which uses Socket.IO. When creating a new server, it was easier to display the progress of it in a meaningful and easy to understand way if the page wasn&#8217;t fully reloaded. The connection can fail in a variety of ways and it can take several seconds for it to be established (or fail), so it makes sense to display a dynamic progress indication instead of a full page load.</p>
<p>Another part of the server editing which uses Socket.IO is connecting and disconnecting servers. This was done due to the same reasons as when creating new servers.</p>
<p>The server editing parts of the admin module are related to configuring how Web Commander handles the server, such as the plugins you want and their settings, and not gameserver-specific things like max player number or friendly fire settings, which are configured from the server module&#8217;s Settings page.</p>
<p>The users part of it is quite self-explanatory: You can edit users and choose what access rights they have on each of the servers. Web Commander implements a simple access check solution which supports having a &#8220;super admin&#8221; with access to all servers under an account, &#8220;server admins&#8221; who have full access to a specific server, and finally a more gradual access where you can choose what specific tools a user can use (eg. if they can kick a player or edit the map list).</p>
<p>Otherwise it&#8217;s mostly your standard admin panel type of functionality with some KnockoutJS sprinkled in for ease of use.</p>
<h3>In closing</h3>
<p>Although I hadn&#8217;t done much node stuff before this project besides some simple experiments, I think the choice of tools for this was pretty much perfect.</p>
<p>There have been no major issues working with node, problems mainly rising from the poor documentation of how the Battlefield 3 server protocol works.</p>
<p>The plan with this currently is to go forward into a SaaS model with paid subscriptions. This will bring some additional complexity to this in the form of having to handle payments and tracking if anyone has not paid, and disabling their servers/account if so.</p>
<p>We&#8217;ll see how that goes. Hopefully later this month.</p>
<p>What do you think? Is there anything else you&#8217;d like to hear about this? I can do another post with more details on any specific parts. </p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/03/10/nodejs-application-architecture-battlefield-3-web-commander/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating custom dojox.dtl filters</title>
		<link>http://codeutopia.net/blog/2012/02/15/creating-custom-dojox-dtl-filters/</link>
		<comments>http://codeutopia.net/blog/2012/02/15/creating-custom-dojox-dtl-filters/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 17:46:59 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=667</guid>
		<description><![CDATA[Dojo&#8217;s implementation of the Django Template Language (DTL) is pretty convenient for client-side templating in Dojo applications. However, sometimes you need to customize formatting of values, or add other custom logic to it. Using a filter for this purpose is quite convenient, but Dojo&#8217;s implementation is completely undocumented as to how you would add your [...]]]></description>
			<content:encoded><![CDATA[<p>Dojo&#8217;s implementation of the Django Template Language (DTL) is pretty convenient for client-side templating in Dojo applications.</p>
<p>However, sometimes you need to customize formatting of values, or add other custom logic to it. Using a filter for this purpose is quite convenient, but Dojo&#8217;s implementation is completely undocumented as to how you would add your own.</p>
<p>Turns out it&#8217;s actually quite easy.</p>
<p><span id="more-667"></span></p>
<h3>Step 1: Create a filter</h3>
<p>All DTL filters in dojo follow a convention like below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'my.dtl.filter.examples'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dojo.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>my.<span style="color: #660066;">dtl</span>.<span style="color: #660066;">filter</span>.<span style="color: #660066;">examples</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    exampleFilter<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//do something with value and return the result</span>
        <span style="color: #000066; font-weight: bold;">return</span> value <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Doing this, we have a filter called <code>exampleFilter</code>, which adds 1 to a value passed to it, for example..</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span> someVal<span style="color: #339933;">|</span>exampleFilter <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The output of the template above would be the value of someVal with 1 added to it. </p>
<h3>Step 2: Register your filter</h3>
<p>To register our custom filter(s) with dojo&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojox.<span style="color: #660066;">dtl</span>.<span style="color: #660066;">register</span>.<span style="color: #660066;">filters</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'my.dtl.filter'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> examples<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'exampleFilter'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In other words, you pass the base namespace as the first parameter, and then an object containing the filters.</p>
<p>In the object, the key should be the name of the object containing the filters, like &#8216;examples&#8217; in our case, and the value should be an array with names of filter functions contained in the object in it.</p>
<h3>Step 3: Profit</h3>
<p>You&#8217;re done. Enjoy your shiny new custom dojox.dtl filter!</p>
<p>Adding custom tags works in a similar fashion, however implementing them is a bit more complicated. Refer to the built in tags to find out how to do it. To register tags, simply use <code>dojox.dtl.register.tags</code> instead of <code>dojox.dtl.register.filters</code></p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/02/15/creating-custom-dojox-dtl-filters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why does everything need to be integrated into a framework?</title>
		<link>http://codeutopia.net/blog/2012/01/21/why-does-everything-need-to-be-integrated-into-a-framework/</link>
		<comments>http://codeutopia.net/blog/2012/01/21/why-does-everything-need-to-be-integrated-into-a-framework/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 18:21:23 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=643</guid>
		<description><![CDATA[There is occasionally people asking about things such as &#8220;Is there an integration for X in framework Y&#8221; Then they are disappointed when it isn&#8217;t, acting as if it&#8217;s a really bad thing. But why do things need to be integrated to begin with? Background A lot of people asking for these integrations seem to [...]]]></description>
			<content:encoded><![CDATA[<p>There is occasionally people asking about things such as &#8220;Is there an integration for X in framework Y&#8221;</p>
<p>Then they are disappointed when it isn&#8217;t, acting as if it&#8217;s a really bad thing.</p>
<p>But why do things need to be integrated to begin with?</p>
<p><span id="more-643"></span></p>
<h3>Background</h3>
<p>A lot of people asking for these integrations seem to be from a Ruby on Rails background. It&#8217;s understandable considering there are lots of integrations for all sorts of libraries like unit testing tools and such for RoR.</p>
<p>However, I think it&#8217;s actually not such a great idea to integrate everything.</p>
<p>A very good example about this was quite recently when a person was asking if there&#8217;s an integration of any browser testing tools for Zend Framework. Something like Selenium or such.</p>
<p>As far as I know, there aren&#8217;t any for ZF, but why do you even need one?</p>
<p>This person was a Ruby on Rails developer, and had already learned a browser testing library, which was integrated into RoR itself. Infact, it was integrated so deeply he couldn&#8217;t use it for anything but RoR itself.</p>
<h3>What if there is no integration?</h3>
<p>Now, what if there was no such integration at all for Rails? </p>
<p>He probably would have learned to use Selenium. After learning to use Selenium, he would have been able to use a tool which is <em>completely technology agnostic</em>. Selenium works with Rails, ZF, Django, anything you throw at it. It&#8217;s a much better skill to have than some Rails-specific library.</p>
<p>One could argue that integrated libraries give you a productivity boost. While it may be so, I think the main boost you get is the very first steps: It&#8217;s easier to get started, but after that the benefit fades. In the long run, it may even turn into a poor investment in general, as you could have learned a general purpose tool instead.</p>
<p>So next time you&#8217;re looking for something that integrates into the framework of your choice, consider the other options too. It may prove much more beneficial.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/01/21/why-does-everything-need-to-be-integrated-into-a-framework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Thoughts on CoffeeScript</title>
		<link>http://codeutopia.net/blog/2012/01/09/thoughts-on-coffeescript/</link>
		<comments>http://codeutopia.net/blog/2012/01/09/thoughts-on-coffeescript/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 15:35:17 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=644</guid>
		<description><![CDATA[I initially dismissed CoffeeScript as just another silly attempt at making JavaScript not be JavaScript. It probably also compiled the code into some sort of totally wonky JavaScript, too. However, after thinking about it for a while, I decided to actually try CoffeeScript and see for myself. Now, after using it quite extensively on some [...]]]></description>
			<content:encoded><![CDATA[<p>I initially dismissed CoffeeScript as just another silly attempt at making JavaScript not be JavaScript. It probably also compiled the code into some sort of totally wonky JavaScript, too.</p>
<p>However, after thinking about it for a while, I decided to actually try CoffeeScript and see for myself. Now, after using it quite extensively on some projects, I feel I&#8217;m qualified to give my completely unbiased, 100% accurate and correct opinion on it.</p>
<p><span id="more-644"></span></p>
<h3>How CoffeeScript compares to JavaScript</h3>
<p>CoffeeScript is actually a quite neat language. It uses indentation instead of braces, making it kind of Python-like, and various other niceties, which remind some other languages like Ruby.</p>
<p>Using it feels like you need to write much less code to achieve the same results, especially when working with lots of callbacks or in functional style code (array map, filter, etc.).</p>
<p>I&#8217;m not going to dive deeply into CS syntax here, but consider the following which demonstrates this quite nicely:</p>
<p><code>itemIds = items.map (i) -> i.id</code></p>
<p>vs.</p>
<p><code>var itemIds = items.map(function(i) { return i.id; });</code></p>
<p>Although the CS example above contains some syntax which may look a bit odd at first, it&#8217;s much, much less &#8220;noisy&#8221;, letting you see what&#8217;s happening more easily with a glance.</p>
<p>In addition to just the syntax, it includes many useful language-level features for things like for-each loops, &#8220;splats&#8221; or binding variables from arrays or objects.</p>
<p>And, believe it or not, the compiled JS code it produces is actually quite tolerable and follows good conventions.</p>
<h3>CoffeeScript does not change JavaScript into something else</h3>
<p>Although CoffeeScript works in places to hide some of JavaScript&#8217;s perceived ugliness, it still is not a complete replacement. </p>
<p>It does not even attempt to entirely hide JavaScript from you: To be able to use CoffeeScript, you also need to know certain things from its JavaScript roots.</p>
<p>Two primary examples are closures and the binding of <code>this</code></p>
<h4>Binding of &#8216;this&#8217;</h4>
<p>CoffeeScript provides a convenient way to bind anonymous functions into the current scope, using the <code>=></code> operator when declaring the function.</p>
<p>What this does is essentially the same as doing <code>someFun.bind(this)</code>. (It does not compile to that exact code though).</p>
<p>However, without knowing how JavaScript&#8217;s scope binding works, you would not know when, why or how to use <code>=></code></p>
<h4>Closures</h4>
<p>Everyone who has used JavaScript has probably fallen victim of the closure in a loop problem.</p>
<p>Contrary to what you might expect, this same thing is present in CoffeeScript. </p>
<p>I spent a good 30 minutes debugging some CS code I wrote, only to realize that this was the cause. Imagine my headdesking.</p>
<p>Essentially the fact I was using CS made me completely oblivious to the cause of the problem. If it had been JavaScript, I would&#8217;ve spotted it much sooner.</p>
<h3>So what is the point of CoffeeScript</h3>
<p>To make writing JavaScript more fun!</p>
<p>Considering CoffeeScript lets me write more concise code, provides a bunch of nice features and is overall a quite well thought out language, it does indeed make writing JavaScript more fun to me.</p>
<p>Since it does not attempt to hide JavaScript from me behind some fancy schmancy syntax, it&#8217;s still ever as flexible as JavaScript is, letting you do confusing things with closures and mess with prototypes and whatever.</p>
<p>So it&#8217;s not is a replacement for knowing JavaScript. If you&#8217;re a beginner in JavaScript, I would suggest you learn to be good in JavaScript first, and then you can consider learning CoffeeScript if it suits your purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/01/09/thoughts-on-coffeescript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>2012 &#8211; Mayans beware</title>
		<link>http://codeutopia.net/blog/2012/01/03/2012-mayans-beware/</link>
		<comments>http://codeutopia.net/blog/2012/01/03/2012-mayans-beware/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 16:36:57 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=651</guid>
		<description><![CDATA[As I&#8217;ve been doing before, here is some statistics from the year 2011 and some other random stuff! Blog stats for 2011 Comparing to last year, the visitor stats remained pretty much the same, with 201 499 visits and 267 478 page views. The top five posts were: Handling errors in Zend Framework Zend_Acl part [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve been doing before, here is some statistics from the year 2011 and some other random stuff!</p>
<p><span id="more-651"></span></p>
<h3>Blog stats for 2011</h3>
<p>Comparing to <a href="http://codeutopia.net/blog/2011/01/24/hello-2011/">last year</a>, the visitor stats remained pretty much the same, with 201 499 visits and 267 478 page views.</p>
<p>The top five posts were: </p>
<ol>
<li><a href="http://codeutopia.net/blog/2009/03/02/handling-errors-in-zend-framework/">Handling errors in Zend Framework</a></li>
<li><a href="http://codeutopia.net/blog/2009/02/06/zend_acl-part-1-misconceptions-and-simple-acls/">Zend_Acl part 1</a></li>
<li><a href="http://codeutopia.net/blog/2009/02/18/zend_acl-part-3-creating-and-storing-dynamic-acls/">Zend_Acl part 3</a></li>
<li><a href="http://codeutopia.net/blog/2009/08/21/using-canvas-to-do-bitmap-sprite-animation-in-javascript/">Using canvas to do bitmap sprite animation in JavaScript</a></li>
<li><a href="http://codeutopia.net/blog/2008/08/07/zend_form-decorator-tips/">Zend_Form decorator tips</a></li>
</ol>
<p>Zend Framework content is still the king here it appears. In fact, the Zend_Acl series and the error handling post have been in the top five for <em>three years in the row</em> now! Zend_Form decorator tips was also in the top 5 back in 2009, and while it was not in the top 5 in 2010, it was still in the top 10.</p>
<p>The completely new and surprising comer is the post on bitmap sprites. It was not in the top 10 in the previous years, not close to it at all. So why is it here this year? My guess is that because of the sudden HTML5 boom of late, people are looking more and more into using canvas for their games or animation purposes. </p>
<p>All of the top five posts were (again) older content, written in 2009 except the decorator one which is from 2008. The most popular post I&#8217;ve written in 2011 turned out to be my take on the discussion on <a href="http://codeutopia.net/blog/2011/05/22/you-dont-need-a-service-layer-fat-controllers-are-okay/">fat controllers and models</a>, which ranked on place #28.</p>
<h3>Personal stuff for 2011</h3>
<p>Last year I mentioned starting a new job, well, I&#8217;m still there and probably still going to stay for the foreseeable future.</p>
<p>In December I also launched my nodejs application, <a href="http://www.bf3web.com">Battlefield 3 Web Commander</a>. You can read a bit about it <a href="http://codeutopia.net/blog/2011/12/05/my-latest-project-battlefield-3-web-commander/">here</a>. It&#8217;s probably the biggest personal project I&#8217;ve had for a while. We&#8217;ll see what comes of it in the long run <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As for the blog, I&#8217;m hoping to have some more stuff to post here than last year, but we&#8217;ll see. I do have a few posts planned, but I&#8217;m not sure if I can top my best year of 2009 when I had time to post more than once a week.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2012/01/03/2012-mayans-beware/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to troll a programming language related community</title>
		<link>http://codeutopia.net/blog/2011/12/26/how-to-troll-a-programming-language-related-community/</link>
		<comments>http://codeutopia.net/blog/2011/12/26/how-to-troll-a-programming-language-related-community/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 19:04:02 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=640</guid>
		<description><![CDATA[Here&#8217;s an easy guide you can use to troll any programming language community. IRC channels, forums, whatever. Your success may vary. Prerequisites: Some understanding of programming languages and related concepts. Start by bashing some language. PHP is a pretty easy target and you can pull all sorts of issues with it with a few quick [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an easy guide you can use to troll any programming language community. IRC channels, forums, whatever. Your success may vary.</p>
<p><span id="more-640"></span></p>
<p>Prerequisites: Some understanding of programming languages and related concepts.</p>
<ol>
<li>Start by bashing some language. PHP is a pretty easy target and you can pull all sorts of issues with it with a few quick google searches. Security issues are generally pretty easy for this, or syntax or OOP, or lack/poor implementation of some programming paradigm.</li>
<li>At this point you can counter any arguments by pulling some functional or academic language and claiming if language you chose for #1 was not created by an idiot, it would not have any issues, like the language you chose for this point.</li>
<li>Namedrop Dijkstra or some other well known computer scientist.</li>
<li>Insult the intelligence of someone participating in the discussion. Claim it is not an insult.</li>
</ol>
<p>At this point if the community does not have anyone around who actually knows anything about this stuff, you should be well off to pissing off many people!</p>
<p>Remember to be vague enough so that your actual lack of knowledge in above points is not obvious.</p>
<p><br/><br/><br/><br />
Loosely based on a common pattern I&#8217;ve seen used by trolls on several IRC channels. Enjoy! <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2011/12/26/how-to-troll-a-programming-language-related-community/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My latest project: Battlefield 3 Web Commander</title>
		<link>http://codeutopia.net/blog/2011/12/05/my-latest-project-battlefield-3-web-commander/</link>
		<comments>http://codeutopia.net/blog/2011/12/05/my-latest-project-battlefield-3-web-commander/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 19:10:51 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
				<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/?p=633</guid>
		<description><![CDATA[Some of you have been wondering why I haven&#8217;t been updating. Well, it&#8217;s because I&#8217;m lazy I&#8217;ve been working on something else: Behold, Battlefield 3 Web Commander! It&#8217;s essentially a Battlefield 3 server live tracking / admin tool service. It&#8217;s powered by nodejs and MongoDB, using libraries Express, Socket.io, Mongoose, Knockout, and a few others. [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you have been wondering why I haven&#8217;t been updating. </p>
<p>Well, it&#8217;s because <strike>I&#8217;m lazy</strike> I&#8217;ve been working on something else: </p>
<p>Behold, <a href="http://www.bf3web.com">Battlefield 3 Web Commander</a>!</p>
<p>It&#8217;s essentially a Battlefield 3 server live tracking / admin tool service. It&#8217;s powered by nodejs and MongoDB, using libraries Express, Socket.io, Mongoose, Knockout, and a few others.</p>
<p>Do check it out, let me know what you think, and if you own a Battlefield 3 server, be sure to sign up for the beta <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2011/12/05/my-latest-project-battlefield-3-web-commander/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

