<%@ language="javascript"%><% /* -*- java -*- */ %> <% /* Copyright (c) 2005 Tony Garnock-Jones Copyright (c) 2005 LShift Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /////////////////////////////////////////////////////////////////////////// // A simple AJAJ application implementing a basic chatroom. // // Makes use of JSONDB (for storing the chatroom database) and AJAJ // (for responding to client RPC requests). /////////////////////////////////////////////////////////////////////////// function padZeros(s, w) { for (var n = w - s.length; n > 0; n--) { s = '0' + s; } return s; } function timeStamp() { var d = new Date(); return (["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"])[d.getDay()] + " " + padZeros('' + d.getHours(), 2) + ":" + padZeros('' + d.getMinutes(), 2) + ":" + padZeros('' + d.getSeconds(), 2); } // We call AJAJ.respond to handle the grunge-work of deserializing the // JSON request object and serializing the response we build in our // handler function for transmission to the client. // // We use JSONDB to process each message from the client in a separate // transaction, possibly updating the database as we run. // AJAJ.respond(function (params) { return JSONDB.transaction ("chatroom", // This is the name of the file in which to store the database // This is the "creator" function, called if the database is absent: function () { return { counter: 0, sofar: [] }; }, // This is the main driver. At this point, we have "params" // containing the object that the client sent us as part of the // HTTP request, and "chatRoom" containing the object read in // from the database on disk. // function (chatRoom) { var action = params.action; var arg = params.arg; // Here we dispatch on the message name that was sent to // us. We'd like to use metaprogramming to automate this and // make it feel more like normal Javascript programming; // however, Javascript doesn't expose the minimum necessary // hooks, so we're forced into manual intepretation and // dispatch of each message. switch (action) { // To "say" something is to append a message to the end // of the DB, not forgetting to update the counter. We // also do some housekeeping to keep the DB to just the // last 100 messages. case "say": { arg.time = timeStamp(); chatRoom.sofar.push([chatRoom.counter++, arg]); while (chatRoom.sofar.length > 100) { chatRoom.sofar.shift(); } break; } // A "reset" command does exactly that - empty the DB, // and set the counter to 0. case "reset": { chatRoom.counter = 0; chatRoom.sofar = []; break; } // "getUpdates" returns all the "new messages". "New" // is determined by comparing the caller's // (i.e. client's) counter against that of the // database. case "getUpdates": { var result = []; for (var i = chatRoom.sofar.length - 1; i >= 0; i--) { var entry = chatRoom.sofar[i]; if (entry[0] < arg) break; result.unshift(entry[1]); } return {counter: chatRoom.counter, updates: result}; } // Messages we don't understand cause an exception to // be thrown back to the client. default: { AJAJ.die("Unknown action", {name: "SimpleAJAJUnknownAction", params: params}); } } return true; }); }); %>