<klip>
   <identity>
      <title>
         API: Engines.TCP
      </title>
   </identity>
   <locations>
      <contentsource>
         http://support.klipfolio.com/files/demo/demo.xml
      </contentsource>
      <icon>
         http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
      </icon>
      <banner>
         http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png   
      </banner>
   </locations>
   <setup>
      <scriptmode>
         extended
      </scriptmode>
   </setup>
   <klipscript>
   <![CDATA[

 //
 // A simple send/receive from a Jabber server to demonstrate the TCP/IP capabilities
 // of Klipfolio Dashboard and parsing XML using regular expressions.
 //
 // Note: To run this script, you must
 //  - turn on developer mode
 //  - add <scriptmode>extended</scriptmode> to the Klip's <setup>
 //
 // <?xml version="1.0" encoding="UTF-8" ?>
 // <stream:stream  to="jabber.org"
 //                 xmlns="jabber:client"
 //                 xmlns:stream="http://etherx.jabber.org/streams">
 // 
 // It will then receive:
 // 
 // <stream:stream from='jabber.org' 
 //                xmlns='jabber:client' 
 //                id='323EB370765371' 
 //                xmlns:stream='http://etherx.jabber.org/streams'>
 //
 
function onRefresh() 
{   
    var tcp = Engines.TCP.newConnection( "jabber.org", 5222 );
    
    _t( "Opened connection to (" + tcp.host + ", " + tcp.port + ")\r\n" );
    if( tcp.open() ) 
    {
      var text = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                   "<stream:stream " +
                   "to=\"jabber.org\" " +
                   "xmlns=\"jabber:client\" " +
                   "xmlns:stream=\"http://etherx.jabber.org/streams\">";
      tcp_send( tcp, text );      
      
      // Look for specific XML in the response
      regEx = new RegExp ( "(<stream:stream [^]*?>)", "i" );
      read_match = tcp_read_match( tcp, regEx ); 
      parse_jabber_header( read_match );
      
      
      // Send a close stream command
      tcp_send( tcp, "</stream>" );      
      
      // Read the last part
      while( tcp.isConnected() ) {
        Klip.delay( 500 );
        _t( "Received: " + tcp.receive( 8192 ));
      }
      
      tcp.close();
    }
    
    // Put one line message to Klip that the user should check the
    // Debug Window
	var xml = "<xml><item><link></link><description>" +
    		  "(See Debug Window for output)" +
    	      "</description></item></xml>";
    	    
    _t( "... onRefresh()" );  
	return Engines.Data.process( xml );
}

function tcp_send( tcp, data )
{
  _t( "Sending: " + data + "\r\n" );
  tcp.send( data, data.length );
}
  
  
function tcp_read_match( tcp, regEx ) {
  var data, match;
  var received = "";
  if( tcp.isConnected() ) {
    while ( (data = tcp.receive( 8192 )) != null ) {

      if( data.length ) {
        received += data;
	
	//
	// Do we have a match for our regular expression
	//
        match = regEx.exec( received ); 
        if( match ) {
           _t( "Matched: " + match + "\r\n" );
           return match[1];
        }
      }
    }
  }
  return null;
}


function parse_jabber_header( xml )
{
	_t( "parse_jabber_header: " + xml );
	//
	// Define a style to extract the attributes we want from the XML
	//
	var style = 
		" stream:stream::attribute(xmlns) { type: scratch; name: 'xmlns'; } " +
		" stream:stream::attribute(from) { type: scratch; name: 'from'; } " +
		" stream:stream::attribute(xmlns:stream) { type: scratch; name: 'stream'; } " +
		" stream:stream::attribute(id) { type: scratch; name: 'id'; } " +
		" stream:stream { type: item; }";
	
	//
	// Using the above style, process the given XML.  For style elements 
	// defined as 'type: scratch', the XML parser will extract the parsed
	// values and make them available via Engines.Data.getScratch().
	//
	Engines.Data.stylesheet = style;
	Engines.Data.process( xml );
	Engines.Data.stylesheet = "@import klip";
	
	_t( "Got xmlns        : " + Engines.Data.getScratch( "xmlns" ));
	_t( "Got from         : " + Engines.Data.getScratch( "from" ));
	_t( "Got stream       : " + Engines.Data.getScratch( "stream" ));
	_t( "Got id           : " + Engines.Data.getScratch( "id" ));
}

//
// Trace code
//
function _t( s ) 
{
   trace( s + "\r\n" );
}


   ]]>
   </klipscript>
</klip>

