/**
 * 
 */
package com.trendrr.strest.examples;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.trendrr.oss.DynMap;
import com.trendrr.oss.concurrent.LazyInit;
import com.trendrr.oss.concurrent.Sleep;
import com.trendrr.strest.StrestHttpException;
import com.trendrr.strest.StrestUtil;
import com.trendrr.strest.annotations.Strest;
import com.trendrr.strest.server.ResponseBuilder;
import com.trendrr.strest.server.StrestController;
import com.trendrr.strest.server.connections.StrestConnectionChannel;
import com.trendrr.strest.server.connections.StrestConnectionGroup;


/**
 * @author Dustin Norlander
 * @created Feb 2, 2011
 * 
 */

/*//
 * 
 * @route /firehose
 * @example /firehose
 * 
 * 
 */
@Strest(
		route = "/firehose"
)
public class FirehoseController extends StrestController implements Runnable{

	protected Log log = LogFactory.getLog(FirehoseController.class);

	/**
	 * This is a special lock that only opens the first time
	 */
	static LazyInit lock = new LazyInit();
	static StrestConnectionGroup connections = new StrestConnectionGroup();
	
	/* (non-Javadoc)
	 * @see com.trendrr.strest.server.StrestController#action(com.trendrr.oss.DynMap)
	 */
	@Override
	public void handleGET(DynMap params) throws Exception {
		if (lock.start()) {
			try {
				//lazily start the thread. (once and only once)
				Thread t = new Thread(this);
				t.setDaemon(true);
				t.start();
			} finally {
				lock.end();
			}
		}
		
		if (!StrestUtil.isTxnMulti(this.request)) {
			throw new StrestHttpException(400, "Strest-Txn-Accept must be multi for this action!");
		}		
		connections.addConnection(this.getTxnConnection());
		this.setSendResponse(false); //make sure to not send a default response.
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		//Send the firehose messages to all registered connections.	
		long num = 0;
		while (true) {
			//build the response.
			ResponseBuilder res = new ResponseBuilder();
			try {
				res.txnStatus(StrestUtil.HEADERS.TXN_STATUS_VALUES.CONTINUE); //lets the client know to expect more messages.
				
				//set the content message.
				res.content("text", ("this is message number: " + num++ + "\r\nThere are " + connections.size() + " concurrent connections").getBytes("utf-8"));
			} catch (UnsupportedEncodingException e) {
				//will never happen :)
			}
			//send it to all connections
			connections.sendMessage(res.getResponse());
			Sleep.millis(500);
		}		
	}
}

@@ -92,59 +92,8 @@
 ion;
-%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aim
@@ -528,78 +528,8 @@
 er;%0A
-import com.trendrr.strest.server.connections.StrestConnectionChannel;%0A
 impo
@@ -1989,26 +1989,16 @@
 lder();%0A
-%09%09%09try %7B%0A%09
 %09%09%09res.t
@@ -2009,57 +2009,19 @@
 atus
-(StrestUtil.HEADERS.TXN_STATUS_VALUES.CONTINUE
+Continue(
 );
- 
 //le
@@ -2067,15 +2067,12 @@
 ges.
-%0A%09
 %09%09%09%0A
-%09
 %09%09%09/
@@ -2097,17 +2097,16 @@
 essage.%0A
-%09
 %09%09%09res.c
@@ -2115,17 +2115,12 @@
 tent
-(%22text%22, 
+UTF8
 (%22th
@@ -2223,106 +2223,9 @@
 ns%22)
-.getBytes(%22utf-8%22));%0A%09%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09%09//will never happen :)%0A%09%09%09%7D
+;
 %0A%09%09%09

Update the firehose example