package com.ociweb.gateway.client;

import com.ociweb.gateway.common.CommonFromFactory;
import com.ociweb.gateway.common.IdGenStage;
import com.ociweb.pronghorn.pipe.Pipe;
import com.ociweb.pronghorn.pipe.PipeConfig;
import com.ociweb.pronghorn.stage.monitor.MonitorConsoleStage;
import com.ociweb.pronghorn.stage.scheduling.GraphManager;

public class ClientAPIFactory {

	public static APIStage clientAPI(APIStageFactory factory) {
		return clientAPI(factory, new GraphManager());
	}
	
	public static APIStage clientAPI(APIStageFactory factory, GraphManager gm) {
		return buildInstance(factory, gm);
	}

	//NOTE: this will be generated from the DOT file in future projects, this is here as an example.
	private static APIStage buildInstance(APIStageFactory factory, GraphManager gm) {
		
	    String rate = factory.getRate();
	    PipeConfig idGenConfig = new PipeConfig(CommonFromFactory.idRangesFROM, factory.getQueuedIdsMaxCount(), 0);		
		PipeConfig connectionInConfig = new PipeConfig(ClientFromFactory.connectionInFROM, factory.getQueuedConInMaxCount(), factory.getMaxTopicPlusPayload());
		PipeConfig connectionOutConfig = new PipeConfig(ClientFromFactory.connectionOutFROM, factory.getQueuedConOutMaxCount(), factory.getMaxTopicPlusPayload());
						
		Pipe unusedIds = new Pipe(idGenConfig);
		Pipe releasedIds = new Pipe(idGenConfig);
		Pipe connectionIn = new Pipe(connectionInConfig);
		Pipe connectionOut = new Pipe(connectionOutConfig);
				

		//these instances are all held by the graph which is passed in	
		GraphManager.addNota(gm, GraphManager.PRODUCER, GraphManager.PRODUCER, new IdGenStage(gm, releasedIds, unusedIds, rate));
		APIStage apiStage = factory.newInstance(gm, unusedIds, connectionOut, connectionIn);
		GraphManager.addNota(gm, GraphManager.PRODUCER, GraphManager.PRODUCER, apiStage);
		new ConnectionStage(gm, connectionIn, connectionOut, releasedIds, rate.length()>2 ? rate.substring(0, rate.length()-2) : "0", 
		                    factory.getInFlightLimit(), factory.getTTLSec(), factory.isSecured(), factory.getPort());
		
		
		//enable monitoring if we have 64mb of memory, //TODO: AAA, do not turn on takes all the memory.
		if (factory.isDebug() && Runtime.getRuntime().freeMemory()>(64<<20)) {
		    
		    //TODO: add telnet server support so we can control and monitor the server remotely
		    //      once complete build http server to serve angularJS pages for a better experiance.
		    
		    //TODO: B, replace with JMX version before release.
			Long defaultMonitorRate = Long.valueOf(50000000);
			PipeConfig defaultMonitorRingConfig = new PipeConfig(CommonFromFactory.monitorFROM, 5, 0);
		    MonitorConsoleStage.attach(gm,defaultMonitorRate,defaultMonitorRingConfig); 
		}
		return apiStage;
	}

	
	
}

@@ -122,24 +122,79 @@
 IdGenStage;%0A
+import com.ociweb.pronghorn.pipe.MessageSchemaDynamic;%0A
 import com.o
@@ -914,32 +914,57 @@
  new PipeConfig(
+new MessageSchemaDynamic(
 CommonFromFactor
@@ -973,24 +973,25 @@
 idRangesFROM
+)
 , factory.ge
@@ -1059,32 +1059,57 @@
  new PipeConfig(
+new MessageSchemaDynamic(
 ClientFromFactor
@@ -1122,24 +1122,25 @@
 ectionInFROM
+)
 , factory.ge
@@ -1248,16 +1248,41 @@
 eConfig(
+new MessageSchemaDynamic(
 ClientFr
@@ -1308,16 +1308,17 @@
 nOutFROM
+)
 , factor
@@ -2733,16 +2733,41 @@
 eConfig(
+new MessageSchemaDynamic(
 CommonFr
@@ -2787,16 +2787,17 @@
 itorFROM
+)
 , 5, 0);

Remove deprecated signature from PipeConfig