// ******************************************************************************** // oop interface for full spacebox socket control // see spacebox.fla for instructions // autor: roman kallweit, romanka@student.ethz.ch // last changed: 04.11.2004 // ******************************************************************************** // Utility : Add String-Replace Functionality to Actionscript // ******************************************************************************** String.prototype.searchReplace = function(find,replace) { return this.split( find ).join( replace); } // SpaceboxXMLSocket Class // ******************************************************************************** function SpaceboxXMLSocket() { // Setup // ******************************************************************************** this.returnField = "\n " this.connected = false; this.socket = new XMLSocket(); this.socket.myServer = "styropor.ethz.ch"; // "localhost"; // this.socket.myPort = 4445; // 4444; // // Public Methods // ******************************************************************************** // Set Level // @param string id {NR1..NR5,LS1..LS10,WINDOW} // @param int level 0<100 this.setLevel = function (id, level) { var command = this.setLevelXML.searchReplace("{id}", id); command = command.searchReplace("{value}", level); command = this.RequestXML.searchReplace("{request}", command); this.socket.send( command ); } // Set multiple Levels at once // @param array key:id, value:value this.setLevels = function (items) { var command = ""; for (item in items) { var c = this.setLevelXML.searchReplace("{id}", item); c = c.searchReplace("{value}", items[item]); command = command + c; } command = this.RequestXML.searchReplace("{request}", command); this.socket.send( command ); } // Get Status // @param string id this.getStatus = function (id) { var command = ""; if (id instanceof Array) { for (item in id) { var c = this.getStatusXML.searchReplace("{id}", item); command = command + c; } } else { var command = this.getStatusXML.searchReplace("{id}", id); } command = this.RequestXML.searchReplace("{request}", command); this.socket.send( command ); } // Send Message this.sendMessage = function(type, from, to, message) { var command = this.MessageXML; command = command.searchReplace("{type}", type); command = command.searchReplace("{from}", from); command = command.searchReplace("{to}", to); command = command.searchReplace("{value}", message); this.socket.send( command ); } // xml-command-templates // ******************************************************************************** this.RequestXML = "{request}"; this.getStatusXML = "{id}status"; this.SetLevelXML = "{id}setLevel{value}"; this.MessageXML = "{from}{to}{type}{value}"; // Socket Connection // ******************************************************************************** this.connect = function() { this.socket.onConnect = spacebox.Socket_onConnect; this.socket.onClose = spacebox.Socket_onClose; // this.socket.onXML = spacebox.Socket_onXML; this.socket.returnField = this.returnField; this.socket.connect(this.socket.myServer, this.socket.myPort); } this.Socket_onConnect = function (success) { if (success) { msg = "Connected to "+ this.myServer +" on port "+ this.myPort; this.connected = true; } else { msg = "There has been an error connecting to "+ this.myServer +" on port "+ this.myPort; } trace("Spacebox >> " +msg); } this.Socket_onClose = function () { msg = "Lost connection to "+ this.myServer +" on port "+ this.myPort; this.connected = false; trace("Spacebox >> "+ msg); } // Socket-Object Extensions : Response Parsing // ******************************************************************************** this.socket.onXML = function (doc) { // validate response var e = doc.firstChild; if (e.nodeName == null) e = e.nextSibling; if (e == null) return; // switch spacebox / message switch( e.nodeName ) { case "spacebox": node = e.firstChild; switch( node.nodeName ) { case "response" : trace("Spacebox >> Status Report:"); // loop through messages node = node.firstChild; do { if( node.nodeName == "message" || node.nodeName == "rcbox" ) { var response = this.processResponse( node ); trace("Spacebox >> _ Value of "+ response["id"] +" is "+ response["value"] ); // call Response-Handler this.responseHandler( response ); } } while( node = node.nextSibling ); break; case "message": // process message var msg = this.processMessage( node ); trace("Spacebox >> Message ("+ msg["type"] +") to "+ msg["to"] +" from "+ msg["from"] +":"); trace("Spacebox >> _ "+ msg["value"]); // call MessageHandler this.messageHandler(msg); break; } break; } } this.socket.processMessage = function (node) { // validate if ( !node.hasChildNodes() ) return; var msg = new Array(); node = node.firstChild; // loop through attributes do { switch ( node.nodeName) { case "type" : msg["type"] = node.firstChild.toString(); break; case "from" : msg["from"] = node.firstChild.toString(); break; case "to" : msg["to"] = node.firstChild.toString(); break; case "value" : msg["value"] = node.firstChild.valueOf(); break; } } while ( node = node.nextSibling ); // return message-array return msg; } this.socket.processResponse = function (node) { // validate if ( !node.hasChildNodes() ) return; var msg = new Array(); // loop through attributes node = node.firstChild; do { switch ( node.nodeName) { case "id" : msg["id"] = node.firstChild.toString(); break; case "value" : msg["value"] = node.firstChild.valueOf(); break; } } while( node = node.nextSibling ); // return message-array return msg; } }