/*
 * Copyright 2014 the original author or authors.
 *
 * 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 net.kuujo.copycat.internal.protocol;

import net.kuujo.copycat.internal.util.concurrent.NamedThreadFactory;
import net.kuujo.copycat.protocol.*;
import net.kuujo.copycat.spi.protocol.AsyncProtocolClient;
import net.kuujo.copycat.spi.protocol.ProtocolClient;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * Asynchronous protocol client wrapper.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
public class WrappedAsyncProtocolClient implements AsyncProtocolClient {
  private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("wrapped-async-protocol-client-%s");
  private final Executor executor = Executors.newCachedThreadPool(THREAD_FACTORY);
  private final ProtocolClient client;

  public WrappedAsyncProtocolClient(ProtocolClient client) {
    this.client = client;
  }

  @Override
  public CompletableFuture<PingResponse> ping(PingRequest request) {
    return CompletableFuture.supplyAsync(() -> client.ping(request), executor);
  }

  @Override
  public CompletableFuture<SyncResponse> sync(SyncRequest request) {
    return CompletableFuture.supplyAsync(() -> client.sync(request), executor);
  }

  @Override
  public CompletableFuture<PollResponse> poll(PollRequest request) {
    return CompletableFuture.supplyAsync(() -> client.poll(request), executor);
  }

  @Override
  public CompletableFuture<SubmitResponse> submit(SubmitRequest request) {
    return CompletableFuture.supplyAsync(() -> client.submit(request), executor);
  }

  @Override
  public CompletableFuture<Void> connect() {
    return CompletableFuture.runAsync(client::connect, executor);
  }

  @Override
  public CompletableFuture<Void> close() {
    return CompletableFuture.runAsync(client::close, executor);
  }

  @Override
  public String toString() {
    return getClass().getSimpleName();
  }

}

@@ -1231,205 +1231,8 @@
 t %7B%0A
-  private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory(%22wrapped-async-protocol-client-%25s%22);%0A  private final Executor executor = Executors.newCachedThreadPool(THREAD_FACTORY);%0A
   pr
@@ -1469,34 +1469,32 @@
 eFuture.
-supplyAsync(() -%3E 
+completedFuture(
 client.p
@@ -1501,34 +1501,24 @@
 ing(request)
-, executor
 );%0A  %7D%0A%0A  @O
@@ -1623,34 +1623,32 @@
 eFuture.
-supplyAsync(() -%3E 
+completedFuture(
 client.s
@@ -1655,34 +1655,24 @@
 ync(request)
-, executor
 );%0A  %7D%0A%0A  @O
@@ -1777,34 +1777,32 @@
 eFuture.
-supplyAsync(() -%3E 
+completedFuture(
 client.p
@@ -1809,34 +1809,24 @@
 oll(request)
-, executor
 );%0A  %7D%0A%0A  @O
@@ -1941,26 +1941,24 @@
 ure.
-supplyAsync(() -%3E 
+completedFuture(
 clie
@@ -1975,26 +1975,16 @@
 request)
-, executor
 );%0A  %7D%0A%0A
@@ -2048,191 +2048,551 @@
     
-return CompletableFuture.runAsync(client::connect, executor);%0A  %7D%0A%0A  @Override%0A  public CompletableFuture%3CVoid%3E close() %7B%0A    return CompletableFuture.runAsync(client::close, executor
+try %7B%0A      client.connect();%0A    %7D catch (Exception e) %7B%0A      CompletableFuture%3CVoid%3E future = new CompletableFuture%3C%3E();%0A      future.completeExceptionally(e);%0A      return future;%0A    %7D%0A    return CompletableFuture.completedFuture(null);%0A  %7D%0A%0A  @Override%0A  public CompletableFuture%3CVoid%3E close() %7B%0A    try %7B%0A      client.close();%0A    %7D catch (Exception e) %7B%0A      CompletableFuture%3CVoid%3E future = new CompletableFuture%3C%3E();%0A      future.completeExceptionally(e);%0A      return future;%0A    %7D%0A    return CompletableFuture.completedFuture(null
 );%0A 

Clean up wrapped async protocol client.