Peer-socket.as

From Organic Design wiki
Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// Establish a connection with Gir (wikid.pl)
// - Gir will spawn a dedicated peerd child in response to a valid connection request
socket = new XMLSocket();
socket.connected = false;
CRLF = String.fromCharCode(13,10,13,10);

// Connect to our host to establish stream
socket.peerConnect = function( domain, port ) {
	if ( socket.connect( domain, port ) )
		echo( 'Connecting to '+domain+':'+port, 'ffffff' );
	else	echo( 'Could not initialise connection!', 'ff0000' );
	};

// When connected, send login info
socket.onConnect = function( success ) {
	if ( this.connected = success ) {
		echo( 'Connected.', '00ff00' );
		// request a dedicated peerd child spawn to handle this stream
		this.request( '/connect/peer/interface' );
		}
	else {
		echo( 'Connection failed!', 'ff0000' );
		echo( 'Will retry soon...', 'ffffff' );
		}
	};

// Decode incoming message
socket.onData = function( data ) {
	echo( 'Message received:', '00ff00' );
	echo( data, 'ffff00' );
	};

socket.onClose = function() {
	this.connected = false;
	echo( 'Connection closed!', 'ff0000' );
	this.ctr = 1;
	echo( 'Will retry soon...', 'ffffff' );
	};

socket.reduce = function() {
	if ( !this.connected && ++this.ctr % 250 == 1 )
		this.peerConnect( 'organicdesign.co.nz', 1729 );
	};

// Send a message to the associated peer
socket.request = function( request ) {
	echo( 'Sending message: "' + request + '"', 'ffffff' );
	_root.socket.send( 'GET ' + request + ' HTTP/1.1' + CRLF );
	};


// needed since socket isn't made with createSymbol()
reduction.push( socket );