<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.livedata.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeMsg;
import org.fudgemsg.FudgeMsgEnvelope;
import org.fudgemsg.mapping.FudgeDeserializer;
import org.fudgemsg.mapping.FudgeSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.livedata.LiveDataListener;
import com.opengamma.livedata.LiveDataSpecification;
import com.opengamma.livedata.LiveDataValueUpdateBean;
import com.opengamma.livedata.LiveDataValueUpdateBeanFudgeBuilder;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.livedata.msg.LiveDataSubscriptionRequest;
import com.opengamma.livedata.msg.LiveDataSubscriptionResponse;
import com.opengamma.livedata.msg.LiveDataSubscriptionResponseMsg;
import com.opengamma.livedata.msg.LiveDataSubscriptionResult;
import com.opengamma.livedata.msg.SubscriptionType;
import com.opengamma.transport.FudgeMessageReceiver;
import com.opengamma.transport.FudgeRequestSender;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.PublicAPI;
import com.opengamma.util.fudgemsg.OpenGammaFudgeContext;

/**
 * A client that talks to a remote LiveData server through an unspecified protocol.
 * Possibilities are JMS, Fudge, direct socket connection, and so on. 
 */
@PublicAPI
public class DistributedLiveDataClient extends AbstractLiveDataClient implements FudgeMessageReceiver {
  private static final Logger s_logger = LoggerFactory.getLogger(DistributedLiveDataClient.class);
  // Injected Inputs:
  private final FudgeContext _fudgeContext;
  private final FudgeRequestSender _subscriptionRequestSender;
  
  private final DistributedEntitlementChecker _entitlementChecker;
  
  /**
   * An exception will be thrown when doing a snapshot if no reply is received from the server
   * within this time. Milliseconds.
   */
  private static final long TIMEOUT = 1800000;
  
  public DistributedLiveDataClient(
      FudgeRequestSender subscriptionRequestSender,
      FudgeRequestSender entitlementRequestSender) {
    this(subscriptionRequestSender, entitlementRequestSender, OpenGammaFudgeContext.getInstance());
  }

  public DistributedLiveDataClient(
      FudgeRequestSender subscriptionRequestSender,
      FudgeRequestSender entitlementRequestSender,
      FudgeContext fudgeContext) {
    ArgumentChecker.notNull(subscriptionRequestSender, "Subscription request sender");
    ArgumentChecker.notNull(entitlementRequestSender, "Entitlement request sender");
    ArgumentChecker.notNull(fudgeContext, "Fudge Context");
    
    _subscriptionRequestSender = subscriptionRequestSender;
    _fudgeContext = fudgeContext;
    
    _entitlementChecker = new DistributedEntitlementChecker(entitlementRequestSender, fudgeContext);
  }

  /**
   * @return the subscriptionRequestSender
   */
  public FudgeRequestSender getSubscriptionRequestSender() {
    return _subscriptionRequestSender;
  }

  /**
   * @return the fudgeContext
   */
  @Override
  public FudgeContext getFudgeContext() {
    return _fudgeContext;
  }

  @Override
  protected void cancelPublication(LiveDataSpecification fullyQualifiedSpecification) {
    s_logger.info("Request made to cancel publication of {}", fullyQualifiedSpecification);
    // TODO kirk 2009-10-28 -- This should handle an unsubscription request. For now,
    // however, we can just make do with allowing the heartbeat to time out.
  }

