<commit_before>package org.jetbrains.jps.client;

import com.google.protobuf.MessageLite;
import com.intellij.openapi.diagnostic.Logger;
import org.jboss.netty.channel.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.api.RequestFuture;

import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author <NAME>
*         Date: 1/22/12
*/
final class ProtobufClientMessageHandler<T extends ProtobufResponseHandler> extends SimpleChannelHandler {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.jps.client.ProtobufClientMessageHandler");
  private final ConcurrentHashMap<UUID, RequestFuture<T>> myHandlers = new ConcurrentHashMap<UUID, RequestFuture<T>>();
  @NotNull
  private final UUIDGetter myUuidGetter;
  private final SimpleProtobufClient myClient;

  public ProtobufClientMessageHandler(@NotNull UUIDGetter uuidGetter, SimpleProtobufClient client) {
    myUuidGetter = uuidGetter;
    myClient = client;
  }

  public final void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    final UUID messageUUID = myUuidGetter.getSessionUUID(e);
    final RequestFuture<T> future = myHandlers.get(messageUUID);
    final T handler = future != null ? future.getResponseHandler() : null;
    if (handler == null) {
      terminateSession(messageUUID);
    }
    else {
      boolean terminateSession = false;
      try {
        terminateSession = handler.handleMessage((MessageLite)e.getMessage());
      }
      catch (Exception ex) {
        terminateSession = true;
        throw ex;
      }
      finally {
        if (terminateSession) {
          terminateSession(messageUUID);
        }
      }
    }
  }

  private void terminateSession(UUID sessionId) {
    final RequestFuture<T> future = removeFuture(sessionId);
    if (future != null) {
      final T handler = future.getResponseHandler();
      try {
        if (handler != null) {
          try {
            handler.sessionTerminated();
          }
          catch (Throwable ignored) {
            ignored.printStackTrace();
          }
        }
      }
      finally {
        future.setDone();
      }
    }
  }

  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    try {
      super.channelClosed(ctx, e);
    }
    finally {
      for (UUID uuid : new ArrayList<UUID>(myHandlers.keySet())) {
        terminateSession(uuid);
      }
    }
  }

  @Override
  public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    try {
      super.channelDisconnected(ctx, e);
    }
    finally {
      // make sure the client is in disconnected state
      myClient.disconnect();
    }
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    try {
      LOG.info(e.getCause());
      ctx.sendUpstream(e);
    }
    finally {
      myClient.disconnect();
    }
  }

  public void registerFuture(UUID messageId, RequestFuture<T> requestFuture) {
    myHandlers.put(messageId, requestFuture);
  }

  public RequestFuture<T> removeFuture(UUID messageId) {
    return myHandlers.remove(messageId);
  }
}<commit_msg>do not disconnect from server on every exception<commit_after>package org.jetbrains.jps.client;

import com.google.protobuf.MessageLite;
import com.intellij.openapi.diagnostic.Logger;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.api.RequestFuture;

import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author <NAME>
*         Date: 1/22/12
*/
final class ProtobufClientMessageHandler<T extends ProtobufResponseHandler> extends SimpleChannelHandler {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.jps.client.ProtobufClientMessageHandler");
  private final ConcurrentHashMap<UUID, RequestFuture<T>> myHandlers = new ConcurrentHashMap<UUID, RequestFuture<T>>();
  @NotNull
  private final UUIDGetter myUuidGetter;
  private final SimpleProtobufClient myClient;

  public ProtobufClientMessageHandler(@NotNull UUIDGetter uuidGetter, SimpleProtobufClient client) {
    myUuidGetter = uuidGetter;
    myClient = client;
  }

  public final void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    final UUID messageUUID = myUuidGetter.getSessionUUID(e);
    final RequestFuture<T> future = myHandlers.get(messageUUID);
    final T handler = future != null ? future.getResponseHandler() : null;
    if (handler == null) {
      terminateSession(messageUUID);
    }
    else {
      boolean terminateSession = false;
      try {
        terminateSession = handler.handleMessage((MessageLite)e.getMessage());
      }
      catch (Exception ex) {
        terminateSession = true;
        throw ex;
      }
      finally {
        if (terminateSession) {
          terminateSession(messageUUID);
        }
      }
    }
  }

  private void terminateSession(UUID sessionId) {
    final RequestFuture<T> future = removeFuture(sessionId);
    if (future != null) {
      final T handler = future.getResponseHandler();
      try {
        if (handler != null) {
          try {
            handler.sessionTerminated();
          }
          catch (Throwable ignored) {
            ignored.printStackTrace();
          }
        }
      }
      finally {
        future.setDone();
      }
    }
  }

  public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    try {
      super.channelClosed(ctx, e);
    }
    finally {
      for (UUID uuid : new ArrayList<UUID>(myHandlers.keySet())) {
        terminateSession(uuid);
      }
    }
  }

  @Override
  public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    try {
      super.channelDisconnected(ctx, e);
    }
    finally {
      // make sure the client is in disconnected state
      myClient.disconnect();
    }
  }

  public void registerFuture(UUID messageId, RequestFuture<T> requestFuture) {
    myHandlers.put(messageId, requestFuture);
  }

  public RequestFuture<T> removeFuture(UUID messageId) {
    return myHandlers.remove(messageId);
  }
}
{'fraction_non_alphanumeric': 0.0945358584290593, 'fraction_numerical': 0.001552312946289972, 'mean_word_length': 3.1973941368078176, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5939282', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1842, 'n_words': 405}