/*
 * Copyright (c) 2012-2013 Spotify AB
 *
 * 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 com.spotify.netty4.handler.codec.zmtp;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;

import static com.spotify.netty4.handler.codec.zmtp.ZMTPUtils.checkNotNull;

/**
 * Represents a single ZMTP session.
 */
public class ZMTPSession {

  private final AtomicReference<ZMTPHandshake> handshake = new AtomicReference<ZMTPHandshake>();

  private final ZMTPConfig config;

  ZMTPSession(final ZMTPConfig config) {
    this.config = checkNotNull(config, "config");
  }

  /**
   * The the configuration of this ZMTP session.
   */
  public ZMTPConfig config() {
    return config;
  }

  /**
   * Get the remote session id (can be used for persistent queuing)
   */
  public ByteBuffer remoteIdentity() {
    final ZMTPHandshake handshake = this.handshake.get();
    if (handshake == null) {
      throw new IllegalStateException("handshake not complete");
    }
    return handshake.remoteIdentity();
  }

  /**
   * Signal ZMTP handshake completion.
   */
  void handshakeDone(final ZMTPHandshake handshake) {
    if (!this.handshake.compareAndSet(null, handshake)) {
      throw new IllegalStateException("handshake result already set");
    }
  }

  /**
   * The ZMTP framing version negotiated as part of the handshake on connection establishment.
   */
  public ZMTPVersion negotiatedVersion() {
    final ZMTPHandshake handshake = this.handshake.get();
    if (handshake == null) {
      throw new IllegalStateException("handshake not complete");
    }
    return handshake.negotiatedVersion();
  }

  @Override
  public String toString() {
    return "ZMTPSession{" +
           "config=" + config +
           ", handshake=" + handshake +
           '}';
  }
}

@@ -669,27 +669,32 @@
 Buffer;%0A
+%0A
 import 
-java
+io.netty
 .util.co
@@ -706,30 +706,75 @@
 ent.
-atomic.AtomicReference
+DefaultPromise;%0Aimport io.netty.util.concurrent.GlobalEventExecutor
 ;%0A%0Ai
@@ -938,30 +938,29 @@
 e final 
-AtomicReferenc
+DefaultPromis
 e%3CZMTPHa
@@ -988,22 +988,21 @@
 new 
-AtomicReferenc
+DefaultPromis
 e%3CZM
@@ -1014,16 +1014,51 @@
 dshake%3E(
+%0A      GlobalEventExecutor.INSTANCE
 );%0A%0A  pr
@@ -1428,161 +1428,199 @@
     
-final ZMTPHandshake handshake = this.handshake.get();%0A    if (handshake == null) %7B%0A      throw new IllegalStateException(%22handshake not complete%22);%0A    %7D
+return handshake().remoteIdentity();%0A  %7D%0A%0A  /**%0A   * The ZMTP framing version negotiated as part of the handshake on connection establishment.%0A   */%0A  public ZMTPVersion negotiatedVersion() %7B
 %0A   
@@ -1636,31 +1636,36 @@
 andshake
-.remoteIdentity
+().negotiatedVersion
 ();%0A  %7D%0A
@@ -1778,13 +1778,8 @@
     
-if (!
 this
@@ -1793,273 +1793,175 @@
 ake.
-compareAndSet(null, handshake)) %7B%0A      throw new IllegalStateException(%22handshake result already set%22);%0A    %7D%0A  %7D%0A%0A  /**%0A   * The ZMTP framing version negotiated as part of the handshake on connection establishment.%0A   */%0A  public ZMTPVersion negotiatedVersion() %7B
+setSuccess(handshake);%0A  %7D%0A%0A  private ZMTPHandshake handshake() %7B%0A    if (!handshake.isDone()) %7B%0A      throw new IllegalStateException(%22handshake not complete%22);%0A    %7D
 %0A   
@@ -2015,16 +2015,19 @@
 .get
+Now
 ();%0A    
 if (
@@ -2026,103 +2026,33 @@
     
-if (handshake == null) %7B%0A      throw new IllegalStateException(%22handshake not complete%22);%0A    %7D
+assert handshake != null;
 %0A   
@@ -2068,36 +2068,16 @@
 andshake
-.negotiatedVersion()
 ;%0A  %7D%0A%0A 

use netty promise