  @Override
  protected void handleSubscriptionRequest(Collection<SubscriptionHandle> subHandles) {
    ArgumentChecker.notEmpty(subHandles, "Subscription handle collection");
    
    // Determine common user and subscription type
    UserPrincipal user = null;
    SubscriptionType type = null;
    
    ArrayList<LiveDataSpecification> specs = new ArrayList<>();
    for (SubscriptionHandle subHandle : subHandles) {

      // TODO - as a LiveDataSpec is immutable, why do we copy rather than just use it?
      specs.add(new LiveDataSpecification(subHandle.getRequestedSpecification()));
      
      if (user == null) {
        user = subHandle.getUser();
      } else if (!user.equals(subHandle.getUser())) {
        throw new OpenGammaRuntimeException("Not all usernames are equal");        
      }
      
      if (type == null) {<commit_msg>ACTIV-55 - Activ LiveDataClient compatibility<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.livedata.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.fudgemsg.FudgeContext;
import org.fudgemsg.FudgeMsg;
import org.fudgemsg.FudgeMsgEnvelope;
import org.fudgemsg.mapping.FudgeDeserializer;
import org.fudgemsg.mapping.FudgeSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.livedata.LiveDataListener;
import com.opengamma.livedata.LiveDataSpecification;
import com.opengamma.livedata.LiveDataValueUpdateBean;
import com.opengamma.livedata.LiveDataValueUpdateBeanFudgeBuilder;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.livedata.msg.LiveDataSubscriptionRequest;
import com.opengamma.livedata.msg.LiveDataSubscriptionResponse;
import com.opengamma.livedata.msg.LiveDataSubscriptionResponseMsg;
import com.opengamma.livedata.msg.LiveDataSubscriptionResult;
import com.opengamma.livedata.msg.SubscriptionType;
import com.opengamma.transport.FudgeMessageReceiver;
import com.opengamma.transport.FudgeRequestSender;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.PublicAPI;
import com.opengamma.util.fudgemsg.OpenGammaFudgeContext;

/**
 * A client that talks to a remote LiveData server through an unspecified protocol.
 * Possibilities are JMS, Fudge, direct socket connection, and so on. 
 */
@PublicAPI
public class DistributedLiveDataClient extends AbstractLiveDataClient implements FudgeMessageReceiver {
  private static final Logger s_logger = LoggerFactory.getLogger(DistributedLiveDataClient.class);
  // Injected Inputs:
  private final FudgeContext _fudgeContext;
  private final FudgeRequestSender _subscriptionRequestSender;
  
  private final DistributedEntitlementChecker _entitlementChecker;
  
  /**
   * An exception will be thrown when doing a snapshot if no reply is received from the server
   * within this time. Milliseconds.
   */
  private static final long TIMEOUT = 1800000;
  
  public DistributedLiveDataClient(
      FudgeRequestSender subscriptionRequestSender,
      FudgeRequestSender entitlementRequestSender) {
    this(subscriptionRequestSender, entitlementRequestSender, OpenGammaFudgeContext.getInstance());
  }

  public DistributedLiveDataClient(
      FudgeRequestSender subscriptionRequestSender,
      FudgeRequestSender entitlementRequestSender,
      FudgeContext fudgeContext) {
    ArgumentChecker.notNull(subscriptionRequestSender, "Subscription request sender");
    ArgumentChecker.notNull(entitlementRequestSender, "Entitlement request sender");
    ArgumentChecker.notNull(fudgeContext, "Fudge Context");
    
    _subscriptionRequestSender = subscriptionRequestSender;
    _fudgeContext = fudgeContext;
    
    _entitlementChecker = new DistributedEntitlementChecker(entitlementRequestSender, fudgeContext);
  }

  /**
   * @return the subscriptionRequestSender
   */
  public FudgeRequestSender getSubscriptionRequestSender() {
    return _subscriptionRequestSender;
  }

  /**
   * @return the fudgeContext
   */
  @Override
  public FudgeContext getFudgeContext() {
    return _fudgeContext;
  }

  @Override
  protected void cancelPublication(LiveDataSpecification fullyQualifiedSpecification) {
    s_logger.info("Request made to cancel publication of {}", fullyQualifiedSpecification);
    // TODO kirk 2009-10-28 -- This should handle an unsubscription request. For now,
    // however, we can just make do with allowing the heartbeat to time out.
  }

  @Override
  protected void handleSubscriptionRequest(Collection<SubscriptionHandle> subHandles) {
    ArgumentChecker.notEmpty(subHandles, "Subscription handle collection");
    
    // Determine common user and subscription type
    UserPrincipal user = null;
    SubscriptionType type = null;

    List<LiveDataSpecification> specs = Lists.newArrayListWithCapacity(subHandles.size());
    for (SubscriptionHandle subHandle : subHandles) {

      specs.add(subHandle.getRequestedSpecification());

      if (user == null) {
        user = subHandle.getUser();
      } else if (!user.equals(subHandle.getUser())) {
        throw new OpenGammaRuntimeException("Not all usernames are equal");        
      }
      
      if (type == null) {
{'fraction_non_alphanumeric': 0.07525247040938213, 'fraction_numerical': 0.004777934629167119, 'mean_word_length': 4.922829581993569, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '7594940', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2532, 'n_words': 610}