// Path: ddp-client/src/main/java/io/advantageous/ddp/DDPError.java
// public class DDPError {
// 
//     private final int error;
// 
//     private final String reason;
// 
//     private final String details;
// 
//     public DDPError(final int error, final String reason, final String details) {
//         this.error = error;
//         this.reason = reason;
//         this.details = details;
//     }
// 
//     public int getError() {
//         return error;
//     }
// 
//     public String getReason() {
//         return reason;
//     }
// 
//     public String getDetails() {
//         return details;
//     }
// 
//     @Override
//     public String toString() {
//         return "DDPError{" +
//                 "error=" + error +
//                 ", reason='" + reason + '\'' +
//                 ", details='" + details + '\'' +
//                 '}';
//     }
// }
// 
// Path: ddp-client/src/main/java/io/advantageous/ddp/DDPMessageEndpoint.java
// public interface DDPMessageEndpoint {
//     void disconnect() throws IOException;
// 
//     void connect(String address) throws IOException, InterruptedException;
// 
//     <T> void registerHandler(Class<T> messageType, DDPMessageHandler.Phase phase, DDPMessageHandler<T> handler);
// 
//     <T> void registerHandler(Class<T> messageType, DDPMessageHandler<T> handler);
// 
//     void await() throws InterruptedException;
// 
//     void send(Object message) throws IOException;
// 
// }


import io.advantageous.ddp.DDPError;
import io.advantageous.ddp.DDPMessageEndpoint;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
/*
 * Copyright (C) 2014. Geoffrey Chandler.
 *
 * 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 io.advantageous.ddp.rpc;



/**
 * This is a client for Meteor's RPC protocol.
 *
 * @author geoffc@gmail.com
 * @since 1/18/14 at 12:55 AM.
 */
public class RPCClientImpl implements RPCClient {

    private static final AtomicLong SEQUENCE = new AtomicLong(0);

    private static final Map<String, DeferredMethodInvocation> CALLBACK_MAP = new ConcurrentHashMap<>();

    private final DDPMessageEndpoint client;

    @Inject
    public RPCClientImpl(final DDPMessageEndpoint client) {
        this.client = client;
        this.client.registerHandler(ResultMessage.class, result -> {
            final DeferredMethodInvocation invocation = CALLBACK_MAP.get(result.getId());

            // Exit early if this message wasn't for us.
            if (invocation == null) return;
// Path: ddp-client/src/main/java/io/advantageous/ddp/DDPError.java
// public class DDPError {
// 
//     private final int error;
// 
//     private final String reason;
// 
//     private final String details;
// 
//     public DDPError(final int error, final String reason, final String details) {
//         this.error = error;
//         this.reason = reason;
//         this.details = details;
//     }
// 
//     public int getError() {
//         return error;
//     }
// 
//     public String getReason() {
//         return reason;
//     }
// 
//     public String getDetails() {
//         return details;
//     }
// 
//     @Override
//     public String toString() {
//         return "DDPError{" +
//                 "error=" + error +
//                 ", reason='" + reason + '\'' +
//                 ", details='" + details + '\'' +
//                 '}';
//     }
// }
// 
// Path: ddp-client/src/main/java/io/advantageous/ddp/DDPMessageEndpoint.java
// public interface DDPMessageEndpoint {
//     void disconnect() throws IOException;
// 
//     void connect(String address) throws IOException, InterruptedException;
// 
//     <T> void registerHandler(Class<T> messageType, DDPMessageHandler.Phase phase, DDPMessageHandler<T> handler);
// 
//     <T> void registerHandler(Class<T> messageType, DDPMessageHandler<T> handler);
// 
//     void await() throws InterruptedException;
// 
//     void send(Object message) throws IOException;
// 
// }


// Path: ddp-client/src/main/java/io/advantageous/ddp/rpc/RPCClientImpl.java
import io.advantageous.ddp.DDPError;
import io.advantageous.ddp.DDPMessageEndpoint;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/*
 * Copyright (C) 2014. Geoffrey Chandler.
 *
 * 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 io.advantageous.ddp.rpc;



/**
 * This is a client for Meteor's RPC protocol.
 *
 * @author geoffc@gmail.com
 * @since 1/18/14 at 12:55 AM.
 */
public class RPCClientImpl implements RPCClient {

    private static final AtomicLong SEQUENCE = new AtomicLong(0);

    private static final Map<String, DeferredMethodInvocation> CALLBACK_MAP = new ConcurrentHashMap<>();

    private final DDPMessageEndpoint client;

    @Inject
    public RPCClientImpl(final DDPMessageEndpoint client) {
        this.client = client;
        this.client.registerHandler(ResultMessage.class, result -> {
            final DeferredMethodInvocation invocation = CALLBACK_MAP.get(result.getId());

            // Exit early if this message wasn't for us.
            if (invocation == null) return;
            final DDPError error = result.getError();