package com.ociweb.gl.example;

import com.ociweb.pronghorn.pipe.BlobReader;
import com.ociweb.pronghorn.pipe.BlobWriter;
import com.ociweb.gl.api.*;

public class MassiveBehavior implements GreenApp {

	public static void main(String[] args) {
		GreenRuntime.run(new MassiveBehavior());
	}
	
	@Override
	public void declareConfiguration(Builder builder) {
		builder.setTimerPulseRate(500);//1);//TimeTrigger.OnTheSecond);
	//	builder.enableTelemetry();

	}

	@Override
	public void declareBehavior(GreenRuntime runtime) {
		
		//runtime.addTimePulseListener(new stopperBehavior(runtime));
		
		int i = 20;
		while (--i>=0) {
			final GreenCommandChannel cmd = runtime.newCommandChannel(DYNAMIC_MESSAGING);
			final String topic = "topic"+i;
			final int value = i;
			
			final Writable writable = new Writable() {

				@Override
				public void write(BlobWriter writer) {
					writer.writePackedInt(value);
				}
				
			};
			
			TimeListener pubs = new TimeListener() {

				@Override
				public void timeEvent(long time, int iteration) {
					if (!cmd.publishTopic(topic, writable)) {
						System.out.println("overloaded can not publish "+value);
					}
				}
				
			};
			runtime.addTimePulseListener(pubs);
			
			PubSubListener subs = new PubSubListener() {
				
				public boolean message(CharSequence topic, BlobReader payload) {
					
					
					return true;
				}
			};
	
			runtime
			 .addPubSubListener(subs)
			 .addSubscription(topic)
			 .addSubscription("/testTopic/#");
			 
			
		}
	
	
	}

}

@@ -417,18 +417,16 @@
 cond);%0A%09
-//
 %09builder
@@ -598,10 +598,9 @@
 i = 
-20
+7
 ;%0A%09%09
@@ -1447,45 +1447,8 @@
 pic)
-%0A%09%09%09 .addSubscription(%22/testTopic/#%22)
 ;%0A%09%09

update to simplify run