/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package reactor.io.net.tcp;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import reactor.Timers;
import reactor.core.error.CancelException;
import reactor.core.subscription.ReactiveSession;
import reactor.io.net.ReactiveNet;
import reactor.io.net.nexus.Nexus;

/**
 * @author Stephane Maldini
 */
public class NexusPlay {

	public static void main(String... args) throws Exception{

		//Nexus nexus2 = ReactiveNet.nexus(12014);
		//nexus2.startAndAwait();

		Nexus nexus = ReactiveNet.nexus();
		nexus.withSystemStats()
		     //.federate("ws://localhost:12014/nexus/stream")
		     .startAndAwait();

//		HttpClient<Buffer, Buffer> client = ReactiveNet.httpClient();
//
//		client
//		           .ws("ws://localhost:12012/nexus/stream")
//				   .subscribe(Subscribers.consumer( ch -> {
//						ch.input().subscribe(Subscribers.consumer(b -> {
//							System.out.println(b);
//						}));
//					}));
//
//		final ReactiveSession<Object> s2 = nexus2.streamCannon();

		final ReactiveSession<Object> s = nexus.streamCannon();
		Timers.create()
		      .schedule(aLong -> {
			      if (!s.isCancelled()) {
				      s.submit(nexus);
				     // s2.submit(nexus2);
			      }
			      else {
				      throw CancelException.get();
			      }
		      }, 200, TimeUnit.MILLISECONDS);

		CountDownLatch latch = new CountDownLatch(1);

		latch.await();
	}
}

@@ -742,16 +742,44 @@
 eUnit;%0A%0A
+import reactor.Subscribers;%0A
 import r
@@ -882,24 +882,57 @@
 iveSession;%0A
+import reactor.io.buffer.Buffer;%0A
 import react
@@ -954,16 +954,55 @@
 iveNet;%0A
+import reactor.io.net.http.HttpClient;%0A
 import r
@@ -1153,18 +1153,16 @@
 ion%7B%0A%0A%09%09
-//
 Nexus ne
@@ -1196,18 +1196,16 @@
 014);%0A%09%09
-//
 nexus2.s
@@ -1291,18 +1291,16 @@
 %0A%09%09     
-//
 .federat
@@ -1364,18 +1364,42 @@
 ait();%0A%0A
+%0A
-//
+%09%09nexus.monitor(nexus);%0A%0A
 %09%09HttpCl
@@ -1454,21 +1454,17 @@
 ient();%0A
-//%0A//
+%0A
 %09%09client
@@ -1464,18 +1464,16 @@
 %09client%0A
-//
 %09%09      
@@ -1518,18 +1518,16 @@
 tream%22)%0A
-//
 %09%09%09%09   .
@@ -1562,26 +1562,24 @@
 er( ch -%3E %7B%0A
-//
 %09%09%09%09%09%09ch.inp
@@ -1621,18 +1621,16 @@
 (b -%3E %7B%0A
-//
 %09%09%09%09%09%09%09S
@@ -1651,18 +1651,16 @@
 tln(b);%0A
-//
 %09%09%09%09%09%09%7D)
@@ -1662,18 +1662,16 @@
 %09%09%09%7D));%0A
-//
 %09%09%09%09%09%7D))
@@ -1676,384 +1676,32 @@
 ));%0A
-//%0A//%09%09final ReactiveSession%3CObject%3E s2 = nexus2.streamCannon();%0A%0A%09%09final ReactiveSession%3CObject%3E s = nexus.streamCannon();%0A%09%09Timers.create()%0A%09%09      .schedule(aLong -%3E %7B%0A%09%09%09      if (!s.isCancelled()) %7B%0A%09%09%09%09      s.submit(nexus);%0A%09%09%09%09     // s2.submit(nexus2);%0A%09%09%09      %7D%0A%09%09%09      else %7B%0A%09%09%09%09      throw CancelException.get();%0A%09%09%09      %7D%0A%09%09      %7D, 200, TimeUnit.MILLISECONDS
+%0A%09%09nexus2.monitor(nexus2
 );%0A%0A

add buffering metrics to inner emitter, procbarrier and rbp