<commit_before>/**
 * Copyright (C) 2014, Apexes.net. All rights reserved.
 * 
 *        http://www.apexes.net
 * 
 */
package net.apexes.wsonrpc.internal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import javax.websocket.Session;

import net.apexes.wsonrpc.BinaryWrapper;
import net.apexes.wsonrpc.ExceptionProcessor;
import net.apexes.wsonrpc.RpcHandler;
import net.apexes.wsonrpc.RpcHandler.JsonMessage;
import net.apexes.wsonrpc.WsonrpcConfig;

/**
 * 
 * @author <a href=mailto:<EMAIL>>HeDYn</a>
 *
 */
class WsonrpcDispatcher implements Caller {

    private final ExecutorService execService;
    private final BinaryWrapper binaryProcessor;
    private final RpcHandler rpcHandler;
    private final long timeout;

    private ExceptionProcessor exceptionProcessor;

    public WsonrpcDispatcher(WsonrpcConfig config) {
        this.execService = config.getExecutorService();
        this.binaryProcessor = config.getBinaryWrapper();
        this.timeout = config.getTimeout();
        this.rpcHandler = config.getRpcHandler();
    }

    public void setExceptionProcessor(ExceptionProcessor processor) {
        this.exceptionProcessor = processor;
    }

    public ExceptionProcessor getExceptionProcessor() {
        return exceptionProcessor;
    }

    public void addService(String name, Object service) {
        rpcHandler.addService(name, service);
    }

    @Override
    public long getTimeout() {
        return timeout;
    }

    @Override
    public void notify(Session session, String serviceName, String methodName, Object argument)
            throws Exception {
        invoke(session, serviceName, methodName, argument, null);
    }

    @Override
    public Future<Object> request(Session session, String serviceName, String methodName, Object argument,
            Type returnType) throws Exception {
        String id = UUID.randomUUID().toString().replace("-", "");
        WosonrpcFuture<Object> future = new WosonrpcFuture<>(id, returnType);
        WsonrpcContext.Futures.put(future);
        try {
            invoke(session, serviceName, methodName, argument, id);
            return future;
        } catch (Exception ex) {
            WsonrpcContext.Futures.out(id);
            throw ex;
        }
    }

    private void invoke(Session session, String serviceName, String methodName, Object argument, String id)
            throws Exception {<commit_msg>ID采用标准的UUID<commit_after>/**
 * Copyright (C) 2014, Apexes.net. All rights reserved.
 * 
 *        http://www.apexes.net
 * 
 */
package net.apexes.wsonrpc.internal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import javax.websocket.Session;

import net.apexes.wsonrpc.BinaryWrapper;
import net.apexes.wsonrpc.ExceptionProcessor;
import net.apexes.wsonrpc.RpcHandler;
import net.apexes.wsonrpc.RpcHandler.JsonMessage;
import net.apexes.wsonrpc.WsonrpcConfig;

/**
 * 
 * @author <a href=mailto:<EMAIL>>HeDYn</a>
 *
 */
class WsonrpcDispatcher implements Caller {

    private final ExecutorService execService;
    private final BinaryWrapper binaryProcessor;
    private final RpcHandler rpcHandler;
    private final long timeout;

    private ExceptionProcessor exceptionProcessor;

    public WsonrpcDispatcher(WsonrpcConfig config) {
        this.execService = config.getExecutorService();
        this.binaryProcessor = config.getBinaryWrapper();
        this.timeout = config.getTimeout();
        this.rpcHandler = config.getRpcHandler();
    }

    public void setExceptionProcessor(ExceptionProcessor processor) {
        this.exceptionProcessor = processor;
    }

    public ExceptionProcessor getExceptionProcessor() {
        return exceptionProcessor;
    }

    public void addService(String name, Object service) {
        rpcHandler.addService(name, service);
    }

    @Override
    public long getTimeout() {
        return timeout;
    }

    @Override
    public void notify(Session session, String serviceName, String methodName, Object argument)
            throws Exception {
        invoke(session, serviceName, methodName, argument, null);
    }

    @Override
    public Future<Object> request(Session session, String serviceName, String methodName, Object argument,
            Type returnType) throws Exception {
        String id = UUID.randomUUID().toString();
        WosonrpcFuture<Object> future = new WosonrpcFuture<>(id, returnType);
        WsonrpcContext.Futures.put(future);
        try {
            invoke(session, serviceName, methodName, argument, id);
            return future;
        } catch (Exception ex) {
            WsonrpcContext.Futures.out(id);
            throw ex;
        }
    }

    private void invoke(Session session, String serviceName, String methodName, Object argument, String id)
            throws Exception {
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.0015311004784688996, 'mean_word_length': 3.3118811881188117, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5216960', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1421, 'n_words': 342}