/*
 * 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.protocol.impl;

import java.util.concurrent.CompletableFuture;

import net.kuujo.copycat.protocol.AppendEntriesRequest;
import net.kuujo.copycat.protocol.AppendEntriesResponse;
import net.kuujo.copycat.protocol.ProtocolClient;
import net.kuujo.copycat.protocol.ProtocolException;
import net.kuujo.copycat.protocol.RequestVoteRequest;
import net.kuujo.copycat.protocol.RequestVoteResponse;
import net.kuujo.copycat.protocol.SubmitCommandRequest;
import net.kuujo.copycat.protocol.SubmitCommandResponse;
import net.kuujo.copycat.registry.Registry;
import net.kuujo.copycat.util.Args;

/**
 * Local protocol client.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
public class LocalProtocolClient implements ProtocolClient {
  private final String address;
  private final Registry registry;

  public LocalProtocolClient(String address, Registry registry) {
    this.address = address;
    this.registry = registry;
  }

  @Override
  public CompletableFuture<AppendEntriesResponse> appendEntries(AppendEntriesRequest request) {
    LocalProtocolServer server = registry.lookup(address);
    Args.checkNotNull(server, () -> {
      throw new ProtocolException("Invalid server address");
    });
    return server.appendEntries(request);
  }

  @Override
  public CompletableFuture<RequestVoteResponse> requestVote(RequestVoteRequest request) {
    LocalProtocolServer server = registry.lookup(address);
    Args.checkNotNull(server, () -> {
      throw new ProtocolException("Invalid server address");
    });
    return server.requestVote(request);
  }

  @Override
  public CompletableFuture<SubmitCommandResponse> submitCommand(SubmitCommandRequest request) {
    LocalProtocolServer server = registry.lookup(address);
    Args.checkNotNull(server, () -> {
      throw new ProtocolException("Invalid server address");
    });
    return server.submitCommand(request);
  }

  @Override
  public CompletableFuture<Void> connect() {
    return CompletableFuture.completedFuture(null);
  }

  @Override
  public CompletableFuture<Void> close() {
    return CompletableFuture.completedFuture(null);
  }

}

@@ -1179,44 +1179,8 @@
 try;
-%0Aimport net.kuujo.copycat.util.Args;
 %0A%0A/*
@@ -1713,54 +1713,148 @@
     
-Args.checkNotNull(server, () -%3E %7B%0A      throw 
+if (server == null) %7B%0A      CompletableFuture%3CAppendEntriesResponse%3E future = new CompletableFuture%3C%3E();%0A      future.completeExceptionally(
 new 
@@ -1892,33 +1892,53 @@
 er address%22)
+)
 ;%0A    
-%7D);
+  return future;%0A    %7D
 %0A    return 
@@ -2142,54 +2142,146 @@
     
-Args.checkNotNull(server, () -%3E %7B%0A      throw 
+if (server == null) %7B%0A      CompletableFuture%3CRequestVoteResponse%3E future = new CompletableFuture%3C%3E();%0A      future.completeExceptionally(
 new 
@@ -2319,33 +2319,53 @@
 er address%22)
+)
 ;%0A    
-%7D);
+  return future;%0A    %7D
 %0A    return 
@@ -2573,54 +2573,148 @@
     
-Args.checkNotNull(server, () -%3E %7B%0A      throw 
+if (server == null) %7B%0A      CompletableFuture%3CSubmitCommandResponse%3E future = new CompletableFuture%3C%3E();%0A      future.completeExceptionally(
 new 
@@ -2760,17 +2760,37 @@
 ss%22)
+)
 ;%0A    
-%7D);
+  return future;%0A    %7D
 %0A   

Return exceptional futures when protocol servers are missing from registry in local protocol.