From Noisebridge
Jump to navigation Jump to search

Void

Summary[edit]

The new frontier

package  
{
	import flash.display.Sprite;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ServerSocketConnectEvent;
	import flash.events.OutputProgressEvent;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.net.ServerSocket;
	import flash.net.Socket;

	public class Void extends Sprite 
	{		
		private var window:NativeWindow;
		
		private var localhostSocket:ServerSocket;
		// TODO: Update to custom class using vectors
		private var localhostClients:Object;
		
		public function Void() 
		{
			// constructor code
			window = stage.nativeWindow;
			window.stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			
			initLocalhost();
			
			stage.addEventListener(Event.ENTER_FRAME, frameHandler);
		}
		
		private function initLocalhost():void
		{
			localhostClients = new Object();
			
			localhostSocket = new ServerSocket();
			localhostSocket.addEventListener(Event.CONNECT, localhostConnect);
			localhostSocket.addEventListener(Event.CLOSE, localhostClose);
			// localhostSocket.bind(8080, "127.0.0.1");
			// localhostSocket.bind(80, "127.0.0.1");
			localhostSocket.bind(8080, "0.0.0.0");
			localhostSocket.listen();
		}
		
		private function update():void
		{
			
		}
		
		private function frameHandler(e:Event):void
		{
			update();
		}
		
		private function downHandler(e:MouseEvent):void
		{
			window.startMove();
		}
		
		
		/////////////////////////////////////////////////////////////
		/////////////////// localhost server ////////////////////////
		/////////////////////////////////////////////////////////////
		
		private function localhostConnect(e:ServerSocketConnectEvent):void
		{
			var socket:Socket = e.socket as Socket;
			localhostClients[socket.remoteAddress + socket.remotePort] = socket;
			
			socket.addEventListener(ProgressEvent.SOCKET_DATA, localhostSocketData);
			socket.addEventListener(Event.CLOSE, localhostClientClose);
			socket.addEventListener(IOErrorEvent.IO_ERROR, localhostIOError);
			socket.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, localhostProgress);
			
			trace("Connect: " + socket.remoteAddress + ":" + socket.remotePort);
		}
		
		private function closeLocalhostSocket(socket:Socket):void
		{
			trace("Close: " + socket.remoteAddress + ":" + socket.remotePort);
			
			socket.removeEventListener(ProgressEvent.SOCKET_DATA, localhostSocketData);
			socket.removeEventListener(Event.CLOSE, localhostClientClose);
			socket.removeEventListener(IOErrorEvent.IO_ERROR, localhostIOError);
			socket.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, localhostProgress);
			
			localhostClients[socket.remoteAddress + socket.remotePort] = null;
			
			if (socket.connected)
				socket.close();
			
			socket = null;
		}
		
		private function localhostProgress(e:OutputProgressEvent):void
		{
			if (e.bytesPending == 0)
			{
				var socket:Socket = e.target as Socket;
				closeLocalhostSocket(socket);
			}
		}
		
		private function localhostSocketData(e:ProgressEvent):void
		{
			var socket:Socket = e.target as Socket;
			var msg:String = socket.readUTFBytes(socket.bytesAvailable);
			
			trace(socket.remoteAddress + ":" + socket.remotePort + "<" + msg.substr(0, msg.indexOf("\n")));
			
			// check for http GET /
			if (msg.substr(0, 6) == "GET / ")
			{
				socket.writeUTFBytes("<!DOCTYPE html>\n<html>\n<head>\n<title>Hello HTML</title>\n</head>\n<body>\n<p>Hello World!</p>\n</body>\n</html>");
				socket.flush();
			}
			else if (msg.substr(0, 16) == "GET /favicon.ico")
			{
				// Noisebridge icon (w/counterfit header)
				var bytes:Array = [0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 
					0x20, 0x33, 0x30, 0x32, 0x20, 0x46, 0x6f, 0x75, 
					0x6e, 0x64, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65, 
					0x3a, 0x20, 0x54, 0x75, 0x65, 0x2c, 0x20, 0x30, 
					0x39, 0x20, 0x41, 0x70, 0x72, 0x20, 0x32, 0x30, 
					0x31, 0x33, 0x20, 0x30, 0x36, 0x3a, 0x30, 0x30, 
					0x3a, 0x33, 0x38, 0x20, 0x47, 0x4d, 0x54, 0x0d, 
					0x0a, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x3a, 
					0x20, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x2f, 
					0x32, 0x2e, 0x32, 0x2e, 0x32, 0x32, 0x20, 0x28, 
					0x55, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x29, 0x0d, 
					0x0a, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 
					0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 
					0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x6e, 
					0x6f, 0x69, 0x73, 0x65, 0x62, 0x72, 0x69, 0x64, 
					0x67, 0x65, 0x2e, 0x6e, 0x65, 0x74, 0x2f, 0x66, 
					0x61, 0x76, 0x69, 0x63, 0x6f, 0x6e, 0x2e, 0x69, 
					0x63, 0x6f, 0x0d, 0x0a, 0x56, 0x61, 0x72, 0x79, 
					0x3a, 0x20, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 
					0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 
					0x67, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 
					0x6e, 0x74, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 
					0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 
					0x70, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 
					0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67, 0x74, 
					0x68, 0x3a, 0x20, 0x31, 0x39, 0x37, 0x0d, 0x0a, 
					0x4b, 0x65, 0x65, 0x70, 0x2d, 0x41, 0x6c, 0x69, 
					0x76, 0x65, 0x3a, 0x20, 0x74, 0x69, 0x6d, 0x65, 
					0x6f, 0x75, 0x74, 0x3d, 0x35, 0x2c, 0x20, 0x6d, 
					0x61, 0x78, 0x3d, 0x31, 0x30, 0x30, 0x0d, 0x0a, 
					0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 
					0x6f, 0x6e, 0x3a, 0x20, 0x4b, 0x65, 0x65, 0x70, 
					0x2d, 0x41, 0x6c, 0x69, 0x76, 0x65, 0x0d, 0x0a, 
					0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 
					0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 
					0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3b, 
					0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 
					0x3d, 0x69, 0x73, 0x6f, 0x2d, 0x38, 0x38, 0x35, 
					0x39, 0x2d, 0x31, 0x0d, 0x0a, 0x0d, 0x0a, 0x1f, 
					0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
					0x03, 0x2d, 0x8e, 0x3b, 0x0f, 0xc2, 0x30, 0x0c, 
					0x84, 0xf7, 0xfe, 0x0a, 0xd3, 0x9d, 0x98, 0xc7, 
					0x86, 0x4c, 0x06, 0x68, 0x11, 0x48, 0x3c, 0x3a, 
					0x84, 0x81, 0x31, 0x10, 0x43, 0x2a, 0xd1, 0xa4, 
					0x6a, 0xd3, 0x56, 0xfc, 0x7b, 0xda, 0xc2, 0x72, 
					0xb2, 0xef, 0x6c, 0x7d, 0x47, 0x93, 0xe4, 0xb2, 
					0x55, 0xb7, 0x2c, 0x85, 0xbd, 0x3a, 0x1d, 0x21, 
					0xbb, 0x6e, 0x8e, 0x87, 0x2d, 0xc4, 0x53, 0xc4, 
					0x43, 0xaa, 0x76, 0x88, 0x89, 0x4a, 0x7e, 0xc9, 
					0x42, 0xcc, 0x10, 0xd3, 0x73, 0x2c, 0x23, 0xb2, 
					0xa1, 0x78, 0x4b, 0xb2, 0xac, 0x4d, 0xbf, 0x84, 
					0x3c, 0xbc, 0x59, 0x2e, 0x67, 0x0b, 0xd8, 0xf9, 
					0xc6, 0x19, 0xc2, 0x9f, 0x11, 0x11, 0x8e, 0x07, 
					0x74, 0xf7, 0xe6, 0x33, 0xfc, 0xcc, 0xe5, 0x3f, 
					0xef, 0xa7, 0x88, 0x4a, 0xa9, 0x2c, 0x83, 0xf1, 
					0x8f, 0xa6, 0x60, 0x17, 0xc0, 0xea, 0x1a, 0x0a, 
					0xdf, 0xb2, 0x01, 0xd2, 0x60, 0x2b, 0x7e, 0xae, 
					0x63, 0x1b, 0x42, 0x59, 0xaf, 0x10, 0xbb, 0xae, 
					0x13, 0xce, 0xe7, 0x35, 0xdf, 0xab, 0xdc, 0xbc, 
					0x58, 0x38, 0x0e, 0xf8, 0xd4, 0x6d, 0xfe, 0xf0, 
					0x4e, 0xf4, 0x12, 0x4b, 0xcb, 0x15, 0x13, 0x6a, 
					0x29, 0x08, 0xcb, 0x01, 0x3b, 0x02, 0x7b, 0xcc, 
					0x50, 0x32, 0xfa, 0x02, 0x4c, 0x97, 0xdc, 0x16, 
					0xdf, 0x00, 0x00, 0x00];
				for (var i:int = 0; i < bytes.length; i++)
				{
					socket.writeByte(bytes[i]);
				}
				socket.flush();
			}
			else
			{
				closeLocalhostSocket(socket);
			}
		}
		
		private function localhostClientClose(e:Event):void
		{
			var socket:Socket = e.target as Socket;
			closeLocalhostSocket(socket);
		}
		
		private function localhostIOError(e:IOErrorEvent):void
		{
			// TODO: Add error handling and recover
			trace("ERROR: " + e.text);
		}
		
		private function localhostClose(e:Event):void
		{
			// TODO: Add error handling and recover
			trace("localhost Server socket closed.");
		}

	}

}