package com.ociweb.oe.foglight.api;

import static com.ociweb.iot.grove.GroveTwig.*;
import com.ociweb.iot.maker.*;
import static com.ociweb.iot.maker.Port.*;

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

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,47 +41,89 @@
 ort 
-static com.ociweb.iot.grove.GroveTwig.*
+java.util.ArrayList;%0Aimport java.util.Random;%0A%0Aimport com.ociweb.iot.maker.FogApp
 ;%0Aim
@@ -148,17 +148,33 @@
 t.maker.
-*
+FogCommandChannel
 ;%0Aimport
@@ -174,23 +174,16 @@
 %0Aimport 
-static 
 com.ociw
@@ -199,68 +199,56 @@
 ker.
-Port.*;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Random
+FogRuntime;%0Aimport com.ociweb.iot.maker.Hardware
 ;%0A%0Ap

remove old imports