« My Top 10 Spam Subjects from 2009 | Main | The Music of Chance »

AIR 2.0 and Presentation Nightmares

Tonight I spoke at the Boston Flex User Group about AIR 2.0 with a focus on the new Native Process API and Networking APIs. Alas the talk didn't go as planned since I forgot my bag of video output adapters at home and didn't notice until I was setting up. The meeting was only a few blocks away from the Allurent offices so I ran (more like a sprint) back to find suitable replacements. Alas in my rush to borrow an adapter (only being able to find a mini display port to HDMI) I ran afoul of the old Mac VGA/HDMI mechanical virus issue. However, it wasn't until after sprinting back to the meeting and futzing with projectors for 15 minutes that I realized this issue. Thankfully a fellow attendee loaned me his computer and using the USB drive I did have on me, I managed to gave a modified version of the talk sans running demo code. I'd like to thank those in attendance for their patience, apologize for my forgetfulness, and hope that at least a little information was conveyed. A copy of the presentation is available in PDF format. The most import code snippets are below:

Define a Native Process Startup Information instance:

// _mxmlc, _cssFile, _brandFile, and _kitDir are of type File
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = _mxmlc;
var mxmlcArguments:Vector.<String> = new Vector.<String>();
nativeProcessStartupInfo.arguments = mxmlcArguments;
nativeProcessStartupInfo.workingDirectory = _kitDir;

Create and attach listeners to a Native Process:

// _nativeProcess is of type NativeProcess
_nativeProcess = new NativeProcess();

_nativeProcess.addEventListener(Event.STANDARD_ERROR_CLOSE, handleStandardErrorClose);
_nativeProcess.addEventListener(Event.STANDARD_INPUT_CLOSE, handleStandardInputClose);
_nativeProcess.addEventListener(Event.STANDARD_OUTPUT_CLOSE, handleStandardOutputClose);

_nativeProcess.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, handleStandardErrorError);
_nativeProcess.addEventListener(IOErrorEvent.STANDARD_INPUT_IO_ERROR, handleStandardInputError);
_nativeProcess.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, handleStandardOutputError);

_nativeProcess.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, handleStandardErrorData);
_nativeProcess.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, handleStandardInputProgress);
_nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, handleStandardOutputData);

_nativeProcess.addEventListener(NativeProcessExitEvent.EXIT, handleNativeProcessExit);

Typical event handling code:

private function handleStandardOutputData(progressEvent:ProgressEvent):void {

private function handleNativeProcessExit(nativeProcessExitEvent:NativeProcessExitEvent):void {

Create a new server socket that listens on an OS selected available port:

// _serverSocket is of type ServerSocket
_serverSocket = new ServerSocket();
_serverSocket.addEventListener(Event.CLOSE, handleServerClose);
_serverSocket.addEventListener(Event.CONNECT, handleServerConnect);

Handle a new incoming connection:

private function handleServerConnect(serverSocketConnectEvent:ServerSocketConnectEvent):void {
    var socket:Socket = serverSocketConnectEvent.socket;
    socket.addEventListener(ProgressEvent.SOCKET_DATA, handleSocketData);
    socket.addEventListener(Event.CLOSE, handleSocketClose);
    socket.addEventListener(IOErrorEvent.IO_ERROR, handleSocketIOError);

Handling socket data:

private function handleSocketData(progressEvent:ProgressEvent):void {
    var socket:Socket = Socket(progressEvent.target);
    var byteArray:ByteArray = new ByteArray();
    socket.readBytes(byteArray, 0, socket.bytesAvailable);

    socket.writeUTFBytes("HTTP/1.1 404 Not Found" + CRLF);
    socket.writeUTFBytes("Connection: close" + CRLF);

Tags: air flex life