package com.ociweb.oe.foglight.api;

import java.util.ArrayList;
import java.util.Random;

import com.ociweb.iot.maker.FogApp;
import com.ociweb.iot.maker.FogCommandChannel;
import com.ociweb.iot.maker.FogRuntime;
import com.ociweb.iot.maker.Hardware;

public class PubSub implements FogApp
{
	ArrayList<Integer> luckyNums = new ArrayList<>();
	Random rand = new Random();
	
	public static int count = 0;

	
    @Override
    public void declareConnections(Hardware c) {
        
    }


    @Override
    public void declareBehavior(FogRuntime runtime) {

    	final FogCommandChannel channel0 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	
    	runtime.addStartupListener(()->{
    		System.out.println("Your lucky numbers are ...");
    		channel0.publishTopic("Starter", writable->{});
    	});
    	
    	
    	final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	
    	runtime.addPubSubListener((topic, payload)-> {
    		
    		int n = rand.nextInt(101);
    		luckyNums.add(n);
    		
    		channel1.publishTopic("Gen", writable->{});
    		channel1.block(500);

    		return true;
    	}).addSubscription("Print").addSubscription("Starter");
    	
    	
    	final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);
    	
    	runtime.addPubSubListener((topic, payload) -> {
    		System.out.print(luckyNums.get(count) + " ");
    		count++;
    		if(count<7){
    			channel2.publishTopic("Print", writable->{});
    		}
    		
    		return true;	
    	}).addSubscription("Gen");
    }
          
}

@@ -41,369 +41,73 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.Random;%0A%0Aimport com.ociweb.iot.maker.FogApp;%0Aimport com.ociweb.iot.maker.FogCommandChannel;%0Aimport com.ociweb.iot.maker.FogRuntime;%0Aimport com.ociweb.iot.maker.Hardware;%0A%0Apublic class PubSub implements FogApp%0A%7B%0A%09ArrayList%3CInteger%3E luckyNums = new ArrayList%3C%3E();%0A%09Random rand = new Random();%0A%09%0A%09public static int count = 0;%0A
+com.ociweb.iot.maker.*;%0A%0A%0Apublic class PubSub implements FogApp%0A%7B
 %0A%09%0A 
@@ -176,16 +176,43 @@
         
+//no connections are needed
 %0A    %7D%0A%0A
@@ -290,608 +290,136 @@
    %09
-final FogCommandChannel channel0 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09%0A    %09runtime.addStartupListener(()-%3E%7B%0A    %09%09System.out.println(%22Your lucky numbers are ...%22);%0A    %09%09channel0.publishTopic(%22Starter%22, writable-%3E%7B%7D);%0A    %09%7D);%0A    %09%0A    %09%0A    %09final FogCommandChannel channel1 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09%0A    %09runtime.addPubSubListener((topic, payload)-%3E %7B%0A    %09%09%0A    %09%09int n = rand.nextInt(101);%0A    %09%09luckyNums.add(n);%0A    %09%09%0A    %09%09channel1.publishTopic(%22Gen%22, writable-%3E%7B%7D);%0A    %09%09channel1.block(500);%0A%0A    %09%09return true;%0A    %09%7D).addSubscription(%22Pri
+runtime.addStartupListener(new KickoffBehavior(runtime));%0A    %09runtime.addPubSubListener(new GenerateBehavior(runtime, %22Cou
 nt%22)
+)
 .add
@@ -436,15 +436,12 @@
 on(%22
-Starter
+Next
 %22);%0A
@@ -449,365 +449,107 @@
    %09
-%0A    %09%0A    %09final FogCommandChannel channel2 = runtime.newCommandChannel(DYNAMIC_MESSAGING);%0A    %09%0A    %09runtime.addPubSubListener((topic, payload) -%3E %7B%0A    %09%09System.out.print(luckyNums.get(count) + %22 %22);%0A    %09%09count++;%0A    %09%09if(count%3C7)%7B%0A    %09%09%09channel2.publishTopic(%22Print%22, writable-%3E%7B%7D);%0A    %09%09%7D%0A    %09%09%0A    %09%09return true;%09%0A    %09%7D).addSubscription(%22Gen%22);
+runtime.addPubSubListener(new CountBehavior(runtime, %22Next%22)).addSubscription(%22Count%22);%0A    %09%0A    %09
 %0A   

Update PubSub.java